ExpandableListView使用
<ExpandableListView
android:id="@+id/exp_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ve_diagnosis_result_bg"
android:padding="20dp"
android:groupIndicator="@null"
android:divider="@color/white"
android:dividerHeight="0dp"
android:scrollbars="none"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp">
</ExpandableListView>
BaseExpandableListAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.TextView
import com.beans.base.utils.log.logE
import com.beans.vehicle.R
import com.beans.vehicle.bean.DiagnosisReportDetail
import kotlinx.android.synthetic.main.ve_diagnosis_item_sub.view.*
import kotlinx.android.synthetic.main.ve_diagnosis_item_super.view.*
/**
* @Author yangtianfu
* @CreateTime 2020/5/15 17:43
* @Describe 二級列表adapter
*/
class DiagnosisAdapter : BaseExpandableListAdapter {
var diagnosisReportDetailFirst: MutableList<DiagnosisReportDetail> = mutableListOf()
var diagnosisReportDetailSecond: MutableList<MutableList<DiagnosisReportDetail>> = mutableListOf()
lateinit var groupViewHolder:GroupViewHolder
lateinit var childViewHolder:ChildViewHolder
constructor(
diagnosisReportDetailFirst: MutableList<DiagnosisReportDetail>,
diagnosisReportDetailSecond: MutableList<MutableList<DiagnosisReportDetail>>
) : super() {
this.diagnosisReportDetailFirst = diagnosisReportDetailFirst
this.diagnosisReportDetailSecond = diagnosisReportDetailSecond
}
//返回第一級List長度
override fun getGroupCount(): Int {
return diagnosisReportDetailFirst.size
}
//返回指定groupPosition的第二級List長度
override fun getChildrenCount(groupPosition: Int): Int {
return diagnosisReportDetailSecond.get(groupPosition).size
}
//返回一級List裏的內容
override fun getGroup(groupPosition: Int): Any {
return diagnosisReportDetailFirst.get(groupPosition)
}
//返回二級List的內容
override fun getChild(groupPosition: Int, childPosition: Int): Any {
return diagnosisReportDetailSecond.get(groupPosition).get(childPosition)
}
//返回一級View的id 保證id惟一
override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
//返回二級View的id 保證id惟一
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
return (groupPosition+childPosition).toLong()
}
//基礎數據進行更改時子ID和組ID是否穩定
override fun hasStableIds(): Boolean {
return true
}
// 返回一級父View
override fun getGroupView(
groupPosition: Int,
isExpanded: Boolean,
convertView: View?,
parent: ViewGroup?
): View {
var mconvertView = convertView
if (convertView == null){
mconvertView = LayoutInflater.from(parent?.context).inflate(R.layout.ve_diagnosis_item_super, parent,false)
groupViewHolder = GroupViewHolder(mconvertView)
mconvertView?.tag = groupViewHolder
}else{
groupViewHolder = mconvertView?.tag as GroupViewHolder
}
groupViewHolder.tv_name_sup?.text = diagnosisReportDetailFirst[groupPosition].sysN.toString()
groupViewHolder.tv_name_des?.text = "${diagnosisReportDetailSecond[groupPosition].size}項異常"
if (isExpanded){
groupViewHolder.iv_super?.setImageResource(R.drawable.icon_diagnosis_up)
}else{
groupViewHolder.iv_super?.setImageResource(R.drawable.icon_diagnosis_down)
}
return mconvertView!!
}
// 返回二級子View
override fun getChildView(
groupPosition: Int,
childPosition: Int,
isLastChild: Boolean,
convertView: View?,
parent: ViewGroup?
): View {
var mconvertView = convertView
if (convertView == null){
mconvertView = LayoutInflater.from(parent?.context).inflate(R.layout.ve_diagnosis_item_sub, parent,false)
childViewHolder = ChildViewHolder(mconvertView)
// 用mconvertView代替convertView,否則會有空指針異常
mconvertView?.tag = childViewHolder
}else{
childViewHolder = mconvertView?.tag as ChildViewHolder
}
childViewHolder.tv_name_sub?.text = diagnosisReportDetailSecond[groupPosition][childPosition].itemN
childViewHolder.iv_sub?.setImageResource(R.drawable.icon_diagnosis_right)
return mconvertView!!
}
//指定位置的子項是否可選
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
inner class GroupViewHolder(view:View) {
var tv_name_sup: TextView? = null
var tv_name_des: TextView? = null
var iv_super: ImageView? = null
init {
tv_name_sup = view.tv_name_sup
tv_name_des = view.tv_name_des
iv_super = view.iv_super
}
}
inner class ChildViewHolder(view:View){
var tv_name_sub: TextView? = null
var iv_sub: ImageView? = null
init {
tv_name_sub = view.tv_name_sub
iv_sub = view.iv_sub
}
}
}
展現二級列表
private fun initListView() {
diagnosisAdapter = DiagnosisAdapter(diagnosisReportDetailFirst,diagnosisReportDetailSecond)
exp_list_view.setAdapter(diagnosisAdapter)
//展開某個分組時,並關閉其餘分組
exp_list_view.setOnGroupExpandListener {
for (groupPosition in diagnosisReportDetailFirst.indices){
if (groupPosition != it){
exp_list_view.collapseGroup(groupPosition)
}
}
}
//點擊某個分組監聽
// exp_list_view.setOnGroupClickListener { parent: ExpandableListView?, v: View?, groupPosition: Int, id: Long ->
// Toast.makeText(this,"點擊一級列表$groupPosition",Toast.LENGTH_SHORT).show()
// logE("點擊一級列表item$groupPosition")
// false // true屏蔽一級列表點擊
// }
//某個分組中的子View點擊事件
exp_list_view.setOnChildClickListener { parent, v, groupPosition, childPosition, id ->
logE("點擊二級列表item$groupPosition")
false
}
}