首页 > 代码库 > Android ExpandableListView实例Demo
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博客中加入链接需要审核后才能发表,所以需要源码的可以到我上传的资源中查找。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。