Android ExpandableListView 可展开的ListView
16lz
2021-01-23
效果图
public class TestExpandableListView extends Activity {ExpandableListView mView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.expandable_listview);mView = (ExpandableListView) findViewById(R.id.el_list);mView.setAdapter(new MyAdapter());}class MyAdapter extends BaseExpandableListAdapter { //设置组视图的图片// int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu}; //设置组视图的显示文字 private String[] generalsTypes = new String[] { "魏", "蜀", "吴" }; //子视图显示文字 private String[][] generals = new String[][] { { "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" }, { "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" }, { "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" } }; //子视图图片// public int[][] generallogos = new int[][] {// { R.drawable.xiahoudun, R.drawable.zhenji,// R.drawable.xuchu, R.drawable.guojia,// R.drawable.simayi, R.drawable.yangxiu },// { R.drawable.machao, R.drawable.zhangfei,// R.drawable.liubei, R.drawable.zhugeliang,// R.drawable.huangyueying, R.drawable.zhaoyun },// { R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,// R.drawable.zhouyu, R.drawable.sunshangxiang } }; //自己定义一个获得textview的方法 TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 100); TextView textView = new TextView(TestExpandableListView.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; }@Overridepublic int getGroupCount() {return generalsTypes.length;}@Overridepublic int getChildrenCount(int groupPosition) {return generals[groupPosition].length;}@Overridepublic Object getGroup(int groupPosition) {return generalsTypes[groupPosition];}@Overridepublic Object getChild(int groupPosition, int childPosition) {return generals[groupPosition][childPosition];}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView.this); ll.setOrientation(0);// ImageView logo = new ImageView(ExpandableList.this);// logo.setImageResource(logos[groupPosition]);// logo.setPadding(50, 0, 0, 0);// ll.addView(logo); TextView textView = getTextView(); textView.setTextColor(Color.BLUE); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); ll.setPadding(100, 10, 10, 10); return ll;}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(TestExpandableListView.this); ll.setOrientation(0);// ImageView generallogo = new ImageView(TestExpandableListView.this);// generallogo.setImageResource(generallogos[groupPosition][childPosition]);// ll.addView(generallogo); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); ll.addView(textView); return ll;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}
expandable_listview.xml
<?xml version="1.0" encoding="utf-8"?>
一些方法说明:
1. ExpandableListAdapter
group和child都有getView方法,即可使用不同的布局,ViewHolder等
在getView方法的,groupPosition和childPosition 即在两个集合中的位置
2. ExpandableListView
getFirstVisiblePosition();
getLastVisiblePosition(); //返回屏幕最后可见item的位置: 只计算可见view的位置(不含被group 收缩的子view)
getPackedPositionGroup(getExpandableListPosition(getFirstVisiblePosition()); //返回 首可见项所在group位置
setOnChildClickListener() child点击
setOnGroupClickListener() group点击
更多相关文章
- android抓log方法
- android 登陆右上角 的关闭按钮的使用方法
- Android AESUtils 工具类与调用方法
- sqlit导入外部数据库查找数据方法
- Android NDK打印log到logcat的方法
- Android获取系统时间的多种方法
- Android根据经纬度获取位置信息
- Android链式方法显示Dialog