【发布时间】:2016-01-29 12:30:36
【问题描述】:
我正在尝试使用CusrorAdapter 为Recyclerview 实现适配器,如下面的解决方案here 中所建议的那样。
我是 Android 新手,我不太清楚应该如何覆盖 CursorAdapter 的 newView 方法和 bindView 方法。另外我猜我的适配器将在ViewHolder 中有多个变量而不是一个(View v1),因为我的布局文件中有几个textViews,但我只是不知道它们如何都适合代码。
public class MyRecyclerAdapter extends Adapter<MyRecyclerAdapter.ViewHolder {
// PATCH: Because RecyclerView.Adapter in its current form doesn't natively support
// cursors, we "wrap" a CursorAdapter that will do all teh 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.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);
}
}
【问题讨论】:
标签: android cursor android-cursoradapter android-recyclerview