android好友多级列表显示Expandabl…
2013-10-10 12:46
197 查看
例如在显示群组信息的时候,我们往往需要点击群组之后能够出现该群组详细列表,这里可以采用ExpandableListView这个控件来实现,其实如果会了ListView
以及 BaseAdapter的重写,那么这个很快就可以掌握了,只不过再多设计一个父类的显示吧了。
就不美化了,直接看吧。。。
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
看代码
Main.xml
这里有点说明:如果你要使用原来自带的@id/listview
那么就必须继承ExpandableListActivity
Parent.xml
Child.xml
下面就是MainActivity
主要看下Adapter怎么实现的,这里children里是各个child,有点嵌套,别晕了。。
以及 BaseAdapter的重写,那么这个很快就可以掌握了,只不过再多设计一个父类的显示吧了。
就不美化了,直接看吧。。。
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
看代码
Main.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/expand" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ExpandableListView> </LinearLayout> |
这里有点说明:如果你要使用原来自带的@id/listview
那么就必须继承ExpandableListActivity
Parent.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" > <TextView android:id="@+id/parent" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> |
Child.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" > <LinearLayout android:id="@+id/child" android:layout_height="wrap_content" android:layout_width="fill_parent"> </LinearLayout> </LinearLayout> |
下面就是MainActivity
主要看下Adapter怎么实现的,这里children里是各个child,有点嵌套,别晕了。。
package com.ylf; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { List<String> parents = null; List<List> children = null; ExpandableListView listView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ExpandableListView)findViewById(R.id.expand); parents = new ArrayList<String>(); children = new ArrayList<List>(); List<String> children1 =new ArrayList<String>(); List<String> children2 =new ArrayList<String>(); List<String> children3 =new ArrayList<String>(); List<String> children4 =new ArrayList<String>(); parents.add("水果"); parents.add("蔬菜"); parents.add("卫生间"); parents.add("垃圾堆"); children1.add("香蕉"); children1.add("苹果"); children2.add("空心菜"); children2.add("西红柿"); children3.add("马桶"); children3.add("水池"); children4.add("屎虫"); children4.add("苍蝇"); children.add(children1); children.add(children2); children.add(children3); children.add(children4); listView.setAdapter(new MyExpandableListAdapter()); listView.setGroupIndicator(null); } class MyExpandableListAdapter extends BaseExpandableListAdapter{ @Override public Object getChild(int arg0, int arg1) { return children.get(arg0).get(arg1); } @Override public long getChildId(int arg0, int arg1) { return arg0*parents.size()+arg1; } @Override public View getChildView(int parentCount, int childCount, boolean arg2, View arg3, ViewGroup arg4) { LayoutInflater inflater = LayoutInflater.from(MainActivity.this); View view = inflater.inflate(R.layout.child, null); LinearLayout ll = (LinearLayout)view.findViewById(R.id.child); TextView tv = new TextView(MainActivity.this); tv.setPadding(60, 5, 5, 5); tv.setText((String)getChild(parentCount, childCount)); ll.addView(tv); ll.setPadding(5, 5, 5, 5); return view; } @Override public int getChildrenCount(int groupPosition) { return children.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return parents.get(groupPosition); } @Override public int getGroupCount() { return parents.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(MainActivity.this); View view = inflater.inflate(R.layout.parent, null); TextView tv = (TextView)view.findViewById(R.id.parent); tv.setPadding(50, 5, 5, 5); tv.setText((String)getGroup(groupPosition)); return view; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } } } |
相关文章推荐
- android中树形json解析为对象,并通过dialog显示,多级列表
- android中树形json解析为对象,并通过dialog显示,多级列表
- Android之仿QQ好友列表显示效果源码
- [Android应用] 仿QQHD长按好友列表显示操作条
- Android:Gtalk(显示好友列表)
- android 自定义ListView显示微信聊天好友列表
- Android基于XMPP Smack Openfire开发IM 【二】获取好友列表
- Android:使用ListView显示信息列表
- android开发 列表显示(ListView)
- Android学习之ListView显示下拉列表
- Android仿QQ好友列表分组实现增删改及持久化
- Android 绘制多级树形选择列表实例代码
- android学习之自定义列表显示
- android之ListView和adapter配合显示图片和文字列表
- Android如何修改app不在多任务列表中显示
- Android基于XMPP Smack Openfire开发IM(3)建立好友列表
- Android 自定义 ListView 显示网络上 JSON 格式歌曲列表
- android 如何让应用程序在主菜单[launcher]程序列表中动态显示或隐藏
- Android利用LIstView显示文件列表
- Android UI开发第二篇——多级列表(ExpandableListView)