二级菜单---ExpandableListView

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);
		}


		@Override
		public int getGroupCount() {
			return parent.size();
		}


		@Override
		public int getChildrenCount(int groupPosition) {
			return child.get(groupPosition).size();
		}


		@Override
		public Object getGroup(int groupPosition) {
			return parent.get(groupPosition);
		}


		@Override
		public Object getChild(int groupPosition, int childPosition) {


			return child.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) {
			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;
		}


		/*
		 * 子类布局
		 */
		@Override
		public 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;
		}


		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}


	}

二级菜单---ExpandableListView_第1张图片   二级菜单---ExpandableListView_第2张图片


你可能感兴趣的:(二级菜单---ExpandableListView)