`
fonter
  • 浏览: 858680 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android BaseExpandableListAdapter 教程

阅读更多

先上图再说,实现效果如下图,选项可多少可变化。

 

 

 

BaseExpandableListAdapter实现

 

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.iwidsets.clear.manager.R;
import com.iwidsets.clear.manager.adapter.BrowserInfo;

public class ClearExpandableListAdapter extends BaseExpandableListAdapter {

	class ExpandableListHolder {
		ImageView appIcon;
		TextView appInfo;
		CheckBox appCheckBox;
	}

	private Context context;
	private LayoutInflater mChildInflater;
	private LayoutInflater mGroupInflater;
	private List<GroupInfo> group;

	public ClearExpandableListAdapter(Context c, List<GroupInfo> group) {
		this.context = c;
		mChildInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mGroupInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.group = group;
	}

	public Object getChild(int childPosition, int itemPosition) {
		return group.get(childPosition).getChild(itemPosition);
	}

	public long getChildId(int childPosition, int itemPosition) {

		return itemPosition;
	}

	@Override
	public int getChildrenCount(int index) {
		return group.get(index).getChildSize();
	}

	public Object getGroup(int index) {
		return group.get(index);
	}

	public int getGroupCount() {
		return group.size();
	}

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

	public View getGroupView(int position, boolean flag, View view,
			ViewGroup parent) {
		ExpandableListHolder holder = null;
		if (view == null) {
			view = mGroupInflater.inflate(
					R.layout.browser_expandable_list_item, null);
			holder = new ExpandableListHolder();
			holder.appIcon = (ImageView) view.findViewById(R.id.app_icon);
			view.setTag(holder);
			holder.appInfo = (TextView) view.findViewById(R.id.app_info);
			holder.appCheckBox = (CheckBox) view
					.findViewById(R.id.app_checkbox);
		} else {
			holder = (ExpandableListHolder) view.getTag();
		}
		GroupInfo info = this.group.get(position);
		if (info != null) {
			holder.appInfo.setText(info.getBrowserInfo().getAppInfoId());
			Drawable draw = this.context.getResources().getDrawable(
					info.getBrowserInfo().getImageId());
			holder.appIcon.setImageDrawable(draw);
			holder.appCheckBox.setChecked(info.getBrowserInfo().isChecked());
		}
		return view;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		return false;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ExpandableListHolder holder = null;
		if (convertView == null) {
			convertView = mChildInflater.inflate(
					R.layout.browser_expandable_list_item, null);
			holder = new ExpandableListHolder();
			holder.appIcon = (ImageView) convertView
					.findViewById(R.id.app_icon);
			convertView.setTag(holder);
			holder.appInfo = (TextView) convertView.findViewById(R.id.app_info);
			holder.appCheckBox = (CheckBox) convertView
					.findViewById(R.id.app_checkbox);
		} else {
			holder = (ExpandableListHolder) convertView.getTag();
		}
		BrowserInfo info = this.group.get(groupPosition)
				.getChild(childPosition);
		if (info != null) {
			holder.appInfo.setText(info.getAppInfoId());
			Drawable draw = this.context.getResources().getDrawable(
					info.getImageId());
			holder.appIcon.setImageDrawable(draw);
			holder.appCheckBox.setChecked(info.isChecked());
		}
		return convertView;
	}

}

 

要想让child获得焦点,只在改

 

	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

  

 

browser_expandable_list_item.xml 用于显示每一个ITEM的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="wrap_content"
	android:orientation="horizontal" android:minHeight="40px"
	android:layout_gravity="center_vertical">
	<CheckBox android:id="@+id/app_checkbox" android:focusable="false"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:layout_marginLeft="35px" android:checked="true"/>
	<ImageView android:id="@+id/app_icon" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_gravity="center_vertical" />

	<TextView android:id="@+id/app_info" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:textColor="?android:attr/textColorPrimary"
		android:paddingLeft="3px" android:layout_gravity="center_vertical" />
</LinearLayout>

 

 

browserlayout.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:orientation="vertical">
	<ExpandableListView android:id="@+id/appList"
		android:layout_width="fill_parent" android:layout_height="0dip"
		android:layout_weight="1" />
	<LinearLayout android:layout_height="wrap_content"
		android:layout_width="fill_parent" android:paddingLeft="4dip"
		android:paddingRight="4dip" android:paddingBottom="1dip"
		android:paddingTop="5dip" android:background="@android:drawable/bottom_bar"
 android:id="@+id/app_footer">
		<Button android:layout_weight="1" android:id="@+id/btn_export"
			android:layout_width="0dip" android:layout_height="fill_parent"
			android:text="@string/clear"></Button>
		<Button android:layout_weight="1" android:id="@+id/btn_sel_all"
			android:layout_width="0dip" android:layout_height="fill_parent"
			android:text="select_all"></Button>
		<Button android:layout_weight="1" android:id="@+id/btn_desel_all"
			android:layout_width="0dip" android:layout_height="fill_parent"
			android:text="deselect_all"></Button>
	</LinearLayout>

</LinearLayout>

 

BrowserInfo用于提供一个项的信息

 

public class BrowserInfo {

	private int appInfoId;
	private int imageId;
	private boolean checked;

	public BrowserInfo(int appInfoId, int imageId, boolean checked) {
		this.appInfoId = appInfoId;
		this.imageId = imageId;
		this.checked = checked;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public int getAppInfoId() {
		return appInfoId;
	}

	public void setAppInfoId(int appInfoId) {
		this.appInfoId = appInfoId;
	}

	public int getImageId() {
		return imageId;
	}

	public void setImageId(int imageId) {
		this.imageId = imageId;
	}

}

 

GroupInfo 用于显示一个组的信息

 

import java.util.List;

public class GroupInfo {

	private BrowserInfo group;
	private List<BrowserInfo> child;

	public GroupInfo(BrowserInfo group, List<BrowserInfo> child) {

		this.group = group;
		this.child = child;
	}
	
	public void add(BrowserInfo info){
		child.add(info);
	}
	
	public void remove(BrowserInfo info){
		child.remove(info);
	}
	
	public void remove(int index){
		child.remove(index);
	}
	
	public int getChildSize(){
		return child.size();
	}
	
	public BrowserInfo getChild(int index){
		return child.get(index);
	}

	public BrowserInfo getBrowserInfo() {
		return group;
	}

	public void setBrowserInfo(BrowserInfo group) {
		this.group = group;
	}

	public List<BrowserInfo> getChild() {
		return child;
	}

	public void setChild(List<BrowserInfo> child) {
		this.child = child;
	}

}

 

 

ClearBrowserActivity 最后就是把要显示的内容显示出来的。

 

 

public class ClearBrowserActivity extends Activity implements ExpandableListView.OnGroupClickListener,ExpandableListView.OnChildClickListener{

	private List<GroupInfo> group;

	private ClearExpandableListAdapter listAdapter = null;
	private ExpandableListView appList;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browserlayout);
		appList = (ExpandableListView) findViewById(R.id.appList);
		init();
		BrowserInfo browsingHistoryParents = newBrowserInfo(
				R.string.browsinghistory, R.drawable.browser_image,true);
		List<BrowserInfo> browsingHistoryChild = new ArrayList<BrowserInfo>();
		BrowserInfo browser = newBrowserInfo(R.string.browser,
				R.drawable.browser_image, true);
		browsingHistoryChild.add(browser);

		BrowserInfo operaClear = newBrowserInfo(R.string.opera_clear,
				R.drawable.browser_image,true);
		browsingHistoryChild.add(operaClear);
		
		BrowserInfo firefoxClear = newBrowserInfo(R.string.firefox_clear,
				R.drawable.browser_image,true);
		browsingHistoryChild.add(firefoxClear);
		
		BrowserInfo ucwebClear = newBrowserInfo(R.string.ucweb_clear,
				R.drawable.browser_image,false);
		browsingHistoryChild.add(ucwebClear);
		
		GroupInfo browserGroup = new GroupInfo(browsingHistoryParents,
				browsingHistoryChild);
		addGroup(browserGroup);

		listAdapter = new ClearExpandableListAdapter(this, group);
		appList.setOnChildClickListener(this);
		appList.setOnGroupClickListener(this);
		appList.setAdapter(listAdapter);
	}

	private void init() {
		group = new ArrayList<GroupInfo>();
	}

	private void addGroup(GroupInfo info) {
		group.add(info);
	}

	private static BrowserInfo newBrowserInfo(int infoId, int imageId,boolean checked) {
		return new BrowserInfo(infoId, imageId,checked);
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		return false;
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		return false;
	}
}

 

 

 

 

  • 大小: 19.3 KB
分享到:
评论
25 楼 gf_crazy 2011-10-21  
找了好久,终于找到了!哈
24 楼 fonter 2011-09-19  
安轩之 写道
你好。现在有个棘手的问题。想请教一下:比如点击某个item父类时,怎么让他直接跳到(显示)你点击的那个item父类并展示出子类的内容,在此过程中并把前面的一些item父类(包括子类)隐藏掉 。QQ 375290562,看到留言,请Q我。非常的感谢。

Activity implements OnItemClickListener类,如下面例子

public void onItemClick(AdapterView<?> ada, View v, int index,
			long indexLong) {
		// TODO Auto-generated method stub
		currentArchiveInfo = (ArchiveInfo) archiveAdapter.getItem(index);
		if (currentArchiveInfo != null) {
			if (currentArchiveInfo.isDirectory()) {
				rootPath = rootPath + currentArchiveInfo.getName();
				setTitle("  " + getString(R.string.archive_browse) + " -- "
						+ rootPath);
				// Log.e("rootPath", "rootPath=" + rootPath);
				browse();
			} else {
				unFileThread(currentArchiveInfo);

			}

		}
	}
23 楼 安轩之 2011-09-18  
你好。现在有个棘手的问题。想请教一下:比如点击某个item父类时,怎么让他直接跳到(显示)你点击的那个item父类并展示出子类的内容,在此过程中并把前面的一些item父类(包括子类)隐藏掉 。QQ 375290562,看到留言,请Q我。非常的感谢。
22 楼 大柳树 2011-07-03  
赞一个先~~~
21 楼 fonter 2010-10-04  
DerekGuoLZU 写道
您好!我能否发个您写过的一个Demo给我们参考一下,谢谢!

最近比较忙,应该没什么时间帮你哦。。。
20 楼 DerekGuoLZU 2010-10-03  
您好!那个点组的时候也不能展开……
19 楼 DerekGuoLZU 2010-10-03  
您好!我能否发个您写过的一个Demo给我们参考一下,谢谢!
18 楼 fonter 2010-10-03  
自定义的布局文件中是否需要
DerekGuoLZU 写道
您好!在自定义的布局文件中是否需要什么特殊的属性配置?其他的问题实在是想不到了……

还是不行吗。。。我也是这样的啊。。那就奇怪了。。
17 楼 DerekGuoLZU 2010-10-03  
您好!在自定义的布局文件中是否需要什么特殊的属性配置?其他的问题实在是想不到了……
16 楼 DerekGuoLZU 2010-10-03  
你好!我在程序中有设置这两个函数的返回值为true
15 楼 fonter 2010-10-03  
要想让child获得焦点,只在改

Java代码
public boolean isChildSelectable(int arg0, int arg1) {  
    return true;  


public boolean isChildSelectable(int arg0, int arg1) {
return true;

DerekGuoLZU 写道
我的意思是虽然注册了ExpandableListView.OnGroupClickListener和
ExpandableListView.OnGroupCollapseListener
,但这两个监听类的onGroupClick和onChildClick方法并未被调用,以至于无法响应单击事件……


要想让child获得焦点,只在改



Java代码
public boolean isChildSelectable(int arg0, int arg1) {  
    return true;  


14 楼 DerekGuoLZU 2010-10-02  
我的意思是虽然注册了ExpandableListView.OnGroupClickListener和
ExpandableListView.OnGroupCollapseListener
,但这两个监听类的onGroupClick和onChildClick方法并未被调用,以至于无法响应单击事件……

13 楼 fonter 2010-10-02  
DerekGuoLZU 写道
您好!看了上面的文章受益匪浅,但本人还有个疑问,还请指教:仿照上面的方法进行开发时,当点击组和小孩时,实现的onGroupClick和onChildClick方法并未被调用,还请指导,谢谢!

DerekGuoLZU 写道
您好!看了上面的文章受益匪浅,但本人还有个疑问,还请指教:仿照上面的方法进行开发时,当点击组和小孩时,实现的onGroupClick和onChildClick方法并未被调用,还请指导,谢谢!

我用到是的目前这两个

ExpandableListView.OnGroupClickListener,
ExpandableListView.OnGroupCollapseListener

你可以改改试试。。文章是我第一次搞时的代码,有些方法可接口可能没用。。。

12 楼 DerekGuoLZU 2010-10-02  
您好!看了上面的文章受益匪浅,但本人还有个疑问,还请指教:仿照上面的方法进行开发时,当点击组和小孩时,实现的onGroupClick和onChildClick方法并未被调用,还请指导,谢谢!
11 楼 fonter 2010-07-18  
Zoeh-ruan 写道
因为这个卡关好久了   太感谢了  

客气了客气了...哈哈
10 楼 Zoeh-ruan 2010-07-18  
因为这个卡关好久了   太感谢了  
9 楼 fonter 2010-07-15  
Zoeh-ruan 写道
太感谢你了
又学了一课
不过有个疑问的点是
似乎没有看到getboolean的地方
那group在展开的时候为什么能够直接抓到DB中的isChecked()值呢?

Zoeh-ruan 写道
太感谢你了
又学了一课
不过有个疑问的点是
似乎没有看到getboolean的地方
那group在展开的时候为什么能够直接抓到DB中的isChecked()值呢?



DriverInfo.getSharedPreferences().getBoolean


8 楼 Zoeh-ruan 2010-07-15  
太感谢你了
又学了一课
不过有个疑问的点是
似乎没有看到getboolean的地方
那group在展开的时候为什么能够直接抓到DB中的isChecked()值呢?
7 楼 fonter 2010-07-13  
Zoeh-ruan 写道
请问"在一个类里写一个全局的如SharedPreferences sharedPreferences = activity.getSharedPreferences("SHRAREDNAME",Context.MODE_PRIVATE);"
意思是像下面的CODE一样另外写一个class去抓值,然后在isChecked()去呼叫吗?

public class SharedPreference extends Activity{
public boolean getPref(String value){
SharedPreferences settings=getSharedPreferences("Preference_bool",0);
boolean b = settings.getBoolean("a01",false);
return b;
}
}
public boolean isChecked() {
SharedPreference test = new SharedPreference();
checked = test.getPref(value);
Log.d("isC","isc :"+checked);
return checked;
}

另一个问题就是除了在ClearBrowserActivity里面使用去SharedPreferences settings=getSharedPreferences("Preference_bool",Context.MODE_PRIVATE);之外,
在其它的JAVA档里使用上面的CODE开启模拟器之后,都会runtime exception,一直不知道为什么   麻烦了


对了记得在Adapter 上加一个事件

			holder.appCheckBox.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					info.setChecked(!info.isChecked());
}
}
6 楼 fonter 2010-07-13  
Zoeh-ruan 写道
请问"在一个类里写一个全局的如SharedPreferences sharedPreferences = activity.getSharedPreferences("SHRAREDNAME",Context.MODE_PRIVATE);"
意思是像下面的CODE一样另外写一个class去抓值,然后在isChecked()去呼叫吗?

public class SharedPreference extends Activity{
public boolean getPref(String value){
SharedPreferences settings=getSharedPreferences("Preference_bool",0);
boolean b = settings.getBoolean("a01",false);
return b;
}
}
public boolean isChecked() {
SharedPreference test = new SharedPreference();
checked = test.getPref(value);
Log.d("isC","isc :"+checked);
return checked;
}

另一个问题就是除了在ClearBrowserActivity里面使用去SharedPreferences settings=getSharedPreferences("Preference_bool",Context.MODE_PRIVATE);之外,
在其它的JAVA档里使用上面的CODE开启模拟器之后,都会runtime exception,一直不知道为什么   麻烦了


我把我的code贴出来给你看吧,你参考一下
	private void saveCheckSettings() {
		Editor editor = DriverInfo.getSharedPreferences().edit();
		List<GroupInfo> list = listAdapter.getGroup();
		for (GroupInfo info : list) {
			if (info != null) {
				editor.putBoolean(info.getBrowserInfo().getValue(), info
						.getBrowserInfo().isChecked());
				List<BrowserInfo> bList = info.getChild();
				for (BrowserInfo bin : bList) {
					if (bin != null) {
						editor.putBoolean(bin.getValue(), bin.isChecked());
					}
				}
			}
		}
		editor.commit();
	}


然后,这个是全局

public class DriverInfo {

	private static int width;
	private static int height;
	private static SharedPreferences sharedPreferences;
	private static String DB = "AndClearDB";

	public static void init(Activity activity) {
		width = activity.getWindowManager().getDefaultDisplay().getWidth();
		height = activity.getWindowManager().getDefaultDisplay().getHeight();
		sharedPreferences = activity.getSharedPreferences(DB,
				Context.MODE_PRIVATE);
	}
	
	public static SharedPreferences getSharedPreferences() {
		return sharedPreferences;
	}

	public static void setSharedPreferences(SharedPreferences sharedPreferences) {
		DriverInfo.sharedPreferences = sharedPreferences;
	}

	public static int getWidth() {
		return width;
	}

	public static int getHeight() {
		return height;
	}

}



有什么问题可以问

相关推荐

Global site tag (gtag.js) - Google Analytics