导航(显示?)以滑动片段

时间:2019-02-06 09:28:25

标签: java android layout

我想为我的滑动片段提供某种导航显示(我真的不知道如何更好地解释它),就像你看到here(我想要的是小点)。

我已经有滑动片段了,只是希望有一种指示器可以显示您在滑动片段时的实际位置。

感谢任何建议!

2 个答案:

答案 0 :(得分:0)

您可以通过广播组使用以下代码轻松添加寻呼机指示符

科特林

编写的代码
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" />

答案 1 :(得分:0)

因此,我找到了一种显示此片段的方法,借助Nielsh响应的帮助/启发,我实现了Java变体。首先将RadioGroup添加到您的MainActivity.xml

    <RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|bottom"
    android:orientation="horizontal"
    android:id="@+id/radio_group">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton"
        android:checked="true" />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton2"
        />
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioButton3" />
</RadioGroup>

初始化您的单选按钮组,然后将onPageListener添加到ViewPager并进行初始化

        (...)
        radioGroup = (RadioGroup)findViewById(R.id.radio_group);
        (...)
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {

        }

        @Override
        public void onPageSelected(int position) {
            page = position;
            switch (position){
                case 0:
                    radioGroup.check(R.id.radioButton);
                    break;
                case 1:
                    radioGroup.check(R.id.radioButton2);
                    break;
                case 2:
                    radioGroup.check(R.id.radioButton3);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    }); // viewpager on page change listener