是否有可能从左向右更改片段的滑动? 我的片段从右向左滑动,但是我需要片段从左向右滑动。
Pager Adapter.kt
class pageradapter (fm: FragmentManager) : FragmentStatePagerAdapter(fm){
override fun getItem(position: Int): Fragment {
when(position){
0-> return fragment1()
1-> return fragment2()
else-> return fragment3()
}
}
override fun getCount(): Int {
return 3
}
fragment1.kt // 2和3具有相同的代码
class fragment1 : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1, container, false)
}
Main4Activity.kt
class Main4Activity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main4)
val adapter = pageradapter(supportFragmentManager)
val pager = findViewById<View>(R.id.pager) as ViewPager
pager.adapter = adapter
}
答案 0 :(得分:1)
您应该像下面那样反转Fragment
的位置:
在PagerAdapter.kt
class pageradapter (fm: FragmentManager) : FragmentStatePagerAdapter(fm){
override fun getItem(position: Int): Fragment {
when(rotatePosition(position)) {
0-> return fragment1()
1-> return fragment2()
else-> return fragment3()
}
}
override fun getCount(): Int {
return 3
}
private fun rotatePosition(position: Int): Int {
return (getCount() - 1) - position
}
}
在Main4Activity中:
class Main4Activity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main4)
val adapter = pageradapter(supportFragmentManager)
val pager = findViewById<View>(R.id.pager) as ViewPager
pager.adapter = adapter
// setting new start position
pager.setCurrentItem(adapter.rotatePosition(0), false);
}
答案 1 :(得分:0)