Reputation: 9
As my tilte, i have a expandable listview
, but setOnChildClick
event do not working, setOnGroupExpandListener
setOnGroupClickListener
being active. help me pls. thanks all !!!!
This is my code, fragment and adapter.
Fragment:
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
// }
}
}
My Adapter
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)
}
val itemsText = convertView!!.findViewById<TextView>(R.id.listItem)
itemsText.text = listItems
return convertView
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
}
Upvotes: -1
Views: 181
Reputation: 9
Solved, Im alredy refer this link, I have added android:descendantFocusability="blocksDescendants"
in Child XML and it really the solution.
Link:
Android ExpandableListView and onChildClick not working
Upvotes: 0