文章目录
一、案例操作 - 选择四大名著人物
1、创建安卓应用【SelectCharacter】
2、将图片素材拷贝到drawable目录
3、主布局资源文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@drawable/background"
tools:context="net.hw.select_character.MainActivity">
<ExpandableListView
android:id="@+id/elvBook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:childDivider="#555555"
android:childIndicator="@drawable/child" />
</LinearLayout>
4、创建组列表项模板文件group_list_item.xml
<?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="match_parent"
android:background="#00b7ec"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:textColor="#ffffff"
android:textSize="25sp" />
</LinearLayout>
5、创建子列表项模板文件child_list_item.xml
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvCharacter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#0000ff"
android:textSize="22sp" />
</LinearLayout>
6、创建可扩展列表适配器BookAdapter
package net.alt.selectcharacter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class BookAdapter extends BaseExpandableListAdapter {
private Context context;
private String[] groups;
private String[][] children;
public BookAdapter(Context context, String[] groups, String[][] children) {
this.context = context;
this.groups = groups;
this.children = children;
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
/**
* 获取组列表视图
*
* @param groupPosition
* @param isExpanded
* @param convertView
* @param viewGroup
* @return
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
GroupViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.group_list_item, null);
holder = new GroupViewHolder();
holder.tvTitle = convertView.findViewById(R.id.tvTitle);
convertView.setTag(holder);
} else {
holder = (GroupViewHolder) convertView.getTag();
}
holder.tvTitle.setText(groups[groupPosition]);
return convertView;
}
/**
* 获取子列表视图
*
* @param groupPosition
* @param childPosition
* @param isExpanded
* @param convertView
* @param viewGroup
* @return
*/
@Override
public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
ChildViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.child, null);
holder = new ChildViewHolder();
holder.tvCharacter = convertView.findViewById(R.id.tvCharacter);
convertView.setTag(holder);
} else {
holder = (ChildViewHolder) convertView.getTag();
}
holder.tvCharacter.setText(children[groupPosition][childPosition]);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
/**
* 子视图容器
*/
private class ChildViewHolder {
TextView tvCharacter;
}
/**
* 组视图容器
*/
private class GroupViewHolder {
TextView tvTitle;
}
}
7、主界面类MainActivity
package net.alt.selectcharacter;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import net.alt.selectcharacter.BookAdapter;
public class MainActivity extends AppCompatActivity {
private String[] groups = {"西游记", "水浒传", "三国演义", "红楼梦"};
private String[][] children = {
{"唐三藏", "孙悟空", "猪八戒", "沙和尚", "如来", "观音菩萨"},
{"宋江", "林冲", "李逵", "鲁智深", "杨志", "武松", "卢俊义"},
{"曹操", "刘备", "孙权", "诸葛亮", "周瑜", "鲁肃", "陆逊", "赵云"},
{"贾宝玉", "林黛玉", "薛宝钗", "王熙凤", "史湘云", "秦可卿", "贾探春"}
};
private ExpandableListView elvBook;
private BookAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源标识获得控件实例
elvBook = findViewById(R.id.elvBook);
// 创建适配器
adapter = new BookAdapter(this, groups, children);
// 给列表控件设置适配器
elvBook.setAdapter(adapter);
// 给列表控件注册组列表展开监听器(非当前组收拢)
elvBook.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
for (int i = 0; i < groups.length; i++) {
if (i != groupPosition) {
elvBook.collapseGroup(i);
}
}
}
});
// 给列表控件注册组列表单击事件监听器
elvBook.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
Toast.makeText(MainActivity.this, "你单击了第[" + (groupPosition + 1) + "]组:" + groups[groupPosition], Toast.LENGTH_SHORT).show();
// 事件可往后传播
return false;
}
});
// 给列表控件注册子列表单击事件监听器
elvBook.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long childId) {
Toast.makeText(MainActivity.this, "你单击了第[" + (groupPosition + 1) + "]组第[" + (childPosition + 1) + "]项:" + children[groupPosition][childPosition], Toast.LENGTH_SHORT).show();
// 禁止事件往后传播
return true;
}
});
}
}
8、启动应用,查看效果