如何在用户停止拖动时停止底部工作表滚动?

时间:2017-01-25 06:46:37

标签: android android-layout

当用户抬起手指时,有没有办法阻止底部纸张显示停止(MotionEvent.ACTION_UP),我所看到的是底部纸张始终向上移动以显示整个内容。

我实现了自定义底页行为,但似乎没有做到这一点。这是底层表格行为java代码的代码。

import android.content.Context;
import android.graphics.Rect;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class LockableBottomSheetBehavior\<V extends View\> extends BottomSheetBehavior<V> {

    public LockableBottomSheetBehavior() {}

    public LockableBottomSheetBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
        return super.onInterceptTouchEvent(parent, child, event);
    }

    @Override
    public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {


        int action = MotionEventCompat.getActionMasked(event);
        if(action == MotionEvent.ACTION_UP && getState() == BottomSheetBehavior.STATE_DRAGGING) {
            Rect scrollBounds = new Rect();
            child.getGlobalVisibleRect(scrollBounds); //Should we set height of
            setState(BottomSheetBehavior.STATE_EXPANDED);
            return true;
        }
        return super.onTouchEvent(parent, child, event);;
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child, View directTargetChild, View target, int nestedScrollAxes) {

        return super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);

    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);

    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, View target) {
        super.onStopNestedScroll(coordinatorLayout, child, target);

    }

    @Override
    public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, V child, View target, float velocityX, float velocityY) {
        return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }
}`

此处是我正在使用的布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mayojava.sample.bottomsheetdemo.MainActivity">

    <RelativeLayout
        android:id="@+id/map_image_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/map_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/map"
            />

        <Button
            android:id="@+id/show_view_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="Show Bottom Sheet"/>
    </RelativeLayout>

    <ScrollView
        android:id="@+id/layout_bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:behavior_hideable="false"
        app:behavior_peekHeight="400dp"
        app:layout_behavior="com.snijsure.sample.bottomsheetdemo.LockableBottomSheetBehavior"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:background="@android:color/white"
            android:orientation="vertical"
            >

            <TextView
                android:id="@+id/title_text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:text="@string/title1"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_curate_images"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title_text1"
                android:clipToPadding="false"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:visibility="visible"/>

            <TextView
                android:id="@+id/title_text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/recycler_curate_images"
                android:text="@string/title2"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_curate_images_second"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title_text2"
                android:clipToPadding="false"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:visibility="visible"/>
        </RelativeLayout>
    </ScrollView>
</android.support.design.widget.CoordinatorLayout>

This is link to video

1 个答案:

答案 0 :(得分:0)

为了它的价值,我使用ObservableScrollView实现了一个解决方案。如果有人有兴趣sample is located at。您还可以找到Youtube video implementation here

希望如果有人希望将来能够实现这样的目标,那会有所帮助。