Vas*_*foo 4 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) {
}
});
Run Code Online (Sandbox Code Playgroud)
这就是方法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();
}
Run Code Online (Sandbox Code Playgroud)
问题是,只有当用户单击imageView导航抽屉的行项目时,此实现才有效。如果用户单击行TextViewItem 的 ,则变量位置的值为 -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>
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
如果您已经注册,ItemTouchListener您可以获得以下查看位置:
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
语句view.getChildAdapterPosition(childView)是方法 fromRecyclerView并返回适配器中触摸视图的位置。
| 归档时间: |
|
| 查看次数: |
8095 次 |
| 最近记录: |