Kotlin中Fragment内ExpandableListView失效问题求助
问题解决:Fragment中ExpandableListView无法工作的修复方案
核心问题梳理
你的代码里有3个关键错误导致ExpandableListView无法正常工作:
onCreateView方法中return view语句写在最前面,导致后面的数据初始化、Adapter设置代码完全没执行- 给Adapter传Context时用了
this(Fragment实例),Fragment不能直接作为Adapter的Context使用 - 子项点击事件里试图用Intent启动Fragment,这是错误的——Fragment不能通过Intent启动,必须用FragmentManager切换
修复步骤1:修正Fragment代码
把return view移到最后,确保所有初始化代码都能执行,同时给Adapter传正确的Context:
@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? { _binding = FragmentAnaSayfaBinding.inflate(inflater, container, false) val view = binding.root // 先初始化列表数据 showList() // 传Activity的Context给Adapter(requireActivity()获取当前Fragment依附的Activity) listViewAdapter = ExpandableListViewAdapter(requireActivity(), chapterList, topicList) binding.eListView.setAdapter(listViewAdapter) // 最后返回视图,这行必须放在所有初始化代码之后 return view } 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 } }
修复步骤2:修正Adapter的点击事件
Fragment必须通过FragmentManager切换,不能用Intent。修改Adapter的getChildView方法:
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 { when(topicTitle) { "Ritmik Saymalar" -> { // 切换Fragment的正确方式:通过Activity的FragmentManager if (context is AppCompatActivity) { val activity = context as AppCompatActivity val transaction = activity.supportFragmentManager.beginTransaction() // 替换成你要跳转的目标Fragment,比如RitmikSaymalarFragment() transaction.replace(R.id.fragment_container, RitmikSaymalarFragment()) transaction.addToBackStack(null) // 可选:加入返回栈,让用户能返回上一个Fragment transaction.commit() } } // 其他子项的点击逻辑可以在这里继续添加 } } return convertView }
关键说明
requireActivity():Fragment必须依附Activity才能运行,这个方法能安全获取当前Fragment所在的Activity,作为Adapter的Context- 代码顺序:
return view必须放在所有初始化代码之后,否则后面的代码永远不会执行(这是最致命的错误) - Fragment切换:只能通过
FragmentManager的事务操作,Intent是用来启动Activity的,不能用来启动Fragment
内容的提问来源于stack exchange,提问作者Ahmet Akar
相关产品推荐
相关产品推荐

