Элемент ArrayList нежелательно повторяется в DialogFragment - PullRequest
0 голосов
/ 24 апреля 2019

Я пытаюсь показать различный контент для разных вкладок в моем DialogFragment, но по какой-то причине текст контента продолжает повторять первый элемент независимо от выбранной вкладки. Имена вкладок отображаются правильно, как и ожидалось. Я считаю, что проблема заключается в классе DialogFragment внутри цикла for вокруг createInstance, но я не знаю, что нужно изменить в этой области.

Ожидаемый результат

  • Вкладка A с Описание A
  • Вкладка B с Описание B
  • Вкладка C с Описание C

Текущий результат

  • Все вкладки показывают Описание A

enter image description here

enter image description here

enter image description here

Класс изделия

data class Item (val myDialogTitle: String,
                 val myDialogDescription: String)

Класс адаптера

class MyDialogAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
    private var mTitleCollection: MutableList<String> = ArrayList()
    private var mFragmentCollection: MutableList<Fragment> = ArrayList()

    fun addFragment(title: String, fragment: Fragment) {
        mTitleCollection.add(title)
        mFragmentCollection.add(fragment)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return mTitleCollection[position]
    }

    override fun getItem(position: Int): Fragment {
        return mFragmentCollection[position]
    }

    override fun getCount(): Int {
        return mFragmentCollection.size
    }
}

Класс DialogFragment

class MyDialog(val myList: ArrayList<Item>) : DialogFragment() {    
    private var mText = ""

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return customView
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val b = AlertDialog.Builder(activity)
                .setTitle("Dialog Title")
                .setPositiveButton(getString(android.R.string.ok)) { dialog, _ -> dialog.dismiss() }

        val adapter = MyDialogAdapter(childFragmentManager)
        for (item in myListTitles) {
            adapter.addFragment(id, MyDialog.createInstance(myListDescriptions[id]))
        }

        b.setView(customView)
        return b.create()
    }

    companion object {
        fun createInstance(txt: String): MyDialog {
            val fragment = MyDialog()
            fragment.mText = txt
            return fragment
        }
    }
}

Класс фрагмента

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 myListTitles = ArrayList<String>()
        myListTitles.add("Tab A")
        myListTitles.add("Tab B")
        myListTitles.add("Tab C")

        val myListDescriptions = ArrayList<String>()
        myListDescriptions.add("Description A")
        myListDescriptions.add("Description B")
        myListDescriptions.add("Description C")


        val mAdapter = MyAdapter(myList, childFragmentManager)

        mRecyclerView.adapter = mAdapter

        super.onActivityCreated(savedInstanceState)
    }
}
...