新浪微博 登陆  注册   设为首页 加入收藏

学PHP >> Android开发应用 >> Android UI开发第二篇——多级列表(ExpandableListView)

Android UI开发第二篇——多级列表(ExpandableListView)

查看次数3598 发表时间2013-06-05 10:36:36

 开发中很多地方使用到了多级列表,android可以使用ExpandableListView很好的实现,下面模仿了手机qq的实现,见下图。多级列表使用了ExpandableListView,自定义了Adapter;main.xml<?xml...

 开发中很多地方使用到了多级列表,android可以使用ExpandableListView很好的实现,下面模仿了手机qq的实现,见下图。


多级列表使用了ExpandableListView,自定义了Adapter;

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:background="@drawable/default_bg">

	<ExpandableListView android:id="@+id/list"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
        	android:layout_alignParentLeft="true"/>

</RelativeLayout>

member_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="40dip"
	android:layout_gravity="center_horizontal">

	<LinearLayout android:orientation="horizontal"
		android:layout_width="fill_parent" android:layout_height="40dip"
		android:id="@+id/layout_013">
		<ImageView android:id="@+id/ImageView01"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:gravity="center_vertical" android:paddingTop="10dip"
			android:src="@drawable/user_group"></ImageView>
		<RelativeLayout android:id="@+id/layout_013"
			android:layout_width="wrap_content" android:layout_height="wrap_content">
			<TextView android:id="@+id/content_001" android:textSize="26px"
				android:layout_width="wrap_content" android:layout_height="fill_parent"
				android:textColor="#FFFFFF" android:gravity="center_vertical"
				android:paddingLeft="10px" android:layout_gravity="center_vertical"></TextView>
			<ImageView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:id="@+id/tubiao"
				android:layout_alignParentRight="true"></ImageView>
		</RelativeLayout>
	</LinearLayout>
</LinearLayout>

member_childitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:id="@+id/childlayout" android:orientation="horizontal">
	<ImageView android:id="@+id/child_image"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:background="@drawable/child_image" android:paddingTop="10dip"
		android:layout_marginLeft="40dip"></ImageView>
	<LinearLayout android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:orientation="vertical">
		<TextView android:text="" android:id="@+id/child_text"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:gravity="center_vertical" android:textSize="16dip"
			android:layout_gravity="center_vertical"></TextView>
		<TextView android:text="" android:id="@+id/child_text2"
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:textSize="12dip" android:gravity="center_vertical"
			android:layout_gravity="center_vertical"></TextView>
	</LinearLayout>
</LinearLayout>

ExListView.java

public class ExListView extends Activity {
	private static final String G_TEXT = "g_text";

	private static final String C_TEXT1 = "c_text1";
	private static final String C_TEXT2 = "c_text1";

	List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
	List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

	ExAdapter adapter;
	ExpandableListView exList;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		for (int i = 0; i < 5; i++) {
			Map<String, String> curGroupMap = new HashMap<String, String>();
			groupData.add(curGroupMap);
			curGroupMap.put(G_TEXT, "Group " + i);

			List<Map<String, String>> children = new ArrayList<Map<String, String>>();
			for (int j = 0; j < 5; j++) {
				Map<String, String> curChildMap = new HashMap<String, String>();
				children.add(curChildMap);
				curChildMap.put(C_TEXT1, "Child " + j);
				curChildMap.put(C_TEXT2, "Child " + j);
			}
			childData.add(children);
		}

		adapter = new ExAdapter(ExListView.this);
		exList = (ExpandableListView) findViewById(R.id.list);
		exList.setAdapter(adapter);
		exList.setGroupIndicator(null);
		exList.setDivider(null);
	}

	class ExAdapter extends BaseExpandableListAdapter {
		ExListView exlistview;

		public ExAdapter(ExListView elv) {
			super();
			exlistview = elv;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {

			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.member_listview, null);
			}

			TextView title = (TextView) view.findViewById(R.id.content_001);
			title.setText(getGroup(groupPosition).toString());

			ImageView image = (ImageView) view.findViewById(R.id.tubiao);
			if (isExpanded)
				image.setBackgroundResource(R.drawable.btn_browser2);
			else
				image.setBackgroundResource(R.drawable.btn_browser);

			return view;
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public Object getGroup(int groupPosition) {
			return groupData.get(groupPosition).get(G_TEXT).toString();
		}

		public int getGroupCount() {
			return groupData.size();

		}

		// **************************************
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.member_childitem, null);
			}
			final TextView title = (TextView) view
					.findViewById(R.id.child_text);
			title.setText(childData.get(groupPosition).get(childPosition)
					.get(C_TEXT1).toString());
			final TextView title2 = (TextView) view
					.findViewById(R.id.child_text2);
			title2.setText(childData.get(groupPosition).get(childPosition)
					.get(C_TEXT2).toString());

			return view;
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public Object getChild(int groupPosition, int childPosition) {
			return childData.get(groupPosition).get(childPosition).get(C_TEXT1)
					.toString();
		}

		public int getChildrenCount(int groupPosition) {
			return childData.get(groupPosition).size();
		}

		// **************************************
		public boolean hasStableIds() {
			return true;
		}

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

	}
}


转自:http://blog.csdn.net/xyz_lmn/article/details/6906268


(转发请注明转自:学PHP)    


  相关推荐




  发表评论
昵称:
(不超过20个字符或10个汉字)
内容: