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

时间:2015-09-20 14:03:13

标签: android navigation-drawer android-recyclerview

recyclerView内有DrawerLayout。每个行项目都有ImageView,图标和Textbox文本。我使用addOnItemTouchListener来检测用户点击时行项的位置,以便打开新的片段。这是我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>

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

如果在onBindViewHolder

上设置onClick侦听器,则可以访问该位置
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//TODO: Use position here.
}