gpt4 book ai didi

android - 获取 Item recyclerView 的文本

转载 作者:太空狗 更新时间:2023-10-29 16:31:54 28 4
gpt4 key购买 nike

我正在开发 Android 应用程序来完成它,但我在 recyclerView 中发现了问题,我不知道如何获取 recyclerView 的 Item 的文本? (当我点击项目时)

最佳答案

首先,您需要创建一个监听器,将 View 和位置作为参数提供。它不像 listView 那样直接存在。为此,您可以创建一个这样的类:

public class RecyclerClickListener implements RecyclerView.OnItemTouchListener {

private OnItemClickListener mListener;
GestureDetector mGestureDetector;

public interface OnItemClickListener {
void onItemClick(View view, int position);
}

public RecyclerClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildLayoutPosition(childView));
return true;
}
return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

}

然后在你的 Activity 中:

 recyclerView.addOnItemTouchListener( // and the click is handled
new RecyclerClickListener(getActivity(), new RecyclerClickListener.OnItemClickListener() {
@Override public void onItemClick(View view, int position) {
Log.d("click item", String.valueOf(position));
TextView textView = (TextView) view.findViewById(R.id.YourTextViewID);
Log.d("test",textView.getText().toString());
}
}
}));

关于android - 获取 Item recyclerView 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36892815/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com