How to implement endless list with RecyclerView?

前端 未结 30 2687
无人及你
无人及你 2020-11-22 02:22

I would like to change ListView to RecyclerView. I want to use the onScroll of the OnScrollListener in RecyclerView to determine if a

30条回答
  •  逝去的感伤
    2020-11-22 03:06

    OK, I did it by using the onBindViewHolder method of RecyclerView.Adapter.

    Adapter:

    public interface OnViewHolderListener {
        void onRequestedLastItem();
    }
    
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
    
        ...
    
        if (position == getItemCount() - 1) onViewHolderListener.onRequestedLastItem();
    }
    

    Fragment (or Activity):

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.comments_list, container, false);
        recyclerView = (RecyclerView) mContentView.findViewById(R.id.my_recycler_view);
        adapter = new Adapter();
        recyclerView.setAdapter(adapter);
    
        ...
    
        adapter.setOnViewHolderListener(new Adapter.OnViewHolderListener() {
            @Override
            public void onRequestedLastItem() {
                //TODO fetch new data from webservice
            }
        });
        return contentView;
    }
    

提交回复
热议问题