java - 滑动片段的导航(显示?)
问题描述
我想为我的滑动片段提供某种导航显示(我真的不知道如何更好地解释这一点)就像你在这里看到的那样(我想要的就是下面的小点)。
我已经让滑动片段工作了,只是希望有某种指示器来显示您在滑过它们时的实际位置。
感谢您的任何建议!
解决方案
您可以通过无线电组使用以下代码轻松添加寻呼机指示器
用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" />
推荐阅读
- javascript - 如何通过用户角色保护路由?角
- java - 如何放心地发送请求中的复杂正文?
- java - 为什么 GregorianCalendar 设置方法会给出轻微且不一致的错误时间?
- javascript - 有没有办法更新/刷新 agm-map 组件?
- c - Strtok 奇怪的行为
- elasticsearch - 弹性搜索和产品命名法:连字符和空格
- opengl-es - 在需要的地方有条件地包括 GLES2
- git - 为什么 Azure Devops 突然在公共项目 Git 存储库上请求登录名和密码?
- python - 如何提取一组字典中的第一项?
- python - discord ASCII bot python字符的长度不一样,所以看起来很糟糕