Yêu cầu thg 1 9, 2023 8:20 SA 42 0 0
  • 42 0 0
0

[KOTLIN] Expandable.setOnChildClickListener không hoạt động !!

Chia sẻ
  • 42 0 0

đây là code của tôi, khi tôi sử dụng <ExpandableListview>, hàm setOnChildClickListener không hoạt động, các hàm setOnGroupExpandListener, Collap vẫn hoạt động bình thường, dưới đây là code của tôi, nhờ mọi người giúp đỡ, cảm ơn tất cả mọi người !!

FRAGMENT

package org.readium.r2.testapp.discover

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import org.readium.r2.testapp.R
import org.readium.r2.testapp.databinding.FragmentFilterDiscoverBinding

class DiscoverFilterFragment:Fragment() {

    private lateinit var binding: FragmentFilterDiscoverBinding
    private lateinit var filterAdapter : ExpandableListAdapter
    private lateinit var titleList : List<String>
    private lateinit var itemsList : HashMap<String, List<String>>
    private val viewModel: DiscoverViewModel by activityViewModels()
    var searchText : String = ""
    var authorText : String = ""
    var dateIssueText : String = ""

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        binding = FragmentFilterDiscoverBinding.inflate(inflater, container, false)

        getDataFromApi(searchText, authorText + ",equals", dateIssueText + ",equals")
        onItemsClickEvent()
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

    private fun getDataFromApi(searchText : String, authorText : String, dateIssueText : String) {
       titleList = ArrayList()
       itemsList = HashMap()

        viewModel.discoverFilter(searchText, authorText, dateIssueText)
        viewModel.getFilterObserverableItem().observe(viewLifecycleOwner){
            it?.let { it ->
                titleList = it.keys.toList()
                //Log.d("Title List","${titleList}")
                itemsList = it
                //Log.d("Title List","${itemsList}")
                filterAdapter = DiscoverFilterAdapter(requireContext(), titleList, itemsList)
                binding.expFilter.setAdapter(filterAdapter)
                (filterAdapter as DiscoverFilterAdapter).notifyDataSetChanged()
            }
        }

    }

    private fun onItemsClickEvent() {
        val expandableListView = binding.expFilter
//        expandableListView.setOnGroupExpandListener {
//            Toast.makeText(
//                activity,
//                "Open"
//                ,Toast.LENGTH_SHORT).show()
//        }
        expandableListView.setOnChildClickListener {
                parent, v, groupPosition, childPosition, id ->
            Toast.makeText(activity, "Clicked: ", Toast.LENGTH_SHORT).show()
            false
        }

//        expandableListView.setOnChildClickListener { parent, v, groupPosition, childPosition, id ->
//            Log.d("Items Clicked","")
//            Toast.makeText(
//                activity,
//                titleList[groupPosition]
//                        + " : "
//                        + itemsList[titleList[groupPosition]]!![childPosition],
//                Toast.LENGTH_SHORT).show()
//            false
//        }
    }
}

ADAPTER

package org.readium.r2.testapp.discover

import android.app.Application
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.CheckBox
import android.widget.ExpandableListView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.*
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.lifecycle.MutableLiveData
import androidx.navigation.Navigation
import com.google.gson.Gson
import com.squareup.picasso.Picasso
import org.readium.r2.testapp.R
import org.readium.r2.testapp.databinding.DiscoverFilterItemBinding
import org.readium.r2.testapp.databinding.DiscoverFilterListBinding
import org.readium.r2.testapp.databinding.ItemRecycleDiscoverBinding
import org.readium.r2.testapp.utils.singleClick

class DiscoverFilterAdapter internal constructor(
    private val context: Context,
    private val titleList: List<String>,
    private val itemsList: HashMap<String, List<String>>) : BaseExpandableListAdapter() {

    private var authorTextLive : MutableLiveData<String> = MutableLiveData()

    fun getAuthorTextObserverable() : MutableLiveData<String> {
        return authorTextLive
    }

    override fun getGroupCount(): Int {
        return titleList.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return this.itemsList[this.titleList[groupPosition]]!!.size
    }

    override fun getGroup(groupPosition: Int): Any {
        return titleList[groupPosition]

    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return this.itemsList[this.titleList[groupPosition]]!![childPosition]
    }

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

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()

    }

    override fun hasStableIds(): Boolean {
        return false
    }

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        var convertView = convertView
        val listTitle = getGroup(groupPosition) as String
        if (convertView == null) {
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(R.layout.discover_filter_list, null)
        }
        val titleText = convertView!!.findViewById<TextView>(R.id.listTitle)
        titleText.text = listTitle
        return convertView

    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {

        var convertView = convertView
        val listItems = getChild(groupPosition, childPosition) as String
        if (convertView == null) {
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(R.layout.discover_filter_item, null)
        }
        var title : String
        val itemsText = convertView!!.findViewById<TextView>(R.id.listItem)
        itemsText.text = listItems
        itemsText.setOnClickListener {
            title = titleList.get(groupPosition)
            Toast.makeText(context, "${listItems}, ${title} Clicked", Toast.LENGTH_SHORT).show()

            val iconCheckBox = convertView.findViewById<CheckBox>(R.id.itemsCheckbox)
            iconCheckBox.isChecked = !iconCheckBox.isChecked
//            when(groupPosition){
//                0-> {
//                    authorTextLive.postValue(listItems)
//                }
//            }
        }
        return convertView

    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }


}
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí