Android使用ExpandableListView实现折叠的效果

ExpandableListView 主要为了显示我们常见的折叠的效果,如下所示:


主要用过一个ExpandableListView+自定义一个继承BaseExpandableListAdapter的适配器实现

不多说直接实现代码:

第一步:首先建议个mian.xml文件文件当中防止一个ExpandableListView控件,

 <ExpandableListView 
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        ></ExpandableListView>
第二步:写一个适配器:
public class ExpandAdapter extends BaseExpandableListAdapter {

	private List<String> group;
	private List<List<String>> child;
	private Context context;
	
	public ExpandAdapter(){}
	public ExpandAdapter(List<String> group,List<List<String>>child,Context context){
		
		this.group = group;
		this.child = child;
		this.context = context;
	}
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return this.child.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPositon, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView childView = getTextView();
		childView.setText(this.child.get(groupPositon).get(childPosition));
		childView.setTextSize(10);
		childView.setTextColor(Color.RED);
		return childView;
	}

	private TextView getTextView(){
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
		TextView textView = new TextView(this.context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setPadding(36, 0, 0, 0);
		
		return textView;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.child.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return this.group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return this.group.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpand, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView groupView = getTextView();
		groupView.setText(this.group.get(groupPosition));
		groupView.setTextSize(25);
		groupView.setTextColor(Color.YELLOW);
		return groupView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

}
第三步:Activity类讲适配器与listView组合
public class MainActivity extends ExpandableListActivity {

	private List<String> group;
	private List<List<String>> child;
	private ExpandableListView listView ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置窗口无标题
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.activity_main);
		//
		getExpandableListView().setBackgroundResource(R.drawable.ic_launcher);
		initView();
		getExpandableListView().setAdapter(new ExpandAdapter(this.group,this.child,this));
		//设置拖动列表的时候防止出现黑色背景  
		getExpandableListView().setCacheColorHint(0);
		
	}

	
	private void initView(){
		listView = (ExpandableListView) findViewById(android.R.id.list);
		this.group = new ArrayList<String>();
		this.child = new ArrayList<List<String>>();
		
		addInfo("网球", new String[]{"费德勒","德约科维奇","纳达尔"});
		addInfo("足球运动员", new String[]{"小罗","梅西","马拉多拉"});
		addInfo("篮球运动员", new String[]{"乔丹","科比","勒布朗"});
		
		
	}
	
	private void addInfo(String g,String[] childitem){
		this.group.add(g);
		List<String> list = new ArrayList<String>();
		for (String str : childitem) {
			list.add(str);
		}
		this.child.add(list);
	}
	
	
}
最终运行效果如上所示。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值