二级菜单---ExpandableListView
16lz
2022-01-09
ExpandableListView: 需要继承 BaseExpandableListAdapter 设置适配器
默认有下拉收缩箭头,可以自定义样式-- android:state_expanded = "true"
注意如果子类布局中如:checkbox 的要设置不获取焦点,否则可能会点击组无效
<ExpandableListView android:id="@+id/grow_scgrade_exlist" android:layout_width="match_parent" <span style="color:#993300;"> <strong>android:groupIndicator="@null"</strong></span> android:layout_height="match_parent" > </ExpandableListView>
<?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="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/item_grow_parent_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="5dp" android:paddingLeft="10dp" android:paddingTop="5dp" android:textSize="20sp" /></LinearLayout>
子类布局
<?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="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/item_grow_child_txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="18sp" /></LinearLayout>
public class MyExpandAdapter extends BaseExpandableListAdapter {private List<String> parent = new ArrayList<String>();private List<List<String>> child = new ArrayList<List<String>>();private LayoutInflater inflater;<span style="white-space:pre"></span>//设置数据源public void setData(List<String> parent, List<List<String>> child) {this.parent = parent;this.child = child;notifyDataSetChanged();}public MyExpandAdapter(Context context) {inflater = LayoutInflater.from(context);}@Overridepublic int getGroupCount() {return parent.size();}@Overridepublic int getChildrenCount(int groupPosition) {return child.get(groupPosition).size();}@Overridepublic Object getGroup(int groupPosition) {return parent.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(groupPosition).get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return false;}/* * 父类布局 */@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {TextView txt;if (convertView == null) {convertView = inflater.inflate(R.layout.item_grow_grade1_layout, parent, false);txt = (TextView) convertView.findViewById(R.id.item_grow_parent_txt);convertView.setTag(R.id.item_grow_parent_txt, txt);} else {txt = (TextView) convertView.getTag(R.id.item_grow_parent_txt);}txt.setText((CharSequence) getGroup(groupPosition));return convertView;}/* * 子类布局 */@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {TextView txt;if (convertView == null) {convertView = inflater.inflate(R.layout.item_grow_grade2_layout, null);txt = (TextView) convertView.findViewById(R.id.item_grow_child_txt);convertView.setTag(R.id.item_grow_child_txt, txt);} else {txt = (TextView) convertView.getTag(R.id.item_grow_child_txt);}txt.setText((String) getChild(groupPosition, childPosition));return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}
更多相关文章
- [Android]设置Activity为全屏显示的两种方法
- Android系统属性SystemProperties分析
- ListView去掉中间的分割线和设置Item之间的间距
- 高通平台android9.0设置开机默认横屏显示
- Android上传图片(PHP服务器)
- android沉浸式状态栏底部背景用图片代替
- arcgis for Android(安卓)100.2 加载shp
- android selector下的设置背景属性值
- Android(安卓)设置EditText光标Curso颜色及粗细