我的Expandablelistview代码可以在Activity中使用,但是当我想在Fragment中使用它时,代码无法使用。我正在分享我的Fragment和Adapter代码。如果你能帮助我,我将非常高兴。我不太擅长编写代码,如果你能简单地解释一下,那就太好了。
片段:
@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
}
}
可扩展适配器:
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
}
}
我猜问题出在listViewAdapter上,但我找不到解决方法。
1条答案
按热度按时间v6ylcynt1#
我解决了这个问题,在从onActivityCreated定义适配器之后,问题就解决了。