2017-06-05 68 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

相关问题