Ad

Android Recyclerview Stop Loading Items Which Already Loaded

In my RecyclcerView, when I scroll down and scroll back up again, I want to stop binding the top items.

I tried this code

recyclerView.getRecycledViewPool().setMaxRecycledViews(ItemTypeHead1, 0);

but position 0 gets rebinded again. the reason I want to do this is rendering of each item is really heavy and that cause lag on scrolling. this is my adapater

public class HomeSliderListAdapter extends AdvanceRecycleViewAdapter<HomeSliderListAdapter.Holder, Banner> implements OnRecycleItemClick<Banner> {

public interface CallBack {
    void onClick(Banner Banner);
}

private Context context;
private CallBack callBack;

public HomeSliderListAdapter(Context context, CallBack callBack) {
    this.context = context;
    this.callBack = callBack;
}

@Override
public Holder createDataHolder(ViewGroup parent, int viewType) {
    return new Holder(makeView(parent, R.layout.row_home_one_banner));
}

@Override
public void onBindDataHolder(Holder holder, int position) {
    Banner banner = getItems().get(position);

    EpizodUtil.getPicasso(context)
            .load(EpizodUtil.getConvertedImagePath(banner.getImagePath() + "", Common.ImageSize.s10))
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .into(holder.img_background);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (callBack != null) {
                callBack.onClick(banner);
            }
        }
    });

    holder.txt_title.setText(EpizodUtil.checkNullString(banner.getTitleFa()));
    holder.txt_subtitle.setText(EpizodUtil.checkNullString(banner.getSubtitleFa()));
}
Ad

Answer

You can't say to the recycler to stop to load the new items coming from the top.

In any case, the recycler view will call onBindViewHolder and it is in this function that you have to setup your item.

What you can do is to detect if the current item loaded (given by onBindViewHolder) is coming from the bottom or from the top.

Here an idea of what I mean :

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder>
{
    private int mLowestPosition = -1;

    ...

    public CustomAdapter(...)
    {
         ...
    }

    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
         ...
    }


    @Override
    public void onBindViewHolder(ViewHolder iHolder, int iPosition)
    {

        if (iPosition > mLowestPosition)
        {
             // All item coming from the bottom for their first time will pass here.
             // Bind your view here

             mLowestPosition = iPosition;
        }
    }

}

If you want to reuse the data disappearing from bottom when you are scrolling up, you can get it by overriding onViewRecycled

Sources :

https://stackoverflow.com/a/26748274/5384689

RecyclerView.Adapter.onViewRecycled()

Ad
source: stackoverflow.com
Ad