【问题标题】:HorizontalScrollView conflict with Recyclerview swipe androidHorizo​​ntalScrollView 与 Recyclerview 滑动 android 冲突
【发布时间】:2015-04-14 11:13:55
【问题描述】:

我正在使用回收器视图滑动来关闭,我的卡片视图或回收器项目包含水平滚动视图。每次触摸都会优先滑动以关闭。这是我的代码...请告诉如何分开两次滑动?

<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>

【问题讨论】:

    标签: android android-recyclerview horizontalscrollview android-cardview


    【解决方案1】:

    在代码上,您必须将OnTouchListener 添加到HorizontalScrollView。然后,当它收到ACTION_MOVE 事件时,您可以禁止父视图(在本例中为RecyclerView)将水平滚动移动捕获为滑动:

    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 的内容没有填满整个单元格宽度(因此不要激活水平滚动),您可能不想禁止滑动。您可以通过使用检查宽度条件的布尔值更改 true 来解决此问题,例如:

    horizontalScrollContents.measure(0, 0);
    boolean lockSwipe = horizontalScrollContents.getMeasuredWidth() > cellWidth;
    ...
    v.getParent().requestDisallowInterceptTouchEvent(lockSwipe);
    

    【讨论】:

    • 天啊,这太棒了。挣扎了一个月,现在我有一个解决方案。
    • 当我偶然发现你的这个答案时,我正要放弃滑动删除,而是引入一个删除按钮。竖起两个大拇指,伙计!作为一个魅力工作!
    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多