RecyclerView Endless Infinite Scrolling Issue

前端 未结 8 620
长发绾君心
长发绾君心 2020-12-31 01:36

I am trying to implement Endless Infinite Scrolling with RecyclerView, but I am only getting first 10 records, not getting next 10 records and even not getting any progress

8条回答
  •  滥情空心
    2020-12-31 01:46

    Issue-1: You have created a new instance of mAdapter before setting the LayoutManager for RecyclerView. Thereby the constructor code in the DataAdapter to add ScrollListener is not executed since recyclerView.getLayoutManager() returns null:

        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager){
            // code to add ScrollListener is never executed
        }
    

    Fix: First set the LayoutManager for the Recyclerview and then create the adapter like below:

        // use a linear layout manager
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new DataAdapter(temArray, mRecyclerView);
        // set the adapter object to the Recyclerview
        mRecyclerView.setAdapter(mAdapter);
    

    Issue-2: You have used temArray to create DataAdapter but in onLoadMore() you are using the studentList to add/remove new items, since studentList is not binded with mAdapter your changes doesn't reflect in the UI.

    Fix: Declare temArray as a class level variable and use temArray to manipulate the items.

     //class variable
     private ArrayList temArray = new ArrayList();
    
     handler.postDelayed(new Runnable() {
                @Override public void run() {
                  //   remove progress item
                  temArray.remove(temArray.size() - 1);
                  mAdapter.notifyItemRemoved(temArray.size());
                  //add items one by one
                  int start = temArray.size();
                  int end = start + 10;
                  if(end<=studentList.size()){
                    temArray.addAll(studentList.subList(start,end));
                  }   
                  mAdapter.setLoaded();
                }
              }, 2000);
    

提交回复
热议问题