限制底部 sheet 向下滑动
Restrict bottom sheet swipe down
我创建了一个 activity 底部 sheet 行为。我在这里分享布局 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/video_details_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/backgroundColor"
app:behavior_hideable="true"
app:behavior_peekHeight="@dimen/player_sheet_peek_height"
app:layout_behavior="@string/bottom_sheet_behavior">
<LinearLayout
android:id="@+id/detailsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/mainMediaFrame"
android:layout_width="match_parent"
android:layout_height="@dimen/zero_dimen"
android:layout_weight="0.35"
android:background="@android:color/black">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/exoPlayerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
app:resize_mode="fit"
app:surface_type="texture_view" />
</FrameLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/videoScroller"
style="@style/scrollBarStyle"
android:layout_height="@dimen/zero_dimen"
android:layout_weight="0.65"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!--todo:include scroll view content layout-->
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</RelativeLayout>
实施后,我可以通过触摸并拖动此布局中的任何视图来折叠底部 sheet。但是,我想通过在布局中拖动视频视图 (mainMediaFrame) 来关闭 sheet。也就是说,我不想通过向下滚动嵌套滚动视图来关闭底部 sheet。我怎样才能做到这一点?
如果用户正在触摸 VideoView
,下面的解决方案将不会拖动底页。
概念很简单
- 禁用触摸 VideoView
- 用户触摸时禁用拖动VideoView
在你的Activity
final LockBottomSheetBehaviour behavior = (LockBottomSheetBehaviour) LockBottomSheetBehaviour.from(bottomSheet);
findViewById(R.id.videoView).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
behavior.setAllowUserDragging(false);
break;
case MotionEvent.ACTION_UP:
behavior.setAllowUserDragging(true);
break;
}
return true;
}
});
在布局中替换
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
到
app:layout_behavior="com.package.LockBottomSheetBehaviour"
LockBottomSheetBehaviour.class
import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class LockBottomSheetBehaviour<V extends View> extends BottomSheetBehavior<V> {
private boolean mAllowUserDragging = true;
public LockBottomSheetBehaviour() {
super();
}
public LockBottomSheetBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setAllowUserDragging(boolean allowUserDragging) {
mAllowUserDragging = allowUserDragging;
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
if (!mAllowUserDragging) {
return false;
}
return super.onInterceptTouchEvent(parent, child, event);
}
}
private class ScrollTouchListener implements View.OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mVideoDetailsView.setNestedScrollingEnabled(true);
activityLockBehavior.setAllowUserDragging(false);
break;
case MotionEvent.ACTION_MOVE:
mVideoDetailsView.setNestedScrollingEnabled(true);
activityLockBehavior.setAllowUserDragging(false);
break;
case MotionEvent.ACTION_UP:
mVideoDetailsView.setNestedScrollingEnabled(false);
activityLockBehavior.setAllowUserDragging(true);
break;
}
return false;
}
}
将此滚动侦听器添加到嵌套滚动视图
mNestedScrollView.setOnTouchListener(new ScrollTouchListener());
我创建了一个 activity 底部 sheet 行为。我在这里分享布局 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/video_details_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/backgroundColor"
app:behavior_hideable="true"
app:behavior_peekHeight="@dimen/player_sheet_peek_height"
app:layout_behavior="@string/bottom_sheet_behavior">
<LinearLayout
android:id="@+id/detailsLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/mainMediaFrame"
android:layout_width="match_parent"
android:layout_height="@dimen/zero_dimen"
android:layout_weight="0.35"
android:background="@android:color/black">
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/exoPlayerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
app:resize_mode="fit"
app:surface_type="texture_view" />
</FrameLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/videoScroller"
style="@style/scrollBarStyle"
android:layout_height="@dimen/zero_dimen"
android:layout_weight="0.65"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!--todo:include scroll view content layout-->
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</RelativeLayout>
实施后,我可以通过触摸并拖动此布局中的任何视图来折叠底部 sheet。但是,我想通过在布局中拖动视频视图 (mainMediaFrame) 来关闭 sheet。也就是说,我不想通过向下滚动嵌套滚动视图来关闭底部 sheet。我怎样才能做到这一点?
如果用户正在触摸 VideoView
,下面的解决方案将不会拖动底页。
概念很简单
- 禁用触摸 VideoView
- 用户触摸时禁用拖动VideoView
在你的Activity
final LockBottomSheetBehaviour behavior = (LockBottomSheetBehaviour) LockBottomSheetBehaviour.from(bottomSheet);
findViewById(R.id.videoView).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
behavior.setAllowUserDragging(false);
break;
case MotionEvent.ACTION_UP:
behavior.setAllowUserDragging(true);
break;
}
return true;
}
});
在布局中替换
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
到
app:layout_behavior="com.package.LockBottomSheetBehaviour"
LockBottomSheetBehaviour.class
import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class LockBottomSheetBehaviour<V extends View> extends BottomSheetBehavior<V> {
private boolean mAllowUserDragging = true;
public LockBottomSheetBehaviour() {
super();
}
public LockBottomSheetBehaviour(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setAllowUserDragging(boolean allowUserDragging) {
mAllowUserDragging = allowUserDragging;
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
if (!mAllowUserDragging) {
return false;
}
return super.onInterceptTouchEvent(parent, child, event);
}
}
private class ScrollTouchListener implements View.OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mVideoDetailsView.setNestedScrollingEnabled(true);
activityLockBehavior.setAllowUserDragging(false);
break;
case MotionEvent.ACTION_MOVE:
mVideoDetailsView.setNestedScrollingEnabled(true);
activityLockBehavior.setAllowUserDragging(false);
break;
case MotionEvent.ACTION_UP:
mVideoDetailsView.setNestedScrollingEnabled(false);
activityLockBehavior.setAllowUserDragging(true);
break;
}
return false;
}
}
将此滚动侦听器添加到嵌套滚动视图
mNestedScrollView.setOnTouchListener(new ScrollTouchListener());