二級菜單---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;
		}


	}

  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章