Я пытаюсь переместить данные с 2 ArrayList
с (объявленных внутри Fragment
) на AlertDialog
, содержащий несколько вкладок.Названия вкладок (myDialogTitles
) отображаются правильно, но по какой-то причине содержимое под каждой вкладкой не отображается, и кажется, что всегда отображаются данные только из первого элемента в ArrayList
(myDialogDescriptions
).Что нужно изменить, чтобы содержимое каждой вкладки отображало правильный текст соответственно?
Ожидаемый результат
Вкладка A -> Описание A
Вкладка B -> Описание B
Вкладка C -> Описание C
Токовый выход
Вкладка A -> Описание A
Вкладка B -> Описание A
Вкладка C -> Описание A
Класс фрагмента
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_rv, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)
mRecyclerView.layoutManager = LinearLayoutManager(activity)
val myList = ArrayList<Item>()
val myDialogTitles = ArrayList<String>()
myDialogTitles.add("Tab A")
myDialogTitles.add("Tab B")
myDialogTitles.add("Tab C")
val myDialogDescriptions = ArrayList<String>()
myDialogDescriptions.add("Description A")
myDialogDescriptions.add("Description B")
myDialogDescriptions.add("Description C")
val mAdapter = MyAdapter(myList, childFragmentManager)
mRecyclerView.adapter = mAdapter
super.onActivityCreated(savedInstanceState)
}
}
Класс диалога
class TabbedDialog(private val facilitiesList: ArrayList<Facility>,
private val myDialogTitles: ArrayList<String>,
private val myDialogDescriptions: ArrayList<String>) : DialogFragment() {
lateinit var customView: View
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return customView
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val b = AlertDialog.Builder(activity)
.setIconAttribute(R.attr.imgNight)
.setTitle(facilitiesList[id].textFacilityADialogTitle)
.setPositiveButton(getString(android.R.string.ok)) { dialog, _ -> dialog.dismiss() }
customView = activity!!.layoutInflater.inflate(R.layout.fragment_dialog, null)
val tabLayout = customView.findViewById<TabLayout>(R.id.mTabLayout)
val viewPager = customView.findViewById<ViewPager>(R.id.mViewPager)
val adapter = MyViewPagerAdapter(childFragmentManager)
for (item in myDialogTitles) {
adapter.addFragment(item, FragmentDialogContent.createInstance(myDialogDescriptions[id]))
}
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
b.setView(customView)
return b.create()
}
}
Просмотр класса адаптера пейджера
class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
private val titleList : MutableList<String> = ArrayList()
private val fragmentList : MutableList<Fragment> = ArrayList()
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
fun addFragment(title: String, fragment: Fragment){
titleList.add(title)
fragmentList.add(fragment)
}
override fun getPageTitle(position: Int): CharSequence? {
return titleList[position]
}
}
Класс данных элемента
data class Item (val myDialogTitle: String, val myDialogDescription: String)