rendering

You can only do two folds.

Brief introduction to properties

Divider: the style that divides the parent layout. ChildDivider: the style that divides the child layout. Divider Height: used to set the height of the divider. Don't set the default is no icon groupIndicator: setting the ICONS shown before the parent layout indicatorLeft/indicatorStart: the default icon from the left side of the distance indicatorRight/indicatorEnd: the default icon from the right side of the distanceCopy the code

The use of,

  1. The main interface
<? xml version="1.0" encoding="utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ExpandableListView
        android:id="@+id/elvExpandList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:groupIndicator="@null"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code
  1. activity
class ExpandActivity: AppCompatActivity(R.layout.activity_expand) {
    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)

        initData()

    }

    lateinit var adapter: ExpandAdapter
    private fun initData(a) {
        val list = ArrayList<Contact>()
        var contact: Contact
        for (i in 0.8.){
            contact = Contact("Friend$i",
                arrayListOf(
                    Person("Zhang"."123658956987"),
                    Person("Bill"."123658956987"),
                    Person("Fifty"."123658956987")
                ))
            list.add(contact)
        }
        adapter = ExpandAdapter(this,list)
        elvExpandList.setAdapter(adapter)
        // Default to expand the number
        elvExpandList.expandGroup(3)
        // Sublayout click
        elvExpandList.setOnChildClickListener { parent, v, groupPosition, childPosition, id ->
            Toast.makeText(this."${list[groupPosition].list[childPosition].name}", Toast.LENGTH_SHORT).show()
            true}}}Copy the code
  1. bean
data class Contact(
    vartitle: String? .var list: List<Person>
)

data class Person(varname: String? .var phone: String?)
Copy the code
  1. adpter
class ExpandAdapter(val context: Context, private val list: List<Contact>) :
    BaseExpandableListAdapter() {

    var layoutInflater: LayoutInflater = LayoutInflater.from(context)

    override fun getGroupCount(a): Int = list.size

    override fun getChildrenCount(groupPosition: Int): Int = list[groupPosition].list.size

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

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return list[groupPosition].list[childPosition]
    }

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

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

    override fun hasStableIds(a): Boolean = false

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View? , parent:ViewGroup?).: View {
        var convertView = convertView
        val holder: GroupViewHolder
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_expand_group, parent, false)
            holder = GroupViewHolder(convertView)
            convertView.tag = holder
        } else {
            holder = convertView.tag as GroupViewHolder
        }
        val groupData = list[groupPosition]
        holder.tvTitle.text = groupData.title
        holder.ivArrow.setImageResource(if (isExpanded) R.drawable.arrow_down else R.drawable.arrow_right)
        return convertView!!
    }

    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View? , parent:ViewGroup?).: View {
        var convertView = convertView
        val holder: ChildViewHolder
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_expand_child, parent, false)
            holder = ChildViewHolder(convertView!!)
            convertView.tag = holder
        } else {
            holder = convertView.tag as ChildViewHolder
        }
        val childData = list[groupPosition].list[childPosition]
        holder.tvName.text = childData.name
        holder.tvMoblie.text = childData.phone
        return convertView
    }

    Return true if you want to set the click event for the child
    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = true

/* internal class TestViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var tvTitle: TextView = itemView.findViewById(R.id.tvItemExpandGroupTitle) }*/

    class GroupViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvTitle: TextView = itemView.findViewById<TextView>(R.id.tvItemExpandGroupTitle)
        var ivArrow: ImageView = itemView.findViewById<ImageView>(R.id.ivExpandGroupArrow)
    }

    class ChildViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvName = itemView.findViewById<TextView>(R.id.tvItemExpandChildName)
        var tvMoblie = itemView.findViewById<TextView>(R.id.tvItemExpandChildPhone)
    }
}
Copy the code
  1. Outer Item layout
<? xml version="1.0" encoding="utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:paddingHorizontal="15dp">

    <TextView
        android:id="@+id/tvItemExpandGroupTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Group"
        android:layout_centerVertical="true"/>

    <ImageView
        android:id="@+id/ivExpandGroupArrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_right"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>
Copy the code
  1. The inner item layout
<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@color/colorAccent">

    <TextView
        android:id="@+id/tvItemExpandChildName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="50dp"
        android:textColor="@color/white"/>

    <TextView
        android:id="@+id/tvItemExpandChildPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="20dp"
        android:textColor="@color/white"/>
</LinearLayout>
Copy the code

reference

ExpandableListView

ExpandableListView ExpandableListView

ExpanableListView