摸鱼学Android 十七(可折叠列表)
程序员文章站
2022-03-11 15:33:43
摸鱼学Android 十七UI控件之七ExpandableListView(可折叠列表)1 常用属性2 说明3 实例UI控件之七ExpandableListView(可折叠列表)1 常用属性childDivider:指定各组内子类表项之间的分隔条,图片不会完全显示, 分离子列表项的是一条直线childIndicator:显示在子列表旁边的Drawable对象,可以是一个图像childIndicatorEnd:子列表项指示符的结束约束位置childIndicatorLeft:子列表项指示符的左...
摸鱼学Android 十七(可折叠列表)
UI控件之七
ExpandableListView(可折叠列表)
1 常用属性
- childDivider:指定各组内子类表项之间的分隔条,图片不会完全显示, 分离子列表项的是一条直线
- childIndicator:显示在子列表旁边的Drawable对象,可以是一个图像
- childIndicatorEnd:子列表项指示符的结束约束位置
- childIndicatorLeft:子列表项指示符的左边约束位置
- childIndicatorRight:子列表项指示符的右边约束位置
- childIndicatorStart:子列表项指示符的开始约束位置
- groupIndicator:显示在组列表旁边的Drawable对象,可以是一个图像
- indicatorEnd:组列表项指示器的结束约束位置
- indicatorLeft:组列表项指示器的左边约束位置
- indicatorRight:组列表项指示器的右边约束位置
- indicatorStart:组列表项指示器的开始约束位置
2 说明
实现ExpandableAdapter的三种方式:
- 扩展BaseExpandableListAdpter实现ExpandableAdapter。
- 使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter
- 使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter
3 实例
- 定义组布局item_group_xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_group_name"
android:layout_width="match_parent"
android:layout_height="56dp"
android:gravity="center_vertical"
android:paddingLeft="30dp"
android:text="分组"
android:textStyle="bold"
android:textSize="20sp" />
</LinearLayout>
- 定义列表项布局item_child.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="horizontal"
android:padding="5dp"
android:background="#6BBA79">
<ImageView
android:id="@+id/img_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/logo"
android:focusable="false"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:focusable="false"
android:text="图标"
android:textSize="18sp" />
</LinearLayout>
- 定义实体类Icon.java
public class Icon {
private int iId;
private String iName;
public Icon() {
}
public Icon(int iId, String iName) {
this.iId = iId;
this.iName = iName;
}
public int getiId() {
return iId;
}
public String getiName() {
return iName;
}
public void setiId(int iId) {
this.iId = iId;
}
public void setiName(String iName) {
this.iName = iName;
}
}
- 自定义BaseExpandableListAdapter
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
private ArrayList<String> gData;
private ArrayList<ArrayList<Icon>> iData;
private Context mContext;
public MyBaseExpandableListAdapter(ArrayList<String> gData,ArrayList<ArrayList<Icon>> iData, Context mContext) {
this.gData = gData;
this.iData = iData;
this.mContext = mContext;
}
@Override
public int getGroupCount() {
return gData.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return iData.get(groupPosition).size();
}
@Override
public String getGroup(int groupPosition) {
return gData.get(groupPosition);
}
@Override
public Icon getChild(int groupPosition, int childPosition) {
return iData.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolderGroup groupHolder;
if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(
R.layout.item_group, parent, false);
groupHolder = new ViewHolderGroup();
groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
convertView.setTag(groupHolder);
}else{
groupHolder = (ViewHolderGroup) convertView.getTag();
}
groupHolder.tv_group_name.setText(gData.get(groupPosition));
return convertView;
}
//取得显示给定分组给定子位置的数据用的视图
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolderItem itemHolder;
if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(
R.layout.item_child, parent, false);
itemHolder = new ViewHolderItem();
itemHolder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
itemHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
convertView.setTag(itemHolder);
}else{
itemHolder = (ViewHolderItem) convertView.getTag();
}
itemHolder.img_icon.setImageResource(iData.get(groupPosition).get(childPosition).getiId());
itemHolder.tv_name.setText(iData.get(groupPosition).get(childPosition).getiName());
return convertView;
}
//设置子列表是否可选中
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private static class ViewHolderGroup{
private TextView tv_group_name;
}
private static class ViewHolderItem{
private ImageView img_icon;
private TextView tv_name;
}
}
- 加入布局
<ExpandableListView
android:id="@+id/exlist_lol"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:childDivider="#E02D2F"/>
- MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<String> gData = null;
private ArrayList<ArrayList<Icon>> iData = null;
private ArrayList<Icon> lData = null;
private Context mContext;
private ExpandableListView exlist_lol;
private MyBaseExpandableListAdapter myAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
exlist_lol = (ExpandableListView) findViewById(R.id.exlist_lol);
//数据准备
gData = new ArrayList<String>();
iData = new ArrayList<ArrayList<Icon>>();
gData.add("工具");
gData.add("娱乐");
gData.add("办公");
//1
lData = new ArrayList<Icon>();
lData.add(new Icon(R.drawable.location,"定位"));
lData.add(new Icon(R.drawable.mike,"录音"));
lData.add(new Icon(R.drawable.printer, "打印机"));
lData.add(new Icon(R.drawable.torch,"手电筒"));
iData.add(lData);
//2
lData = new ArrayList<Icon>();
lData.add(new Icon(R.drawable.game, "游戏"));
lData.add(new Icon(R.drawable.music, "音乐"));
iData.add(lData);
//3
lData = new ArrayList<Icon>();
lData.add(new Icon(R.drawable.chat, "聊天"));
iData.add(lData);
myAdapter = new MyBaseExpandableListAdapter(gData, iData, mContext);
exlist_lol.setAdapter(myAdapter);
//为列表设置点击事件
exlist_lol.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(mContext, "你点击了:" + iData.get(groupPosition).get(childPosition).getiName(), Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
8.运行App,展开列表项,点击显示
本文地址:https://blog.csdn.net/qq_23470315/article/details/108580572
上一篇: stm32 L0 +433(sx1212模块)低功耗
下一篇: 疯壳MSP430实验教程2数码管实验