2017-07-18 164 views
0

以下是我的回收视图实施。刷新RecyclerView适配器每1秒

回收站视图令人耳目一新,但视图滞后,无法顺利滚动。 如果我不刷新数据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(); 
} 

问题 - 仅在每1秒更新一次数据时,回收站的滚动视图才会丢失

尝试关注其他帖子;但仍然没有运气。 请帮助.. !!

+0

你说'回收者视图令人耳目一新'。那么你想在这里做什么? – UmarZaii

+0

我想让recyclerview滚动平滑;截至目前它的刷新列表每1秒和recyclerview相当滞后......! –

回答

1

分配,如果你想更新你的列表中找到您的更改,然后就通知位置,因为notifyDataSetChanged重置您的所有数据。 你应该使用轻量级notifies.something这样的:

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

,或者如果你在一个范围内多于一个变化,你可以使用:

notifyItemRangeChanged(yourPosition);//and etc 
+0

我想刷新整个列表;因为我不会知道列表中的项目是否发生了变化以及发生了多少变化..我使用notifyItemRangeChanged(0,list.size());这使得recyclerview更慢 –

+0

@RiteshKarmare notifyItemRangeChanged(0,list.size());与notifyDataSetChanged相同,我建议你计算你的programList变化(如果变化数小于你的列表大小),然后只是通知这些变化,因为一个大arrayList上的notifyDataSetChange速度很慢,你不能让它变得更快。 –

+0

在我的recyclerView Items中有4个textView;如果对于两个项目其中一个textview是如何更新适配器....我如何跟踪哪些项目被更改 –

0

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

而且,试试这个,而不是直接programList

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

是的我正在调用它在我的重置数据函数 –

+0

而不是分配新的ArrayList到programList尝试清除它并使用addAll将新列表中的每个值添加到programList使用addAll检查编辑的答案 – Vivekanandan

+0

我已经尝试了上述解决方案;但这里的问题似乎是laggy滚动,因为刷新1秒 –

0

删除您的线路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); 
+0

notifyDataSetChanged()正在工作在现有的代码....问题是与laggy滚动recyclerView –

+0

@ RiteshKarmare,notifyDataSetChanged()是一个回调,由后端的AsyncTask控制。 notifyDataSetChanged(index)只跟踪特定索引中的数据集更改,因此效率更高。我把programListList()-1作为索引的逻辑是==>回收视图保持listitems的更新,最后一个更新的项目在programList.size()-1。如果你的programList.size()太大,你的屏幕只能保存5个项目(例如),那么当你执行notifyDataSetChanged(0)时,系统必须创建新的ListItem来在你的recyclerview中保存新项目。 –

+0

您正在制作的laggy,因为您不是在回收列表项;相反,你应该使用programsList.size() - 1作为索引。 –

0

使用DiffUtil。请勿使用notifyDataSetChanged

根据该https://developer.android.com/reference/android/support/v7/util/DiffUtil.html

DiffUtil是一个工具类,可以计算 两个列表并输出转换 第一列表到第二一个更新的操作的列表之间的差。

它可以用来计算RecyclerView适配器的更新。

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

而且您不必像RecyclerView.Adapter中的其他notifyData方法那样手动查找新列表和旧列表之间的差异。

+0

你需要解释'DiffUtil'是什么,并且确实如此,否则这个答案可能会因为质量太差 –

+0

好了..谢谢你的建议...... –