2017-06-05 71 views
1

我需要在RecycleView上加載一長串數據(5000-1000),並且RecycleView的每個項目都有很多細節,因此5 TextView,1 ImageViewProgressBar。所有的數據都存儲在SQLite DB。 加載一個包含所有數據並將其設置到適配器的ArrayList<Object>是更好嗎?還是將數據加載到適配器上更好? 請考慮我對適配器有其他事情要做,如重試圖像(使用Glide)。 任何建議或考慮都會有幫助!什麼是處理回收視圖上的大量數據的最佳方式

謝謝

回答

0

你可以嘗試只顯示數據的一部分,並使用Recycler View's OnScroll Listener加載和顯示更多當用戶已經達到了回收視圖(基本上,一個分頁)結束。這樣,您將不必支付一次加載所有數據的完整有效負載。

希望這會有所幫助!

0

您可以嘗試cursorRecyclarViewAdapter

https://gist.github.com/skyfishjy/443b7448f59be978bc59

public class MyRecyclerAdapter extends Adapter<MyRecyclerAdapter.ViewHolder> { 

// Because RecyclerView.Adapter in its current form doesn't natively 
// support cursors, we wrap a CursorAdapter that will do all the job 
// for us. 
CursorAdapter mCursorAdapter; 

Context mContext; 

public MyRecyclerAdapter(Context context, Cursor c) { 

    mContext = context; 

    mCursorAdapter = new CursorAdapter(mContext, c, 0) { 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      // Inflate the view here 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cursor) { 
      // Binding operations 
     } 
    }; 
} 

public static class ViewHolder extends RecyclerView.ViewHolder { 
    View v1; 

    public ViewHolder(View itemView) { 
     super(itemView); 
     v1 = itemView.findViewById(R.id.v1); 
    } 
} 

@Override 
public int getItemCount() { 
    return mCursorAdapter.getCount(); 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    // Passing the binding operation to cursor loader 
    mCursorAdapter.getCursor().moveToPosition(position); //EDITED: added this line as suggested in the comments below, thanks :) 
    mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor()); 

} 

@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    // Passing the inflater job to the cursor-adapter 
    View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent); 
    return new ViewHolder(v); 
} 
} 
+0

謝謝!我會試一試:-) – Pecana

0

如果您使用回收站視圖,然後我猜它實際上是最好的方法(但對我來說),用於裝載大名單...我認爲這兩個方法(存儲Arraylist並將數據發送到適配器)在某些情況下有效,但回收器視圖會銷燬已滾動的數據。但我認爲許多開發人員使用的最好方式和最有效的方法是一次性在屏幕上顯示數據量的限制,然後在滾動監​​聽器上使用以加載更多,然後再循環查看也可以做到這一點!

看看這裏非常完美

Android Endless List

相關問題