Android 每1秒刷新一次RecyclerView适配器

Android 每1秒刷新一次RecyclerView适配器,android,performance,android-recyclerview,Android,Performance,Android Recyclerview,下面是我对回收者视图的实现 “回收器”视图正在刷新,但该视图滞后很多,无法平滑滚动。 如果我不刷新数据,则recyclerView是平滑的。 问题在于仅刷新 我需要在保持相同滚动位置的RecyclerView中每1秒完全刷新一次数据 xml <android.support.v7.widget.RecyclerView android:id="@+id/recyView_disp_pgms" android:layout_width="match_parent"

下面是我对回收者视图的实现

“回收器”视图正在刷新,但该视图滞后很多,无法平滑滚动。 如果我不刷新数据,则recyclerView是平滑的。 问题在于仅刷新

我需要在保持相同滚动位置的RecyclerView中每1秒完全刷新一次数据

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" />
适配器:

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);
 public void resetData(ArrayList<ProgramDetails_Table> programsList) {
    this.programDetailsList = programsList;
    this.notifyDataSetChanged();
}
public void resetData(ArrayList programsList){
this.programDetailsList=程序列表;
this.notifyDataSetChanged();
}
问题-仅当每隔1秒刷新数据时,recyclerView滚动滞后

曾尝试跟随其他职位;但还是没有运气。
请帮忙

确保在resetData函数内调用notifyDataSetChanged()

此外,请尝试此操作,而不是直接分配给programList

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

确保正在resetData函数内调用notifyDataSetChanged()

此外,请尝试此操作,而不是直接分配给programList

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

如果要刷新列表,请查找更改,然后通知该位置,因为notifyDataSetChanged会重置所有数据。 您应该使用轻量级通知。如下所示:

    notifyItemChanged(yourPosition);
    notifyItemInserted(yourPosition);
    notifyItemRemoved(yourPosition);
或者,如果您在一个范围内得到多个更改,您可以使用:

notifyItemRangeChanged(yourPosition);//and etc

如果要刷新列表,请查找更改,然后通知该位置,因为notifyDataSetChanged会重置所有数据。 您应该使用轻量级通知。如下所示:

    notifyItemChanged(yourPosition);
    notifyItemInserted(yourPosition);
    notifyItemRemoved(yourPosition);
或者,如果您在一个范围内得到多个更改,您可以使用:

notifyItemRangeChanged(yourPosition);//and etc

删除您的行
this.programDetailsList=programsList
并添加以下内容。这是一个简单的技巧,可以让您理解异步回调

/* 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);

删除您的行
this.programDetailsList=programsList
并添加以下内容。这是一个简单的技巧,可以让您理解异步回调

/* 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);

使用
DiffUtil
。不要使用
notifyDataSetChanged

据此,

DiffUtil是一个实用程序类,可以计算 两个列表,并输出转换 将第一个列表转换为第二个列表

它可用于计算RecyclerView适配器的更新

与notifyDataSetChanged方法不同,它不会重新加载整个列表,因此具有更好的性能


您不必像使用
DiffUtil
中的其他
notifyData
方法一样,手动查找新列表和旧列表之间的差异。不要使用
notifyDataSetChanged

据此,

DiffUtil是一个实用程序类,可以计算 两个列表,并输出转换 将第一个列表转换为第二个列表

它可用于计算RecyclerView适配器的更新

与notifyDataSetChanged方法不同,它不会重新加载整个列表,因此具有更好的性能


而且您不必像
RecyclerView.Adapter

中的其他
notifyData
方法那样手动查找新列表和旧列表之间的差异。您说过“回收器视图正在刷新”。那么你想要什么呢?我希望recyclerview的滚动平滑;到目前为止,它每1秒重新设计一次列表,并且recyclerview相当滞后。。。!!你说“回收者视图令人耳目一新”。那么你想要什么呢?我希望recyclerview的滚动平滑;到目前为止,它每1秒重新设计一次列表,并且recyclerview相当滞后。。。!!我想刷新整个列表;因为我不知道列表中的哪些项目发生了变化,有多少项发生了变化。。我使用notifyItemRangeChanged(0,list.size());这使得recyclerview更具吸引力slow@RiteshKarmarenotifyItemRangeChanged(0,list.size());与notifyDataSetChanged相同,我建议您计算您的程序列表更改(如果更改计数小于列表大小),然后通知这些更改,因为大型arrayList上的notifyDataSetChange速度较慢,您无法使其更快;如果对于两个项目,其中一个textview为change,则如何更新适配器。。。。我如何跟踪哪个项目是正确的changed@RiteshKarmare,对RecycleView适配器的更新通过异步调用进行。这就是为什么有这么多回调。当您执行insert/delete和notifyDataSetChange()操作时,适配器知道您需要更新,并且它会更新。我希望每1秒刷新一次整个适配器,而此时正在更新。。问题是回收服务的滚动视图太大,我想刷新整个列表;因为我不知道列表中的哪些项目发生了变化,有多少项发生了变化。。我使用notifyItemRangeChanged(0,list.size());这使得recyclerview更具吸引力slow@RiteshKarmarenotifyItemRangeChanged(0,list.size());与notifyDataSetChanged相同,我建议您计算您的程序列表更改(如果更改计数小于列表大小),然后通知这些更改,因为大型arrayList上的notifyDataSetChange速度较慢,您无法使其更快;如果对于两个项目,其中一个textview为change,则如何更新适配器。。。。我如何跟踪哪个项目是正确的changed@RiteshKarmare,对RecycleView适配器的更新通过异步调用进行。这就是为什么有这么多回调。当您执行insert/delete和notifyDataSetChange()操作时,适配器知道您需要更新,并且它会更新。我希望每1秒刷新一次整个适配器,而此时正在更新。。问题是RecycleServiceWnotifyDataSetChanged()的laggy滚动视图在现有代码中工作。。。。问题是滚动的时间太长recyclerView@RiteshKarmare,notifyDataSetChanged()是一个回调,由后端的AsyncTask控制。notifyDataSetChanged(索引)仅跟踪pa中的数据集更改