底部工作表的动画布局更改

新手上路,请多包涵

在我的应用程序中,我使用了一个底部工作表(来自支持库),效果很好。现在,我想在向上拖动工作表时为布局更改设置动画。为此,我创建了 BottomSheetCallback 的子类(这通常是 Fragment 的内部类,因此并非所有在此 calss 中使用的对象都在此处初始化):

 public class MyBehavior extends BottomSheetBehavior.BottomSheetCallback {

    Transition transition;
    float lastOffset = 0;
    Scene scene;

    public PlayerBehavior() {
        TransitionInflater inflater = TransitionInflater.from(getContext());
        transition = inflater.inflateTransition(R.transition.player);
        //transition.setDuration(300);

        scene = fullLayout;

        transition.setInterpolator(new Interpolator() {
            @Override
            public float getInterpolation(float v) {
                return lastOffset;
            }
        });
    }

    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if(newState == BottomSheetBehavior.STATE_DRAGGING) {
            TransitionManager.go(scene, transition);
        }
    }

    @Override
    public void onSlide(View bottomSheet, final float slideOffset) {
        scene = (slideOffset > lastOffset) ? smallLayout : fullLayout;
        lastOffset = slideOffset;
    }
}

如您所见,我还从不同的布局文件创建了两个 Scene 和一个自定义 Transition 以使用 TransitionManager 在场景之间制作动画。 My problem is that the Transition should be based on the slideOffset parameter (in range of 0-1) but the TransitionManager uses the Animation 后台类,在 Android 中通常是基于时间的。

我尝试创建自定义 Intapolator,但这无法正常工作。那么我如何创建一个基于外部变量而不是时间的 Transition 呢?

原文由 Cilenco 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 178
1 个回答

根据您的描述,我认为您正在尝试实现类似 google maps bottom sheet 的行为。布局随着 bottomsheet 被向上拖动而改变。

如果这是您想要实现的目标,那么您不需要强制执行自定义动画,因为 bottomsheetdialog 本身在合并到父 Coordinator Layout 中时具有这些动画行为。

这是我如何实现相同行为的示例代码。当 bottomsheet 被拖动到全屏大小时,它也会使 FloatingActionButton 不可见:

  1. 创建一个要在主布局中使用的 bottomsheetdialog
    public class CustomBottomDialog extends BottomSheetDialogFragment {

   String mSomeName;
   @Override
   public void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // if some arguments are passed from the calling activity
       mSomeName = getArguments().getString("some_name");


   }

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View bottomSheet = inflater.inflate(R.layout.bottomsheet_layout, container, false);
        // initialise your bottomsheet_layout items here
       TextView tvName = bottomSheet.findViewById(R.id.display_name);
       tvName.setText(mSomeName);
       tvName.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
              // do something here
               ((MainActivity)getActivity()).doSomething();
           }
       });

       return bottomSheet;
   }
   }

  1. bottomsheet_layout:
    <android.support.design.widget.CoordinatorLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <android.support.design.widget.FloatingActionButton
   android:id="@+id/nav"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/navigation_tilt_grey"
   app:backgroundTint="@color/colorAccent"
   app:elevation="3dp"
   app:fabSize="normal"
   android:layout_marginEnd="@dimen/activity_horizontal_margin"
   app:layout_anchor="@+id/live_dash"
   app:layout_anchorGravity="top|right" />

   <!--BottomSheet-->

   <android.support.v4.widget.NestedScrollView
   android:id="@+id/live_dash"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#F3F3F3"
   android:clipToPadding="true"
   app:layout_behavior="android.support.design.widget.BottomSheetBe
   havior"
   tools:layout_editor_absoluteY="150dp">

   <!--Include your items here, the height of all items combined
   will take the main screen layout size with animation-->

   </android.support.v4.widget.NestedScrollView>

   </android.support.design.widget.CoordinatorLayout>

  1. 从您的活动中调用此 BottomSheet:
    public void notifyBottomSheet(String somename){

   BottomSheetDialogFragment customDialogFragment = new CustomBottomDialog();
   Bundle args = new Bundle();
   args.putString("some_name", somename);
   customDialogFragment.setArguments(args);
   customDialogFragment.show(getSupportFragmentManager(), customDialogFragment.getTag());
   customDialogFragment.setCancelable(false); // if you don't wish to hide
   }

希望这可以解决您要实现的目标。

原文由 Prashant Kumar 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏