我已经使用 NestedScrollView 实现了 bottomsheet 行为。并且想知道在外部触摸时是否可以隐藏底部视图。
原文由 Pradeep Kumar Kushwaha 发布,翻译遵循 CC BY-SA 4.0 许可协议
我已经使用 NestedScrollView 实现了 bottomsheet 行为。并且想知道在外部触摸时是否可以隐藏底部视图。
原文由 Pradeep Kumar Kushwaha 发布,翻译遵循 CC BY-SA 4.0 许可协议
感谢 OP 的问题/答案。我使用了他的代码,但改进了它的清洁度并想分享。您可以直接在 BottomSheetBehavior 中编写代码,而不是扩展视图和添加界面。像这样:
AutoCloseBottomSheetBehavior.java
import android.content.Context;
import android.graphics.Rect;
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 AutoCloseBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {
public AutoCloseBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN &&
getState() == BottomSheetBehavior.STATE_EXPANDED) {
Rect outRect = new Rect();
child.getGlobalVisibleRect(outRect);
if (!outRect.contains((int) event.getRawX(), (int) event.getRawY())) {
setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
return super.onInterceptTouchEvent(parent, child, event);
}
}
然后您只需将它添加到您的 XML 布局中:
<?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">
... your normal content here ...
<SomeLayout... />
... the bottom sheet with the behavior
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="<com.package.name.of.the.class>.AutoCloseBottomSheetBehavior">
... the bottom sheet views
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
原文由 Budius 发布,翻译遵循 CC BY-SA 3.0 许可协议
3 回答973 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
1 回答846 阅读✓ 已解决
2 回答958 阅读
2 回答895 阅读
1 回答833 阅读
2 回答699 阅读
最后我能够做到这一点,
使用了以下代码行:
希望它能拯救某人一整天!