我试图将数据从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)
答案 0 :(得分:0)
从{p>替换TabbedDialog
中的for循环
for (item in myDialogTitles) {
adapter.addFragment(item, FragmentDialogContent.createInstance(myDialogDescriptions[id]))
}
使用
myDialogTitles.forEachIndexed { index, item->
adapter.addFragment(item, FragmentDialogContent.createInstance(myDialogDescriptions[index]))
}
问题是您使用id
作为索引。