当前位置: 代码迷 >> Android >> 基于Android的计步器(Pedometer)的讲授(七)——ExpandableListView
  详细解决方案

基于Android的计步器(Pedometer)的讲授(七)——ExpandableListView

热度:98   发布时间:2016-04-28 02:47:32.0
基于Android的计步器(Pedometer)的讲解(七)——ExpandableListView

今天移动开发课程验收完成,可以小小的松一口气了,感觉在同学中,自己做的这个计步器还是挺不错的嘛~~

然后对原来的手机计步器(pedometer)项目又做了一些完善:

https://github.com/296777513/pedometer

先给出ExpandableListView的一些效果图

先给出ExpandableListViewAdapter的代码:

package com.bit.expandablelistview;import java.util.HashMap;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.TextView;public class ExpandableListViewAdapter extends BaseExpandableListAdapter {	private List<String> group;//这是组视图	private HashMap<String, List<User>> child;//这是子视图	private ExpandableListView listView;//当选择另一个组视图时关闭当前打开的组视图	private LayoutInflater inflater;//加载布局文件	private int last;//判断是否打开当前的页面	public ExpandableListViewAdapter(Context context, List<String> group,			HashMap<String, List<User>> child, ExpandableListView listView) {		this.group = group;		this.child = child;		this.listView = listView;		this.inflater = LayoutInflater.from(context);	}	/**	 * 对外提供一个借口,当数据改变时调用这个函数	 * @param group	 * @param child	 */	public void setOnDataChanged(List<String> group,			HashMap<String, List<User>> child) {		this.group = group;		this.child = child;		this.notifyDataSetChanged();	}	@Override	public int getGroupCount() {		// TODO Auto-generated method stub		return group.size();	}	@Override	public int getChildrenCount(int groupPosition) {		List<User> tempList = child.get(group.get(groupPosition));		if (tempList != null) {			return tempList.size();		} else {			return 0;		}	}	@Override	public Object getGroup(int groupPosition) {		// TODO Auto-generated method stub		return group.get(groupPosition);	}	@Override	public Object getChild(int groupPosition, int childPosition) {		// TODO Auto-generated method stub		return child.get(group.get(groupPosition)).get(childPosition);	}	@Override	public long getGroupId(int groupPosition) {		// TODO Auto-generated method stub		return groupPosition;	}	@Override	public long getChildId(int groupPosition, int childPosition) {		// TODO Auto-generated method stub		return childPosition;	}	@Override	public boolean hasStableIds() {		// TODO Auto-generated method stub		return false;	}	/**	 * 得到组视图布局文件,对这个进行了优化,可以看到converView是一个Android提供的缓存的对象,如果第一次没有这个布局文件	 * 就创建一个,如果有的话,直接从convertView得到,不用进行创建,减少内存开销	 */	public View getGroupView(int groupPosition, boolean isExpanded,			View convertView, ViewGroup parent) {		String groupName = group.get(groupPosition);		View view;		TextView name;		if (convertView == null) {			view = inflater.inflate(R.layout.group_item, null);			name = (TextView) view.findViewById(R.id.group_number);			view.setTag(name);		} else {			view = convertView;			name = (TextView) view.getTag();		}		name.setText(groupName);		return view;	}	private class ViewHolder {		ImageView picture;		TextView name;	}	/**	 * 得到子视图的布局文件,类似于组视图	 */	@Override	public View getChildView(int groupPosition, int childPosition,			boolean isLastChild, View convertView, ViewGroup parent) {		User user = child.get(group.get(groupPosition)).get(childPosition);		View view;		ViewHolder viewHolder;		if (convertView == null) {			view = inflater.inflate(R.layout.child_item, null);			viewHolder = new ViewHolder();			viewHolder.picture = (ImageView) view.findViewById(R.id.pic);			viewHolder.name = (TextView) view.findViewById(R.id.child_name);			view.setTag(viewHolder);		} else {			view = convertView;			viewHolder = (ViewHolder) view.getTag();		}		viewHolder.name.setText(user.getName());		viewHolder.picture.setImageResource(user.getPicture());		return view;	}	/**	 * 设置可以点击子视图	 */	@Override	public boolean isChildSelectable(int groupPosition, int childPosition) {		// TODO Auto-generated method stub		return true;	}	@Override	public void onGroupExpanded(int groupPosition) {		super.onGroupExpanded(groupPosition);		if (groupPosition != last) {			listView.collapseGroup(last);		}		last = groupPosition;	}}

然后给出MainActivity类的代码

package com.bit.expandablelistview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.Toast;public class MainActivity extends Activity implements OnChildClickListener {	private List<String> group;//这是组视图	private HashMap<String, List<User>> child;//这是子视图	private ExpandableListView listView;//这是可以扩展的listview	private ExpandableListViewAdapter adapter;	//这是子视图要加载的所有图片	private int pic[] = new int[] { R.drawable.head1, R.drawable.head2,			R.drawable.head3, R.drawable.head4, R.drawable.head5,			R.drawable.head6, R.drawable.head7, R.drawable.head8,			R.drawable.head9, R.drawable.head10 };	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		init();		prepareData();		showData();	}	/**	 * 显示数据	 */	private void showData() {		if (adapter == null) {			adapter = new ExpandableListViewAdapter(this, group, child,					listView);			listView.setAdapter(adapter);		} else {			adapter.setOnDataChanged(group, child);		}	}	/**	 * 初始化一些参数,设置监听事件	 */	private void init() {		group = new ArrayList<String>();		child = new HashMap<String, List<User>>();		listView = (ExpandableListView) findViewById(R.id.expandlistview);		listView.setOnChildClickListener(this);	}	/**	 * 准备测试数据	 */	private void prepareData() {		group.add("第一组");		group.add("第二组");		group.add("第三组");		List<User> users = new ArrayList<User>();		for (int i = 0; i < 4; i++) {			User user = new User();			user.setName("张全蛋" + i);			user.setPicture(pic[i]);			users.add(user);		}		child.put(group.get(0), users);		users = new ArrayList<User>();		for (int i = 4; i < 7; i++) {			User user = new User();			user.setName("李铁柱" + i);			user.setPicture(pic[i]);			users.add(user);		}		child.put(group.get(1), users);		users = new ArrayList<User>();		for (int i = 7; i < 10; i++) {			User user = new User();			user.setName("唐马儒" + i);			user.setPicture(pic[i]);			users.add(user);		}		child.put(group.get(2), users);		}	@Override	public boolean onChildClick(ExpandableListView parent, View v,			int groupPosition, int childPosition, long id) {		//点击显示当前子视图的位置		Toast.makeText(				MainActivity.this,				"你点击的是"						+ child.get(group.get(groupPosition))								.get(childPosition).getName(),				Toast.LENGTH_SHORT).show();		return false;	}}

大家注意,其中的User类是自己定义的一个类。

给出了一个简单实现的demo(0分下载):

http://download.csdn.net/detail/a296777513/8362613

  相关解决方案