RecyclerView Endless Infinite Scrolling Issue

前端 未结 8 568
长发绾君心
长发绾君心 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:41

    Try changing the

    int i = start + 1; i <= end; i++
    

    in the for loop to

    int i = start + 1; i < end; i++
    

    The <= validation adds an extra item.

    0 讨论(0)
  • 2020-12-31 01:42

    replace

    private List<Student> studentList;
    

    with

    private List<Object> list;
    

    also replace

    @Override
    public int getItemViewType(int position) {
        return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }
    

    with

    @Override
    public int getItemViewType(int position) {
      return list.get(position) instanceof Student ?  VIEW_ITEM : VIEW_PROG;
    }
    

    for detect end of List you can using

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
    
                // When went to the end of the list, load more posts
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
    
                    if (linearLayoutManager.findLastVisibleItemPosition() >= linearLayoutManager.getItemCount() - 1) {
    
                        // Grow List
                    }
                }
    }
    

    Also for Add Loading Item. add this code in adapter

    public void addLoadingView(){
       list.add(new Object());
       notifyDataSetChanged();
    }
    
    0 讨论(0)
  • 2020-12-31 01:43
            mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                        @Override
                        public void onLoadMore() {
                            //add null , so the adapter will check view_type and show progress bar at bottom
                            studentList.add(null);
                            mAdapter.notifyItemInserted(studentList.size() - 1);
    
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    //   remove progress item
                                    studentList.remove(studentList.size() - 1);
                                    mAdapter.notifyItemRemoved(studentList.size());
                                    //add items one by one
                                    int start = studentList.size();
                                    int end = start + 10;
    
                                    for (int i = start + 1; i < end; i++) {
       // studentList.add();
                                        mAdapter.notifyItemInserted(studentList.size());
                                    }
                                    mAdapter.setLoaded();
                                   //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
                                }
                            }, 2000);
                        }
                    });
    

    ok so you inserted progress bar, and then you've removed it as well but you never inserted the next student to show... something like studentList.add();

    I hope that solved your problem... good luck..

    0 讨论(0)
  • 2020-12-31 01:46

    Try notifyItemRangeChanged

    yourCurrentList.addAll(newData);    
    mAdapter.notifyItemRangeChanged(yourCurretList.size() + 1, newDataSize);
    

    I think this'll help you.

    0 讨论(0)
  • 2020-12-31 01:46

    1.

    Getting first 11 recyclerview Items blank (And showing progress bar continuously), see below screenshot:

    Change loadData method as:

    private void loadData() {
      new Parser().execute("http://clienturl.com/jsons/mytest.txt");  
     }
    

    2.

    Whereas I was suppose to get first 10 records, and on scroll next 10 records and so on...

    Change onPostExecute method of Parser as:

    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        ArrayList< Student > temArray = 
                     new ArrayList< Student >(studentList.subList(0, 10));           
        mAdapter = new DataAdapter(temArray, mRecyclerView);
    
        // set the adapter object to the Recyclerview
        mRecyclerView.setAdapter(mAdapter);
     }
    

    And also remove following lines from onCreate method:

        mAdapter = new DataAdapter(studentList, mRecyclerView);
    
        // set the adapter object to the Recyclerview
        mRecyclerView.setAdapter(mAdapter);
    
    0 讨论(0)
  • 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<Student> temArray = new ArrayList<Student>();
    
     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);
    
    0 讨论(0)
提交回复
热议问题