我读到了关于recyclerviews和游标适配器的this thread,并试图使用第一个solution。我对onviewholder函数的实现感到困惑。我知道onviewholder可以由(viewholder,int)或(viewholder,cursor)调用,但是我不知道如何在我的项目中使用它。
这是我原来的适配器
public class FeedAdapter extends CursorAdapter {
/*
Etiqueta de Depuración
*/
private static final String TAG = FeedAdapter.class.getSimpleName();
/**
* View holder para evitar multiples llamadas de findViewById()
*/
static class ViewHolder {
TextView titulo;
TextView descripcion;
int tituloI;
int descripcionI;
}
public FeedAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_layout, null, false);
ViewHolder vh = new ViewHolder();
// Almacenar referencias
vh.titulo = (TextView) view.findViewById(R.id.titulo);
vh.descripcion = (TextView) view.findViewById(R.id.descripcion);
// Setear indices
vh.tituloI = cursor.getColumnIndex(ScriptDatabase.ColumnEntradas.TITULO);
vh.descripcionI = cursor.getColumnIndex(ScriptDatabase.ColumnEntradas.DESCRIPCION);
view.setTag(vh);
return view;
}
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder vh = (ViewHolder) view.getTag();
// Setear el texto al titulo
vh.titulo.setText(cursor.getString(vh.tituloI));
// Obtener acceso a la descripción y su longitud
vh.descripcion.setText(descripcion);
}
}
我的新适配器和RecyclerView。OnBindViewHolder未完成,因为我不知道如何设置它。
public class RVAdapter extends CursorRecyclerViewAdapter<RVAdapter.ViewHolder>{
private Cursor mCursorAdapter;
public RVAdapter(Context context, Cursor cursor){
super(context,cursor);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView titulo;
TextView descripcion;
int tituloI;
int descripcionI;
public ViewHolder (View view){
super(view);
cv = (CardView) itemView.findViewById(R.id.cv);
titulo = (TextView) itemView.findViewById(R.id.titulo);
descripcion = (TextView) itemView.findViewById(R.id.descripcion);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
ViewHolder vh = new ViewHolder(itemView);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
mCursorAdapter.moveToPosition(position);
final ViewHolder vh = (ViewHolder) view.getTag();
}
}
在这种情况下,我应该使用“onBindViewHolder(ViewHolder ViewHolder,光标光标)”吗?我怎么能让它工作呢?
编辑1
这是扩展我的rvadapter的抽象类
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private Context mContext;
private Cursor mCursor;
private boolean mDataValid;
private int mRowIdColumn;
private DataSetObserver mDataSetObserver;
public CursorRecyclerViewAdapter(Context context, Cursor cursor) {
mContext = context;
mCursor = cursor;
mDataValid = cursor != null;
mRowIdColumn = mDataValid ? mCursor.getColumnIndex("_id") : -1;
mDataSetObserver = new NotifyingDataSetObserver();
if (mCursor != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
}
public Cursor getCursor() {
return mCursor;
}
@Override
public int getItemCount() {
if (mDataValid && mCursor != null) {
return mCursor.getCount();
}
return 0;
}
@Override
public long getItemId(int position) {
if (mDataValid && mCursor != null && mCursor.moveToPosition(position)) {
return mCursor.getLong(mRowIdColumn);
}
return 0;
}
@Override
public void setHasStableIds(boolean hasStableIds) {
super.setHasStableIds(true);
}
public abstract void onBindViewHolder(VH viewHolder, Cursor cursor);
@Override
public void onBindViewHolder(VH viewHolder, int position) {
if (!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
onBindViewHolder(viewHolder, mCursor);
}
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*/
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
/**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*/
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
final Cursor oldCursor = mCursor;
if (oldCursor != null && mDataSetObserver != null) {
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
mCursor = newCursor;
if (mCursor != null) {
if (mDataSetObserver != null) {
mCursor.registerDataSetObserver(mDataSetObserver);
}
mRowIdColumn = newCursor.getColumnIndexOrThrow("_id");
mDataValid = true;
notifyDataSetChanged();
} else {
mRowIdColumn = -1;
mDataValid = false;
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
return oldCursor;
}
private class NotifyingDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
super.onChanged();
mDataValid = true;
notifyDataSetChanged();
}
@Override
public void onInvalidated() {
super.onInvalidated();
mDataValid = false;
notifyDataSetChanged();
//There is no notifyDataSetInvalidated() method in RecyclerView.Adapter
}
}
}
最佳答案
您可以使用viewholder和position访问视图和字段,如
下表:
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
StructName name = names.get(position);
viewHolder.txttitle.setText(name.title);
viewHolder.rootItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
它比使用游标更容易
如果有帮助的话,让我知道。