首页 > 解决方案 > 滑动片段的导航(显示?)

问题描述

我想为我的滑动片段提供某种导航显示(我真的不知道如何更好地解释这一点)就像你在这里看到的那样(我想要的就是下面的小点)。

我已经让滑动片段工作了,只是希望有某种指示器来显示您在滑过它们时的实际位置。

感谢您的任何建议!

标签: javaandroidlayout

解决方案


您可以通过无线电组使用以下代码轻松添加寻呼机指示器

用Kotlin编写的代码

import kotlinx.android.synthetic.main.activity_test.*
class TestActivity : AppCompatActivity(){ 
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

 customImageAdapter = MyCustomPagerAdapter(this@TestActivity, numbers)
        vpIntro.adapter = customImageAdapter
    /* Add Circle indicator for view the current page*/
        addRadioButtons(4, rgPagerIndicator)

vpIntro.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                currentPos = position
                rgPagerIndicator.check(position)
            }
        })
}

添加单选按钮功能

fun addRadioButtons(number: Int, rgPager: RadioGroup) {//, radioButtons: java.util.ArrayList<RadioButton>
//        radioButtons.clear()
        rgPager.removeAllViews()
        if (number > 1) {
            for (i in 0 until number) {
                val params_button: RadioGroup.LayoutParams
                val device_height = 1184

                val display = windowManager.defaultDisplay
                val size = Point()
                display.getSize(size)
                val width = size.x
                val height = size.y

                if (device_height == height) {
                    params_button = RadioGroup.LayoutParams(25, 25)
                    params_button.setMargins(7, 0, 7, 0)
                } else {
                    params_button = RadioGroup.LayoutParams(35, 35)
                    params_button.setMargins(10, 0, 10, 0)
                }

                val rdbtn = RadioButton(this@WelcomeActivity)
                rdbtn.isEnabled = false
                rdbtn.id = i
                rdbtn.layoutParams = params_button
                rdbtn.setBackgroundResource(R.drawable.bg_viewpager_indicator)
                rdbtn.setButtonDrawable(R.color.transparent)
                if (i == 0) {
                    rdbtn.isChecked = true
                }

                rgPager.addView(rdbtn)
            }
        }
    }

在 xml 中查看寻呼机下方的一个 Radio 组

<RadioGroup
        android:id="@+id/rgPagerIndicator"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="@dimen/margin_10"
        android:gravity="center"
        android:orientation="horizontal" />

推荐阅读