使用 CursorLoader 时在 RecyclerView 项目中获取光标单击

Getting cursor in RecyclerView item click when using CursorLoader

您好,我正在使用 this 库来实现带有 CursorLoader 的 RecyclerView。它工作正常,但我想要的是在单击 RecyclerView 项目时获得光标。

如果是 ListView,我可以通过

轻松获取光标
Cursor cursor = (Cursor) listView.getAdapter.getItem(position); 

在 ListView Itemclick;

但我无法对 RecyclerView 执行相同的操作。如何做到这一点?

我尝试过的:

public class MovieAdapter extends RecyclerViewCursorAdapter<MovieAdapter.MovieViewHolder>{

/**
 * Column projection for the query to pull Movies from the database.
 */
public static final String[] MOVIE_COLUMNS = new String[] {
        MovieContract.MovieEntry.TABLE_NAME + "." + MovieContract.MovieEntry._ID,
        MovieContract.MovieEntry.COLUMN_NAME
};

/**
 * Index of the name column.
 */
private static final int NAME_INDEX = 1;

/**
 * Constructor.
 * @param context The Context the Adapter is displayed in.
 */
public MovieAdapter(Context context) {
    super(context);

    setupCursorAdapter(null, 0, R.layout.list_item_movie, false);
}

/**
 * Returns the ViewHolder to use for this adapter.
 */
@Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new MovieViewHolder(mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent));
}

/**
 * Moves the Cursor of the CursorAdapter to the appropriate position and binds the view for
 * that item.
 */
@Override
public void onBindViewHolder(MovieViewHolder holder, int position) {
    // Move cursor to this position
    mCursorAdapter.getCursor().moveToPosition(position);

    // Set the ViewHolder
    setViewHolder(holder);

    // Bind this view
    mCursorAdapter.bindView(null, mContext, mCursorAdapter.getCursor());
}

/**
 * ViewHolder used to display a movie name.
 */
public class MovieViewHolder extends RecyclerViewCursorViewHolder {
    public final TextView mMovieName;

    public MovieViewHolder(View view) {
        super(view);

        mMovieName = (TextView) view.findViewById(R.id.movie_name);

    }

    @Override
    public void bindCursor(Cursor cursor) {
        mMovieName.setText(cursor.getString(NAME_INDEX));
        mMovieName.setTag(mCursorAdapter.getCursor());
        mMovieName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getTag()!=null){
                    Cursor cursorTag = (Cursor)v.getTag();

                    **//Here I get last item's name in every item click using both cursorTag and  cursor.**
                    Toast.makeText(mContext, "SSSSS "+cursor.getString(NAME_INDEX),Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
  }
}

适配器有一个受保护的成员变量mCursor。您可以使用它来检索带有

的预期光标
if (mCursor != null) {
    mCursor.moveToPosition(position)
    //TODO: handle your functionality here (i.e. mCursor now points to the correct item)
}

但是,该特定库提供的 CursorAdapter 与其说是一个真正的解决方案,不如说是一种 hack。我建议使用更完整的库,例如 RecyclerExt 来控制一切,而不是试图将真正的功能交给 ListView CursorAdapter

这是我按照@RocketSpock 的回答修复它的方法。

@Override
public void onBindViewHolder(MovieViewHolder holder, int position) {
    // Move cursor to this position
    mCursorAdapter.getCursor().moveToPosition(position);

    // Set the ViewHolder
    setViewHolder(holder);

    // Bind this view
    mCursorAdapter.bindView(null, mContext, mCursorAdapter.getCursor());
    holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCursorAdapter.getCursor().moveToPosition(position);
            Toast.makeText(mContext, "SSSSS "+mCursorAdapter.getCursor()
                    .getString(NAME_INDEX),Toast.LENGTH_SHORT).show();
        }
    });

}

/**
 * ViewHolder used to display a movie name.
 */
public class MovieViewHolder extends RecyclerViewCursorViewHolder {
    public final TextView mMovieName;
    View view;

    public MovieViewHolder(View view) {
        super(view);

        this.view = view;
        mMovieName = (TextView) view.findViewById(R.id.movie_name);
    }

    @Override
    public void bindCursor(Cursor cursor) {
        mMovieName.setText(mCursorAdapter.getCursor().getString(NAME_INDEX));

    }

}