Set notifyDataSetChanged() on Recyclerview adapter

前端 未结 5 990
说谎
说谎 2020-12-11 02:25

I\'m implementing an endless data loading for a RecyclerView. When software detects that last item is going to be shown, it downloads new items and call to the loadMor

相关标签:
5条回答
  • 2020-12-11 02:30

    Issue is in these lines..

         adapter = new RVAdapter(RecyclerViewActivity.this, list);
            rv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
    

    You are initialising your adapter every time. No need to reinitialize it. Just update your arraylist and invoking to adapter.notifyDataSetChanged(); will make it work.

    0 讨论(0)
  • 2020-12-11 02:36

    you are setting the new list to the RecyclerView Adapter , set the list in the Adapter:

    make a method setItems(list) in adapter and call it before notifyDataSetChanged() and in adapter do

    this.persons = new ArrayList<>(persons);
    

    in setItems

    add this method in adapter:

    public void setItems(List<ServiceModel> persons) {
        this.persons = persons;
    }
    

    and call it before notifyDataSetChanged() like this:

    adapter.setItems(list);
    adapter.notifyDataSetChanged();
    
    0 讨论(0)
  • 2020-12-11 02:37

    Every time you fill your list call the method below:

    if (adapter != null) // it works second time and later
       adapter.notifyDataSetChanged();
    else { // it works first time
      adapter = new AdapterClass(context,list);
      listView.setAdapter(adapter);
    }
    
    0 讨论(0)
  • 2020-12-11 02:42

    Like @Beena mentioned, you are creating and setting new adapter ever time, in your success response.

    One approach would be to create an adapter and set it to the recycler view only for the first time, and then onSuceess() of your api callback, call a method of your adapter.

    In, them adapter method, just add that new data in your main arraylist and do notifyItemInserted() instead of notifyDataSetChanged, in this way you will also see the default adding animation of recyclerView.

    0 讨论(0)
  • 2020-12-11 02:46

    I solved this with added method in RVAdapter that call notifyDataSetChanged().

    Simply in RVAdapter:

    public void refreshList(){
        notifyDataSetChanged();
    }
    

    and call this method in MainActivity when need:

    rVAdapter.refreshList();

    0 讨论(0)
提交回复
热议问题