我正在尝试将底部工作表用作底部的弹出窗口。我需要删除协调器布局中的拖动功能。我可以打电话
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
如果我想为 View 设置动画。我只是想阻止用户拖动他们的手指来拉起 View 。
<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:id="@+id/coordinator_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true" >
<LinearLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="true"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:orientation="vertical" >
<!-- view and stuff... -->
</LinearLayout>
</android.support.design.widget.CoordinatorLayout >
有人有什么想法吗?
最佳答案
如果您想要一个从底部自动显示的 View ,而用户无法拖动它,那么底部工作表不是正确的小部件。使用 ViewGroup(LinearLayout、RelativeLayout、...)并设置其 translationY,使其最初位于屏幕外。当您想要显示它时,将平移 Y 从该偏移量动画化回 0。
您需要做的是在屏幕底部绘制 View ,然后在扩充布局时,将其平移到屏幕外的高度。
假设你的 View 叫做“ View ”,在膨胀之后,你会这样做:
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
view.getViewTreeObserver().removeOnPreDrawListener(this);
view.setTranslationY(/* get the view top Y and translate to it becomes the bottom of the screen */);
return false;
}
});
然后当你想制作动画时,你可以:
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "translationY", /* translation value calculated above */, 0);
objectAnimator.setDuration(400);
objectAnimator.setInterpolator(new DecelerateInterpolator());
objectAnimator.start();
关于android - 防止用户在 BottomSheet 支持库 23.2 上拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35709446/