The article directories

    • ExpandableListView use
    • BaseExpandableListAdapter
    • Show secondary lists

ExpandableListView use

        <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>
Copy the code

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 * @ the Describe secondary list 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} / / return the first level List length override fun getGroupCount () : Int {return diagnosisReportDetailFirst. Size} / / () function returns the length of the specified groupPosition secondary List override fun getChildrenCount(groupPosition: Int): Int {return diagnosisReportDetailSecond. Get (groupPosition). The size} / / return level 1 in the List of content override fun getGroup (groupPosition: Int) : Any {return diagnosisReportDetailFirst. Get (groupPosition)} / / return the contents of a secondary List override fun getChild (groupPosition: Int, childPosition: Int): Any {return diagnosisReportDetailSecond. Get (groupPosition) get (childPosition)} / / returns the primary View id that id only override fun getGroupId(groupPosition: Int): Long {return groupPosition.tolong ()} override fun getChildId(groupPosition: Int, childPosition: Int): Long {return (groupPosition+childPosition).tolong ()} override fun hasStableIds(): Boolean {return true} 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} incredibly" if (isExpanded) { groupViewHolder.iv_super?.setImageResource(R.drawable.icon_diagnosis_up) }else{ groupViewHolder.iv_super?.setImageResource(R.drawable.icon_diagnosis_down) } return mconvertView!! 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?. 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): 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 } } }Copy the code

Show secondary lists

private fun initListView() { diagnosisAdapter = DiagnosisAdapter(diagnosisReportDetailFirst,diagnosisReportDetailSecond) Exp_list_view. setAdapter(diagnosisAdapter) And close the other grouping exp_list_view. SetOnGroupExpandListener {for (groupPosition in diagnosisReportDetailFirst. Indices) {if (groupPosition ! It) = {exp_list_view. CollapseGroup (groupPosition)}}} / /. Click a group to monitor / / exp_list_view setOnGroupClickListener {parent: ExpandableListView? , v: View? , groupPosition: Int, id: MakeText (this," click $groupPosition", toast.length_short).show() // logE(" click $groupPosition") // toast.maketext (this," click $groupPosition", toast.length_short).show() // logE(" click $groupPosition") // True/false "block level list click / /} / / a group of child View click event exp_list_view. SetOnChildClickListener {parent, v, groupPosition, ChildPosition, id -> logE(" $groupPosition") false}}Copy the code