问题描述
我正在从由 CursorAdapter 支持的 ListView 迁移到由 shywim 的 CursorRecyclerAdapter.
I'm migrating from a ListView backed by a CursorAdapter, to a RecyclerView backed by shywim's CursorRecyclerAdapter.
我在迁移用于返回游标对象的这部分时遇到问题:
I'm having trouble migrating this part that used to return a cursor object:
(MyCursor)mListView.getItemAtPosition(i)
如何访问 RecyclerView 中特定位置的光标?谢谢.
How to get access to cursor at specific position inside RecyclerView? Thanks.
推荐答案
遗憾的是它不是 RecyclerView 的一部分.为了克服它,我定义了一个接口:
Unfortunately it is not part of the RecyclerView. To overcome it I define an interface:
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
ViewHolder 实现了 View.OnClickListener,它的构造函数采用了一个实现了我的 OnItemClickListener 接口的对象:
The ViewHolder implements the View.OnClickListener, and its constructor takes an object that implements my OnItemClickListener interface:
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private OnItemClickListener mListener;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
}
public ViewHolder(View itemView, OnItemClickListener listener) {
this(itemView);
mListener = listener;
}
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onItemClick(v, getPosition());
}
}
当您单击该行时,我将通过侦听器转发视图单击的视图及其位置
When you click on the row, I forward trough the listener the view clicked and its position
这篇关于ListView.getItemAtPosition(i) 在 RecyclerView 中等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!