2017-07-18 163 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

好了..謝謝你的建議...... –