如何在单击按钮时将这些视图从右向左滑动?

How can i slide these views from right to left on button clicked?

我想在点击左箭头按钮时滑动一些视图。
[1]: https://i.stack.imgur.com/RbuMX.png

按下此按钮后,我想显示从屏幕末尾向左方向的视图幻灯片。
[2]: https://i.stack.imgur.com/pBGoa.png

我找到了 SliderDrawer,但它在几年前就被弃用了... 所以知道我怎样才能做到这一点。 作为参考,你可以看看我真正想要的MxPlayer滑动菜单。

我想使用导航抽屉布局,但考虑到一点点性能增强,我还认为如果我们从设备内存使用的角度来看它会增加一些负载。 我在 XML 和 animate() 函数中使用 translationX 属性 实现了相同的效果。这些组合给出了我正在寻找的相同结果。

在XML中:

<LinearLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="@dimen/_8sdp"
    android:translationX="@dimen/_138sdp"
    tools:translationX="0dp">

   <ImageView
       android:id="@+id/btn_action_menu"
       android:layout_width="@dimen/_28sdp"
       android:layout_height="@dimen/_28sdp"
       android:layout_gravity="center_vertical"
       android:background="@drawable/shape_action_menu_bg"
       android:contentDescription="@null"
       android:src="@drawable/arrow_left" />
    ........
</LinearLayout>

在Jave/Kotlin一侧

var rotation = 180f // For Arrow rotation effect from < to >
val translationX = binding.root.translationX

为了获得类似抽屉的效果,我在左箭头按钮上设置了点击监听器并执行了这样的代码。

binding.btnActionMenu.setOnClickListener {
    // This animation is for rotating arrow from < to > upon clicked
    it.animate().rotation(rotation).start()

    // This will drag or you can say open a menu from right to left with
    // 500ms delay effect.
    binding.actionMenuLayout.animate().also {
           .translationX(if(rotation != 0f) 0f else translationX)
           .duration = 500L
         }
         .animate.start()

    rotation = if(rotation == 0f) 180f else 0f
}

就是这样。 使我免于使用导航抽屉布局,也节省了一点内存。

菜单最初通过在 xml 中设置 translationX 来隐藏:- https://i.stack.imgur.com/RbuMX.png
延迟 500 毫秒从右向左拖动箭头时显示的菜单:- https://i.stack.imgur.com/pBGoa.png