Как зациклить FragmentStateAdapter
var imageLoader: ImageLoader? = null
var CURRENT_PAGE = 1
var NUM_COLLECTIONS: Int? = null
var NUM_PAGES: Int? = null
class Main : FragmentActivity() {
//переменные...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
NUM_COLLECTIONS = Integer.valueOf(intent.getIntExtra("collection", 1))
if (NUM_COLLECTIONS?.toInt() == 1) {
NUM_PAGES =
Integer.valueOf(resources.getStringArray(R.array.collection_1_names).size)
}
mViewPager = findViewById(R.id.pager) as ViewPager2
mViewPager!!.adapter = ScreenSlidePagerAdapter(this)
//больше кода...
inner class ScreenSlidePagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
override fun createFragment(position: Int): Fragment {
val fragment = PageFragment()
val bundle = Bundle()
bundle.putInt("position", position)
bundle.putInt("collection", NUM_COLLECTIONS!!.toInt())
fragment.arguments = bundle
return fragment
}
override fun getItemCount(): Int = NUM_PAGES!!.toInt()
}
class PageFragment : Fragment() {
private var imageResource: Int? = null
private var textResource: Int? = null
override fun onCreateView(
layoutInflater: LayoutInflater,
viewGroup: ViewGroup?,
bundle: Bundle?
): View {
val viewGroup2 =
layoutInflater.inflate(R.layout.pager_fragment, viewGroup, false) as ViewGroup
val textView = viewGroup2.findViewById<View>(R.id.text) as TextView
val imageView = viewGroup2.findViewById<View>(R.id.image) as ImageView
val linearLayout = viewGroup2.findViewById<View>(R.id.textLayout) as LinearLayout
val arguments: Bundle? = getArguments()
val i: Int = arguments!!.getInt("position")
val i2:Int = arguments!!.getInt("collection")
if (i2 == 1) {
imageResource = Integer.valueOf(R.array.collection_1_pictures)
textResource = Integer.valueOf(R.array.collection_1_names)
}
//код класса Fragment
}
}
Суть в том, что в главном меню выбирается группа (коллекция), при входе в которую считываются ресурсы ( if (i2 == 1) {...). Как сделать чтобы картинки листались циклично? Весь проект есть здесь:
https://github.com/ZakirovAlmaz/MyFirstRepository
Ответы (1 шт):
Автор решения: Sergei Buvaka
→ Ссылка
Я вам предложу использовать ViewPager2. Он работает лучше и имеет всю плюшки RecyclerView. Вот тут есть статья о том как с ним работать.
Теперь о решении вашего вопроса:
Задаете размер вашего списка как Integer.MAX_VALUE
override fun getItemCount(): Int = Integer.MAX_VALUE
И в методе createFragment():
override fun createFragment(position: Int): Fragment {
val positionInList: Int = position % fragmentList.size()
return fragmentList.get(positionInList)
}
В целом никто не мешает провернуть такую же штуку и с вашим адаптером.