📜  导航底部引导程序从左到右滑动 (1)

📅  最后修改于: 2023-12-03 15:25:12.791000             🧑  作者: Mango

导航底部引导程序从左到右滑动

介绍

这个引导程序可以在底部导航栏的下方显示,由左到右滑动显示各个页面的标题或图标,让用户对页面进行选择。

实现
布局

使用线性布局,将所有需要显示的页面的标题或图标放在RelativeLayout中,将RelativeLayout放在ScrollView中,最后将ScrollView放在底部导航栏下方。

<LinearLayout
        android:layout_width="match_parent"
        ...
        android:orientation="vertical">

        <!-- 底部导航栏 -->

        ...

        <!-- 引导程序 -->

        <ScrollView
            android:layout_width="match_parent"
            ...
            >

            <RelativeLayout
                android:layout_width="wrap_content"
                ...
                >

                <!-- 页面一的标题或图标 -->

                ...

                <!-- 页面二的标题或图标 -->

                ...

                <!-- 页面三的标题或图标 -->

                ...

                ...

            </RelativeLayout>

        </ScrollView>

</LinearLayout>
动画

使用属性动画,将RelativeLayout从左到右移动,当移动到最右侧时,重置RelativeLayout的位置到最左侧,保持循环。

private fun startScrollAnimation() {
        val scrollX = binding.scrollView.scrollX
        val width = binding.rlNavigationContent.width
        val screenWidth = resources.displayMetrics.widthPixels
        val animator = ObjectAnimator.ofInt(binding.scrollView, "scrollX", scrollX, scrollX + width - screenWidth)
        animator.duration = ANIMATION_DURATION
        animator.interpolator = LinearInterpolator()
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                binding.scrollView.scrollTo(0, 0)
                startScrollAnimation()
            }
        })
        animator.start()
    }
调用

在Activity或Fragment中的onCreate()函数中调用startScrollAnimation()函数启动动画。

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    startScrollAnimation()
}
总结

通过上述介绍,我们了解了一个导航底部引导程序从左到右滑动的实现方法。在实现过程中,我们使用了线性布局、ScrollView、RelativeLayout以及属性动画等组件,让用户能够更方便的选择所需页面。