我正在使用回收器 View 滑动来关闭,并且我的卡片 View 或回收器项目包含水平 ScrollView 。每次触摸都会优先选择滑动以关闭。这是我的代码...请告诉我如何分开两次滑动?
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_feed_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/activity_feed_image_layout"
android:layout_alignParentLeft="true"
android:layout_margin="@dimen/five_dp"
android:orientation="horizontal">
<com.workplains.androidapp.workmatec.LibraryClasses.CircularImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/image1"
android:id="@+id/task_feed_image" />
</LinearLayout>
<TextView
android:id="@+id/activity_feed_timestamp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_alignTop="@id/activity_feed_image_layout"
android:text="9:15 PM"
android:layout_margin="@dimen/five_dp"
android:textSize="12sp"
android:textColor="@color/actionbar_color" />
<TextView
android:id="@+id/task_feed_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Task By WorkMatec"
android:padding="@dimen/two_dp"
android:layout_alignTop="@id/activity_feed_image_layout"
android:layout_toRightOf="@+id/activity_feed_image_layout"
android:layout_toLeftOf="@id/activity_feed_timestamp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/task_feed_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Task Description by the workmatec. New Task TAsk by the workmatec"
android:padding="@dimen/two_dp"
android:layout_toRightOf="@+id/activity_feed_image_layout"
android:layout_below="@id/task_feed_title"
android:textColor="@android:color/black"
android:textSize="15sp" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/task_feed_attachments"
android:focusableInTouchMode="true"
android:accessibilityLiveRegion="polite"
android:layout_toRightOf="@+id/activity_feed_image_layout"
android:layout_below="@id/task_feed_desc">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bt_ic_imageplaceholder"
android:tint="@color/red"
android:tintMode="src_in" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bt_ic_imageplaceholder"
android:tint="@color/red"
android:tintMode="src_in" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
最佳答案
在代码中,您必须向 HorizontalScrollView
添加一个 OnTouchListener
。然后,当它收到 ACTION_MOVE
事件时,您可以禁止父 View (在本例中为 RecyclerView
)捕获水平滚动移 Action 为滑动:
HorizontalScrollView hScroll = [findViewById or whatever];
hScroll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
// Disallow the touch request for parent scroll on touch of child view
v.getParent().requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
请注意,如果您的 HorizontalScrollView
的内容没有填满整个单元格宽度(因此不会激活水平滚动),也许您不想禁止滑动。您可以通过使用检查宽度条件的 bool 值更改 true
来解决此问题,例如:
horizontalScrollContents.measure(0, 0);
boolean lockSwipe = horizontalScrollContents.getMeasuredWidth() > cellWidth;
...
v.getParent().requestDisallowInterceptTouchEvent(lockSwipe);
关于android - HorizontalScrollView与Recyclerview滑动android冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29625911/