Ritesh Karmare
Ritesh Karmare

Reputation: 85

Refreshing RecyclerView adapter every 1 second

Below is my implementation for recycler view.

The recycler view is refreshing but the view is lagging alot making it not smooth to scroll. If I dont refresh data the recyclerView is smooth. The problem is with refreshing only..!!

I need to refresh data completely every 1 sec in RecyclerView maintaining same scroll position.

xml

 <android.support.v7.widget.RecyclerView
    android:id="@+id/recyView_disp_pgms"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null" />

Activity :

RecyclerView recyView_disp_pgms = (RecyclerView) findViewById(R.id.recyView_disp_pgms);

DisplayProgramsAdapterRecycler pgmAdapter = new DisplayProgramsAdapterRecycler(programsList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyView_disp_pgms.setLayoutManager(layoutManager);
recyView_disp_pgms.setAdapter(pgmAdapter);


handler = new Handler();
    runnable = new Runnable() {
        @Override
        public void run() {
            System.out.println("AllPgm Runflag : " + runflag);
            programList = // Get List from database

           if (programsList != null && programsList.size() > 0) {
            if (pgmAdapter != null && pgmAdapter.getItemCount() > 0) {
                pgmAdapter.resetData(programsList);
            }
        }


            handler.postDelayed(this, 1000);
        }
    };

    handler.post(runnable);

Adapter :

 public void resetData(ArrayList<ProgramDetails_Table> programsList) {
    this.programDetailsList = programsList;
    this.notifyDataSetChanged();
}

Problem - Lagging scroll of recyclerView only while refreshing data for every 1 sec

Tried following other posts; but still no luck. Please help..!!

Upvotes: 1

Views: 6632

Answers (4)

Toe
Toe

Reputation: 594

use DiffUtil. Don't use notifyDataSetChanged.

According to this https://developer.android.com/reference/android/support/v7/util/DiffUtil.html,

DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.

It can be used to calculate updates for a RecyclerView Adapter.

Unlike the notifyDataSetChanged method, it does not reload the whole list so it has a better performance.

And you don't have to manually find the differences between the new and the old list like the other notifyData methods from RecyclerView.Adapter.

Upvotes: 1

Vivekanandan
Vivekanandan

Reputation: 108

Make sure you are calling notifyDataSetChanged() inside the resetData function.

Also, try this instead of assigning directly to programList

programList.clear();
programList.addAll(arrayListOfDataFromServer);

Upvotes: 0

Uddhav P. Gautam
Uddhav P. Gautam

Reputation: 7626

Delete your line this.programDetailsList = programsList; and add the following. This is a simple hack, a way that makes you understand Async Callbacks.

/* I have done adding one element and then deleting the same element as the last element of programsList */
/* adding and deleting triggers notifyDataChange() callback */

//add an element in index 0 as the last element into the programList
programsList.add(programsList.size() - 1, programsList.get(0));

//remove the same last element from ProgramList
programsList.add(programsList.size() - 1);

//now it works
this.notifyDataSetChanged(programsList.size() - 1);

Upvotes: 0

Reza.Abedini
Reza.Abedini

Reputation: 2257

if you want to refresh your list find your changes and then just notify that position because notifyDataSetChanged resets all of your data. you should use lightweight notifies.something like this:

    notifyItemChanged(yourPosition);
    notifyItemInserted(yourPosition);
    notifyItemRemoved(yourPosition);

or if you get more than one change in a range you can use:

notifyItemRangeChanged(yourPosition);//and etc

Upvotes: 3

Related Questions