ExpandableListView implements a simple secondary list

  • 2021-10-24 23:53:14
  • OfStack

This article example for everyone to share ExpandableListView to achieve a simple 2-level list of specific code, for your reference, the specific content is as follows

xml Create 1 ExpandableListView


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="${relativePackage}.${activityClass}" >
 <ExpandableListView 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/expandableListView"> 
 </ExpandableListView>
 
</RelativeLayout>

Level 1 List Layout for ExpandableListView


<?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="80dp"
 android:orientation="vertical" > 
 <TextView 
 android:id="@+id/one_name"
 android:layout_width="wrap_content"
 android:layout_height="40dp"
 android:layout_centerInParent="true"
 android:textSize="20sp"
 android:text=" I am 1 Level list "/>
 
</RelativeLayout>

Level 2 List Layout for ExpandableListView


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="60dp"
 android:orientation="horizontal" >
 
 <ImageView 
 android:id="@+id/img"
 android:layout_width="60dp"
 android:layout_height="60dp"
 android:src="@drawable/ic_launcher"/>
 <TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/tow_name"
 android:layout_marginTop="20dp"
 android:layout_marginLeft="20dp"
 android:text=" Laughing and joking "/>
</LinearLayout>

Java code


package com.example.expandablelistview;
 
import java.util.ArrayList;
import java.util.List;
 
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 private MainActivity.Madapder madapder;
 private ExpandableListView expandableListView;
 private List<String> allList;
 private List<List<Person>> list;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // Get control 
 expandableListView=(ExpandableListView) findViewById(R.id.expandableListView);
 // Initialization data 
 initData();
 // Custom adapter 
 madapder=new Madapder();
 expandableListView.setAdapter(madapder);
 }
 public void initData() {
 allList=new ArrayList<String>();
 allList.add(" List ① ");
 allList.add(" List 2 ");
 allList.add(" List ③ ");
 list=new ArrayList<List<Person>>();
 List<Person> list1=new ArrayList<Person>();
 list1.add(new Person("  Concubine Yu "));
 list1.add(new Person("  Zhen Ji "));
 list1.add(new Person("  Ali "));
 list1.add(new Person("  Fox "));
 List<Person> list2=new ArrayList<Person>();
 list2.add(new Person("  Li Bai "));
 list2.add(new Person("  Feathers "));
 list2.add(new Person("  In fact, in fact, the "));
 list2.add(new Person("  Cao Cao "));
 List<Person> list3=new ArrayList<Person>();
 list3.add(new Person("  Gong Sun Li "));
 list3.add(new Person("  Sun Shangxiang "));
 list3.add(new Person("  Di Renjie "));
 list3.add(new Person("  Cai Wenji "));
 list.add(list1);
 list.add(list2);
 list.add(list3);
 }
 class Madapder extends BaseExpandableListAdapter{
 
 @Override
 public int getGroupCount() {
 // TODO Auto-generated method stub
 return allList.size();
 }
 
 @Override
 public int getChildrenCount(int groupPosition) {
 // TODO Auto-generated method stub
 return list.get(groupPosition).size();
 }
 
 @Override
 public Object getGroup(int groupPosition) {
 // TODO Auto-generated method stub
 return allList.get(groupPosition);
 }
 
 @Override
 public Object getChild(int groupPosition, int childPosition) {
 // TODO Auto-generated method stub
 return list.get(groupPosition).get(childPosition);
 }
 
 @Override
 public long getGroupId(int groupPosition) {
 // TODO Auto-generated method stub
 return groupPosition;
 }
 
 @Override
 public long getChildId(int groupPosition, int childPosition) {
 // TODO Auto-generated method stub
 return childPosition;
 }
 
 @Override
 public boolean hasStableIds() {
 // TODO Auto-generated method stub
 return true;
 }
 
 @Override
 public View getGroupView(int groupPosition, boolean isExpanded,
 View convertView, ViewGroup parent) {
 GroupView groupView;
 if(convertView==null){
 groupView=new GroupView();
 // Get 1 Layout of level list 
 convertView=View.inflate(MainActivity.this,R.layout.item_1, null);
 // Reusable control 
 groupView.name=(TextView) convertView.findViewById(R.id.one_name);
 // Binding 
 convertView.setTag(groupView);
 }else {
 groupView = (GroupView) convertView.getTag();
 }
 // Set a value for the control 
 groupView.name.setText(allList.get(groupPosition));
 return convertView;
 }
 @Override
 public View getChildView(int groupPosition, int childPosition,
 boolean isLastChild, View convertView, ViewGroup parent) {
 ViewHolder Holder;
 if(convertView==null){
 Holder=new ViewHolder();
 // Get 2 Layout of level list 
 convertView=View.inflate(MainActivity.this,R.layout.item_2, null);
 // Reusable control 
 Holder.text_name=(TextView) convertView.findViewById(R.id.tow_name);
 // Binding 
 convertView.setTag(Holder);
 }else {
 Holder = (ViewHolder) convertView.getTag();
 }
 // Set a value for the control 
 Holder.text_name.setText(list.get(groupPosition).get(childPosition).getName());
 return convertView;
 }
 
 @Override
 public boolean isChildSelectable(int groupPosition, int childPosition) {
 // TODO Auto-generated method stub
 return true;
 } 
 }
 class ViewHolder{
 TextView text_name;
 }
 class GroupView{
 TextView name;
 }
}

Java_ encapsulation class


package com.example.expandablelistview;
 
public class Person {
 String name;
 public Person() {
 // TODO Auto-generated constructor stub
 }
 public Person(String name) {
 super();
 this.name = name;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 @Override
 public String toString() {
 return "Person [name=" + name + "]";
 }
 
}

Related articles: