当前位置: 代码迷 >> Android >> Android 之ExpandableListView 完全仿QQ密友列表,自定义ExpandableListView
  详细解决方案

Android 之ExpandableListView 完全仿QQ密友列表,自定义ExpandableListView

热度:69   发布时间:2016-05-01 13:55:09.0
Android 之ExpandableListView 完全仿QQ好友列表,自定义ExpandableListView
运行效果:


main.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:padding="8.0dip" android:layout_weight="1.0">	<FrameLayout android:layout_width="fill_parent"		android:layout_height="fill_parent">		<com.iaiai.activity.QExListView			android:id="@id/android:list" android:scrollbars="vertical"			android:layout_width="fill_parent" android:layout_height="fill_parent"			android:layout_marginLeft="0.0dip" android:drawSelectorOnTop="false"			android:scrollingCache="true" android:layout_weight="1.0"			android:fastScrollEnabled="false" android:footerDividersEnabled="true"			android:cacheColorHint="#00000000" />	</FrameLayout></LinearLayout>


group.xml
<?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="match_parent">	<TextView android:id="@+id/groupto" android:layout_width="match_parent"		android:layout_height="match_parent" android:paddingLeft="60px"		android:paddingTop="10px" android:paddingBottom="10px"		android:textSize="26sp" android:text="NO DATA FOUND" /></LinearLayout>


child.xml
<?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="50dp">	<TextView android:id="@+id/childto" android:layout_width="match_parent"		android:layout_height="match_parent" android:paddingLeft="50px"		android:paddingTop="5px" android:paddingBottom="5px" android:textSize="20sp"		android:text="NO DATA FOUND" /></LinearLayout>


MainActivity.java
package com.iaiai;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ExpandableListActivity;import android.os.Bundle;import android.widget.SimpleExpandableListAdapter;/** *  * <p> * Title: MainActivity.java * </p> * <p> * E-Mail: [email protected] * </p> * <p> * QQ: 176291935 * </p> * <p> * Http: iaiai.iteye.com * </p> * <p> * Create time: 2011-10-10 * </p> *  * @author 丸子 * @version 0.0.1 */public class MainActivity extends ExpandableListActivity {	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		List<Map<String, String>> groups = new ArrayList<Map<String, String>>();		Map<String, String> group1 = new HashMap<String, String>();		group1.put("g", "g11");		Map<String, String> group2 = new HashMap<String, String>();		group2.put("g", "g21");		Map<String, String> group3 = new HashMap<String, String>();		group3.put("g", "g31");		groups.add(group1);		groups.add(group2);		groups.add(group3);		List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();		Map<String, String> childdata1 = new HashMap<String, String>();		childdata1.put("c", "c11");		Map<String, String> childdata2 = new HashMap<String, String>();		childdata2.put("c", "c12");		Map<String, String> childdata3 = new HashMap<String, String>();		childdata3.put("c", "c13");		Map<String, String> childdata4 = new HashMap<String, String>();		childdata4.put("c", "c14");		Map<String, String> childdata5 = new HashMap<String, String>();		childdata5.put("c", "c15");		Map<String, String> childdata6 = new HashMap<String, String>();		childdata6.put("c", "c16");		Map<String, String> childdata7 = new HashMap<String, String>();		childdata7.put("c", "c17");		Map<String, String> childdata8 = new HashMap<String, String>();		childdata8.put("c", "c18");		Map<String, String> childdata9 = new HashMap<String, String>();		childdata9.put("c", "c19");		child1.add(childdata1);		child1.add(childdata2);		child1.add(childdata3);		child1.add(childdata4);		child1.add(childdata5);		child1.add(childdata6);		child1.add(childdata7);		child1.add(childdata8);		child1.add(childdata9);		List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();		Map<String, String> childdata21 = new HashMap<String, String>();		childdata21.put("c", "c21");		Map<String, String> childdata22 = new HashMap<String, String>();		childdata22.put("c", "c22");		child2.add(childdata21);		child2.add(childdata22);		List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();		Map<String, String> childdata31 = new HashMap<String, String>();		childdata31.put("c", "c31");		Map<String, String> childdata32 = new HashMap<String, String>();		childdata32.put("c", "c32");		Map<String, String> childdata33 = new HashMap<String, String>();		childdata33.put("c", "c33");		Map<String, String> childdata34 = new HashMap<String, String>();		childdata34.put("c", "c34");		Map<String, String> childdata35 = new HashMap<String, String>();		childdata35.put("c", "c35");		Map<String, String> childdata36 = new HashMap<String, String>();		childdata36.put("c", "c36");		child3.add(childdata31);		child3.add(childdata32);		child3.add(childdata33);		child3.add(childdata34);		child3.add(childdata35);		child3.add(childdata36);		List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();		childs.add(child1);		childs.add(child2);		childs.add(child3);		SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(				this, groups, R.layout.group, new String[] { "g" },				new int[] { R.id.groupto }, childs, R.layout.child,				new String[] { "c" }, new int[] { R.id.childto });		setListAdapter(adapter);	}}


QExListView.java
package com.iaiai.activity;import android.content.Context;import android.os.Handler;import android.util.AttributeSet;import android.view.View;import android.widget.AbsListView;import android.widget.AdapterView;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.FrameLayout;import android.widget.LinearLayout;/** *  * <p> * Title: QExListView.java * </p> * <p> * E-Mail: [email protected] * </p> * <p> * QQ: 176291935 * </p> * <p> * Http: iaiai.iteye.com * </p> * <p> * Create time: 2012-1-12 下午3:39:29 * </p> *  * @author 丸子 * @version 0.0.1 */public class QExListView extends ExpandableListView implements AbsListView.OnScrollListener {	private ExpandableListAdapter _exAdapter = null;	private LinearLayout _groupLayout;	private int _groupIndex = -1;	/**	 * @param context	 */	public QExListView(Context context) {		super(context);		super.setOnScrollListener(this);	}	/**	 * @param context	 * @param attrs	 */	public QExListView(Context context, AttributeSet attrs) {		super(context, attrs);		super.setOnScrollListener(this);	}	/**	 * @param context	 * @param attrs	 * @param defStyle	 */	public QExListView(Context context, AttributeSet attrs, int defStyle) {		super(context, attrs, defStyle);		super.setOnScrollListener(this);	}	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {		if (_exAdapter == null)			_exAdapter = this.getExpandableListAdapter();		int ptp = view.pointToPosition(0, 0);		if (ptp != AdapterView.INVALID_POSITION) {			QExListView qExlist = (QExListView) view;			long pos = qExlist.getExpandableListPosition(ptp);			int groupPos = ExpandableListView.getPackedPositionGroup(pos);			int childPos = ExpandableListView.getPackedPositionChild(pos);			if (childPos < 0) {				groupPos = -1;			}			if (groupPos < _groupIndex) {				_groupIndex = groupPos;				if (_groupLayout != null)					_groupLayout.removeAllViews();			} else if (groupPos > _groupIndex) {				final FrameLayout fl = (FrameLayout) getParent();				_groupIndex = groupPos;				if (_groupLayout != null)					fl.removeView(_groupLayout);				_groupLayout = (LinearLayout) getExpandableListAdapter().getGroupView(groupPos, true, null, null);				_groupLayout.setOnClickListener(new OnClickListener() {					public void onClick(View v) {						collapseGroup(_groupIndex);						new Handler().post(new Runnable() {							public void run() {								// TODO Auto-generated method stub								fl.removeView(_groupLayout);								fl.addView(_groupLayout, new LayoutParams(LayoutParams.FILL_PARENT, 50));							}						});					}				});				fl.addView(_groupLayout, fl.getChildCount(), new LayoutParams(LayoutParams.FILL_PARENT, 50));_groupLayout.setVisibility(GONE);			}		}	}	public void onScrollStateChanged(AbsListView view, int scrollState) {	}}

===============================================================

1 楼 wrh200 2012-06-03  
求源码打包 [email protected]
2 楼 luowenyue5 2012-07-13  
你这个自定义的QExListView也没啥用啊
3 楼 好久不见魔杰座 2012-07-20  
太假了,就只有G11那种,任何效果都没有
4 楼 iaiai 2012-07-21  
好久不见魔杰座 写道
太假了,就只有G11那种,任何效果都没有

你写的有问题吧.... 我试过的没问题的
  相关解决方案