繁体   English   中英

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

[英]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 中的translationX属性和animate()函数实现了相同的效果。 这些的组合给出了我正在寻找的相同结果。

在 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM