Android UI控件ExpandableListView基本用法详解
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
//View
private ExpandableListView expandableListView;
//Model:定义的数据
private String[] groups = {"A", "B", "C"};
//注意,字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}
private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}, {"A1", "A2", "A3", "C4"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListView.setAdapter(new MyExpandableListView());
}
//为ExpandableListView自定义适配器
class MyExpandableListView extends BaseExpandableListAdapter {
//返回一级列表的个数
@Override
public int getGroupCount() {
return groups.length;
}
//返回每个二级列表的个数
@Override
public int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表
Log.d("smyhvae", "-->" + groupPosition);
return childs[groupPosition].length;
}
//返回一级列表的单个item(返回的是对象)
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
//返回二级列表中的单个item(返回的是对象)
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs[groupPosition][childPosition]; //不要误写成groups[groupPosition][childPosition]
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//每个item的id是否是固定?一般为true
@Override
public boolean hasStableIds() {
return true;
}
//【重要】填充一级列表
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item_group, null);
} else {
}
TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
tv_group.setText(groups[groupPosition]);
return convertView;
}
//【重要】填充二级列表
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item_child, null);
}
ImageView iv_child = (ImageView) convertView.findViewById(R.id.iv_child);
TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);
//iv_child.setImageResource(resId);
tv_child.setText(childs[groupPosition][childPosition]);
return convertView;
}
//二级列表中的item是否能够被选中?可以改为true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播