Android Recyclerview停止加载已加载的项目

时间:2019-05-07 07:52:02

标签: android android-recyclerview lazy-loading

在我的RecyclcerView中,当我向下滚动并再次向上滚动时,我想停止装订顶部物品。

我尝试了此代码

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

,但位置0再次重新绑定。 我要执行此操作的原因是渲染每个项目的操作确实很繁琐,导致滚动时滞。  这是我的爸爸

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()));
}

1 个答案:

答案 0 :(得分:2)

您不能对回收商说停止加载来自顶部的新物品。

无论如何,回收者视图将调用onBindViewHolder,并且您必须在此功能中设置商品。

您可以做的是检测当前加载的项目(由onBindViewHolder给定)是来自底部还是顶部。

这里是我的意思:

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;
        }
    }

}

如果要重复使用向上滚动时从底部消失的数据,可以通过覆盖onViewRecycled

来获取

来源:

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

RecyclerView.Adapter.onViewRecycled()