gpt4 book ai didi

android - 如何检查在 recyclerview 子项中单击了哪个项目

转载 作者:行者123 更新时间:2023-11-29 14:37:16 25 4
gpt4 key购买 nike

recyclers child view包含两个可点击的对象,一个是一组TextViews和ImageView,另一个是一个CheckBox(引用 - Android Recyclerview Multiple onclick items)我想看看是否在 child 身上点击了复选框。如果是这样,那么我更改 CheckBox 的状态。否则,我发起另一项 Activity (为了结果)。上面的链接显示了处理 View 持有者中的点击。我想在设置 RecyclerView 的其他 Activity 中处理点击。

    //the view needs a listener
final GestureDetector mGestureDetector = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {

@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}

});


mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());

if(child!=null && mGestureDetector.onTouchEvent(motionEvent)){

if(child.getId() == R.id.chk_box_pin){
//checking, this didn't work
}
else {
TextView idView = (TextView) child.findViewById(R.id.note_id_txt_vw);
if (DEBUG) Toast.makeText(getActivity(),
"ID is " + idView.getText().toString(), Toast.LENGTH_SHORT).show();
startEditNoteActivity(ActivityContract.ACTIVITY_EDIT, idView.getText().toString());
}
return true;
}

return false;
}

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

}
});

有没有一种方法可以处理对 Activity 中子项(而不是 ViewHolder)中的多个项目的点击?

最佳答案

您始终可以创建一个将事件传递给 Activity 的接口(interface)。例如

public interface ChildItemClickListener { 
public void onClick(View v, int position);
}

然后在你的适配器中

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
/* your code*/
...
private ChildItemClickListener listener;
...
*/
public void setChildItemClickListener(ChildItemClickListener listener) {
this.listener = listener;
}
}

然后你像这样调用它

if(child!=null && mGestureDetector.onTouchEvent(motionEvent)) {
if(child.getId() == R.id.chk_box_pin){
//checking, this didn't work
}
else {
TextView idView = (TextView) child.findViewById(R.id.note_id_txt_vw);
if (DEBUG) Toast.makeText(getActivity(),
"ID is " + idView.getText().toString(), Toast.LENGTH_SHORT).show();
startEditNoteActivity(ActivityContract.ACTIVITY_EDIT, idView.getText().toString());
}
if(listener != null)
listener.onClick(YOUR_VIEW, POSITION);
return true;
}

就是这样 :) 希望对您有所帮助,祝您好运

关于android - 如何检查在 recyclerview 子项中单击了哪个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175426/

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