目录
案例演示:选择四大名著人物
1、创建安卓项目
- 选择模板
- 完善项目信息
2、导入图片素材
3、编写主布局资源文件
4、创建组列表项模板文件
- 修改文件代码
5、创建子列表项模板文件
- 修改文件代码
6、创建可扩展列表适配器
- 源代码
package net.zs.select_character;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
/**
* 功能:图书适配器
* 作者:华卫
* 日期:2020年11月13日
*/
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_list_item, 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、编写主界面类代码
- 源代码
package net.zs.select_character;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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、运行程序,查看效果