gpt4 book ai didi

android - 如何在触摸时获取 recyclerView 的项目的位置

转载 作者:太空狗 更新时间:2023-10-29 16:34:21 26 4
gpt4 key购买 nike

我在 DrawerLayout 中有一个 recyclerView。每个行项目都有一个 ImageView 图标和一个 Textbox 文本。我使用 addOnItemTouchListener 来检测用户单击时行项目的位置,以便打开一个新 fragment 。这是我来自 addOnItemTouchListener 的代码:

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View item = rv.findChildViewUnder(e.getX(),e.getY()); //finding the view that clicked , using coordinates X and Y
int position = rv.getChildLayoutPosition(item); //getting the position of the item inside the list
//Log.d("billy",String.valueOf(position));
onListViewItemClick(position);
mDrawer.closeDrawers(); // closing the navigation drawer
return true;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
});

这是 onListViewItemClick 方法:

public void onListViewItemClick(int position) {
DrawerItem data = mList.get(position); //Item of the recycler View at specific position
Bundle lbundle = new Bundle();
lFragmentManager = getFragmentManager();
lFragment = lFragmentManager.findFragmentById(R.id.frame_container);
lFragment = new FragmentHome();
lbundle.putSerializable("Item", data); // sending to fragment the Item of the Recycler View , which pressed
lFragment.setArguments(lbundle);
//To the fragment
lFragmentManager.beginTransaction().replace(R.id.frame_container ,lFragment ).commit();
}

问题在于,只有当用户单击抽屉导航的行项目的 imageView 时,此实现才有效。如果用户单击 Item 行的 TextView,则变量 position 的值为 -1 。我认为方法 findChildViewUnder(e.getX(), e.getY()) 只返回 imageView 而不是两者。我想要 findChildViewUnder(e.getx(),e.getY()) 返回 viewGroup。

这是定义每一行的 XML 文件 - 抽屉导航的项目:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground">
<LinearLayout
android:id="@+id/inner_layout"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/nv_title"
android:layout_alignTop="@+id/nv_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="@+id/nv_image"
android:background="@android:color/transparent"
android:scaleType="fitXY"
android:layout_marginLeft="15dp"
android:layout_gravity="center"/>

</LinearLayout>

<TextView
android:id="@+id/nv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="13dp"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/inner_layout"
android:layout_gravity="center"
android:singleLine="true"
android:ellipsize="end"
android:layout_toRightOf="@+id/inner_layout" />

</RelativeLayout>

感谢您的帮助!

最佳答案

如果您已经注册了ItemTouchListener,您可以获得如下 View 位置:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private static final String TAG = RecyclerItemClickListener.class.getSimpleName();

public void setListener(OnItemClickListener mListener) {
this.mListener = mListener;
}

private OnItemClickListener mListener;

public RecyclerItemClickListener(Context context) {
this(context, null);
}


GestureDetector mGestureDetector;

public RecyclerItemClickListener(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(view, childView, view.getChildAdapterPosition(childView));
}
return false;
}

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

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
}

语句 view.getChildAdapterPosition(childView)RecyclerView 的方法,返回触摸 View 在适配器中的位置。

关于android - 如何在触摸时获取 recyclerView 的项目的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32680240/

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