zoukankan      html  css  js  c++  java
  • Android ExpandableListView实例Demo

    前几篇文章介绍了Listview。但在实际开发中也常常会用到多层的Listview来展示数据,比方qq中的好友展示,所以这张来了解一下ExpandableListview。基本思想与Listview大致是同样的。所以用起来会比較方便。

    实现效果图:

    程序代码:


    布局文件:

    activity_main.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
        <ExpandableListView
            android:id="@+id/expandableListView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ExpandableListView>
    
    </RelativeLayout>

    group和child共同使用的布局(当然也能够使用不同的布局来实现)。这的布局比較简单,仅仅有一个Textview:

    list_item:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >
    
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    
    </RelativeLayout>
    

    MainActivity:

    package com.listviewdemo_expandablelistview;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ExpandableListView;
    
    public class MainActivity extends Activity {
    	private ExpandableListView listView;
    	private List<String> group;
    	private List<List<String>> child;
    	private MyAdapter adapter;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		listView = (ExpandableListView) findViewById(R.id.expandableListView);
    		/**
    		 * 初始化数据 
    		 */
    		initData();
    		adapter = new MyAdapter(this,group,child);
    		listView.setAdapter(adapter);
    		
    	}
    
    	private void initData() {
    		group = new ArrayList<String>();
    		child = new ArrayList<List<String>>();
    		addInfo("北京",new String[]{"朝阳","海淀","东城区","西城区"});
    		addInfo("河北", new String[]{"邯郸","石家庄","邢台"});
    		addInfo("广东", new String[]{"广州","深圳","珠海"});
    	}
    	
    	/**
    	 * 加入数据信息
    	 * @param g
    	 * @param c
    	 */
    	private void addInfo(String g,String[] c) {
    		group.add(g);
    		List<String> list = new ArrayList<String>();
    		for (int i = 0; i < c.length; i++) {
    			list.add(c[i]);
    		}
    		child.add(list);
    	}
    
    }
    

    MyAdapter:

    package com.listviewdemo_expandablelistview;
    
    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.TextView;
    /**
     * expandableListView适配器
     *
     */
    public class MyAdapter extends BaseExpandableListAdapter {
    	private Context context;
    	private List<String> group;
    	private List<List<String>> child;
    
    	public MyAdapter(Context context, List<String> group,
    			List<List<String>> child) {
    		this.context = context;
    		this.group = group;
    		this.child = child;
    	}
    
    	@Override
    	public int getGroupCount() {
    		return group.size();
    	}
    
    	@Override
    	public int getChildrenCount(int groupPosition) {
    		return child.size();
    	}
    
    	@Override
    	public Object getGroup(int groupPosition) {
    		return group.get(groupPosition);
    	}
    
    	@Override
    	public Object getChild(int groupPosition, int childPosition) {
    		return child.get(childPosition).get(childPosition);
    	}
    
    	@Override
    	public long getGroupId(int groupPosition) {
    		return groupPosition;
    	}
    
    	@Override
    	public long getChildId(int groupPosition, int childPosition) {
    		return childPosition;
    	}
    
    	@Override
    	public boolean hasStableIds() {
    		return false;
    	}
    	
    	/**
    	 * 显示:group
    	 */
    	@Override
    	public View getGroupView(int groupPosition, boolean isExpanded,
    			View convertView, ViewGroup parent) {
    		ViewHolder holder;
    		if (convertView == null) {
    			convertView = LayoutInflater.from(context).inflate(
    					R.layout.list_item, null);
    			holder = new ViewHolder();
    			holder.textView = (TextView) convertView
    					.findViewById(R.id.textView);
    			convertView.setTag(holder);
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    		}
    		holder.textView.setText(group.get(groupPosition));
    		holder.textView.setTextSize(25);
    		holder.textView.setPadding(36, 10, 0, 10);
    		return convertView;
    
    	}
    	
    	/**
    	 * 显示:child
    	 */
    	@Override
    	public View getChildView(int groupPosition, int childPosition,
    			boolean isLastChild, View convertView, ViewGroup parent) {
    		ViewHolder holder;
    		if (convertView == null) {
    			convertView = LayoutInflater.from(context).inflate(
    					R.layout.list_item, null);
    			holder = new ViewHolder();
    			holder.textView = (TextView) convertView
    					.findViewById(R.id.textView);
    			convertView.setTag(holder);
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    		}
    		holder.textView.setText(child.get(groupPosition).get(childPosition));
    		holder.textView.setTextSize(20);
    		holder.textView.setPadding(72, 10, 0, 10);
    		return convertView;
    	}
    
    	class ViewHolder {
    		TextView textView;
    	}
    
    	@Override
    	public boolean isChildSelectable(int groupPosition, int childPosition) {
    		return false;
    	}
    
    }
    

    如今CSDN博客中增加链接须要审核后才干发表。所以须要源代码的能够到我上传的资源中查找。

  • 相关阅读:
    【今日CS 视觉论文速览】4 Jan 2019
    【今日CS 视觉论文速览】3 Jan 2019
    【今日CS 视觉论文速览】1 Jan 2019
    【机器学习】混淆矩阵及其绘制
    【Vim】批量插入注释符号
    【今日CS 视觉论文速览】31 Dec 2018
    【深度学习风格化/生成艺术】图像融合--毫无违和
    CSDN的一些年度大牛(2018)
    【今日CS 视觉论文速览】 27 Dec 2018
    python与机器视觉(X)打包为exe可执行文件
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5302755.html
Copyright © 2011-2022 走看看