Search code examples
androidandroid-recyclerviewpicasso

disable recyclerview autoscroll with picasso


In my fragment I have some layout with text and below that layout is a recyclerview. Layout for items in adapter is a single imageView in which I download an image from REST API with picasso.

The problem is that when image is loaded screen automatically scrolled to the first item in recyclerview, so my layout with text is not visible, and whats strange it scrolls only to the first item, not each.

In other words: you open fragment -> you see layout with text -> when images loaded screen scrolls to first image and layout now somewhere above, not visible until you manually scroll to it.

How can I get rid of this annoying auto-scroll?

I also have a button to load more images, and if you press on it recycler won`t scroll after images loaded

Here's how I load an image:

override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, p1: Int) {
    Picasso.get().load(array_news[p1].image).into(target(viewHolder))
    //Picasso.get().load(array_news[p1].image).into(viewHolder.itemView.news_item_image)
}

private fun target(viewHolder: RecyclerView.ViewHolder) = object: Target {

    override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
        if (bitmap != null) {
            val bmp = Bitmap.createScaledBitmap(bitmap, 400, 400, true)
            viewHolder.itemView.news_item_image.setImageBitmap(bmp)
        }
    }
    override fun onPrepareLoad(placeHolderDrawable: Drawable?) {}
    override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {}
}

Solution

  • You can try this:

    <LinearLayout  
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true"
        android:orientation="vertical">
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    Assume the LinearLayout which is wrapping the RecyclerView is your parent layout. Just add this line:

    android:focusableInTouchMode="true"
    

    It will prevent the focusing issue most of the cases, especially when the RecyclerView is placed in NestedScrollView