Home > Blockchain >  How can I use Expandablelistview in fragment in Kotlin?
How can I use Expandablelistview in fragment in Kotlin?

Time:08-21

My code for Expandablelistview works with Activity but when I want to use it inside Fragment the code doesn't work. I am sharing my Fragment and Adapter codes. I'll be happy if you can help me. I'm not very good at coding, it would be nice if you could explain it simply.

Fragment:

    @Suppress("UNREACHABLE_CODE")
class AnaSayfaFragment : Fragment() {

    private lateinit var listViewAdapter: ExpandableListViewAdapter
    private lateinit var chapterList : List<String>
    private lateinit var topicList : HashMap<String,List<String>>
    private var _binding: FragmentAnaSayfaBinding? = null
    private val binding get() = _binding!!


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_chat, container, false)
        _binding = FragmentAnaSayfaBinding.inflate(inflater, container, false)
        val view = binding.root
        return view
        showList()


        listViewAdapter= ExpandableListViewAdapter(this,chapterList,topicList)
        binding.eListView.setAdapter(listViewAdapter)


    }



    private fun showList() {

        chapterList = ArrayList()
        topicList = HashMap()
        (chapterList as ArrayList<String>).add("Sayı Alıştırmaları")
        (chapterList as ArrayList<String>).add("Toplama İşlemi")
        (chapterList as ArrayList<String>).add("Çıkarma İşlemi")
        (chapterList as ArrayList<String>).add("Ölçme")

        val topic1: MutableList<String> = ArrayList()
        topic1.add("Ritmik Saymalar")
        topic1.add("Önceki Sonraki Sayılar")
        topic1.add("Aradaki Sayılar")
        topic1.add("Sayı Kadar Şekil")
        topic1.add("Sayı Karşılaştırma")
        topic1.add("Bul Boya")
        topic1.add("Sayı Okuma")
        topic1.add("Sayı Yazma")

        val topic2: MutableList<String> = ArrayList()
        topic2.add("Toplama İşlemi (Rakamlarla)")
        topic2.add("Toplama İşlemi (10'a Kadar)")
        topic2.add("Toplama İşlemi (2 Basamak - 1 Basamak)")
        topic2.add("Toplama İşlemi (20 İçinde)")
        topic2.add("Toplamı 10 Olan Sayılar")
        topic2.add("10 ile Toplama")
        topic2.add("Verilmeyenli Toplama")

        val topic3: MutableList<String> = ArrayList()
        topic3.add("Çıkarma İşlemi (Rakamlarla)")
        topic3.add("Çıkarma İşlemi (Seviye 2)")
        topic3.add("Çıkarma İşlemi (Seviye 3)")

        val topic4: MutableList<String> = ArrayList()
        topic4.add("Saat Okuma")
        topic4.add("Saat Çizme")

        topicList[chapterList[0]] = topic1
        topicList[chapterList[1]] = topic2
        topicList[chapterList[2]] = topic3
        topicList[chapterList[3]] = topic4



    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}

ExpandableAdapter:

    class ExpandableListViewAdapter internal constructor(private val context: Context, private val chapterList: List<String>, private val topicList: HashMap<String,List<String>>) :
    BaseExpandableListAdapter() {
    override fun getGroupCount(): Int {
        return chapterList.size
    }

    override fun getChildrenCount(p0: Int): Int {
        return this.topicList[this.chapterList[p0]]!!.size
    }

    override fun getGroup(p0: Int): Any {
        return chapterList[p0]
    }

    override fun getChild(p0: Int, p1: Int): Any {
        return this.topicList[this.chapterList[p0]]!![p1]
    }

    override fun getGroupId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getChildId(p0: Int, p1: Int): Long {
        return p1.toLong()
    }

    override fun hasStableIds(): Boolean {
        return false
    }

    override fun getGroupView(p0: Int, p1: Boolean, convertView: View?, p3: ViewGroup?): View {
        var convertView = convertView
        val chapterTitle= getGroup(p0) as String

        if (convertView == null){

            val inflater =  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(R.layout.chapter_list, null)
        }
        val chapterTv = convertView!!.findViewById<TextView>(R.id.chapter_tv)
        chapterTv.setText(chapterTitle)

        return convertView
    }

    override fun getChildView(p0: Int, p1: Int, p2: Boolean, convertView: View?, p4: ViewGroup?): View {
        var convertView = convertView
        val topicTitle= getChild(p0, p1) as String


        if (convertView == null){
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(R.layout.topics_list, null)
        }
        val topicTv = convertView!!.findViewById<TextView>(R.id.topics_tv)
        topicTv.setText(topicTitle)

        convertView.setOnClickListener {
            val chapterTv = convertView.findViewById<TextView>(R.id.chapter_tv)
            // if (getChild(p0, p1) =="Ritmik Saymalar"){ }

            when(getChild(p0,p1)){

                "Ritmik Saymalar" -> {
                    val intent = Intent(context, AnaSayfaFragment::class.java)
                    context.startActivity(intent)
                }




            }



        }

        return convertView
    }

    override fun isChildSelectable(p0: Int, p1: Int): Boolean {
        return true
    }
}

I guess the problem is with listViewAdapter but I can't find how to solve it.

CodePudding user response:

I solved the problem. After defining the adapter from onActivityCreated, the problem was solved.

  • Related