Android(安卓)ExpandableListView显示模板
以下是expandexample.java. 拷贝后即可运行:
package com.xx.xx.xxxxx import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.LinearLayout; import android.widget.TextView; public class pdfOutlineList extends Activity { private ExpandableListView mElv; private LinearLayout mLayout; private List<String> mGroupArray; private List<List<String>> mChildArray; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLayout = new LinearLayout(this); mElv = new ExpandableListView(this); mGroupArray = new ArrayList<String>(); mChildArray = new ArrayList<List<String>>(); mGroupArray.add("第一行"); mGroupArray.add("第二行"); List<String> tempArray = new ArrayList<String>(); tempArray.add("第一条"); tempArray.add("第二条"); tempArray.add("第三条"); for(int index = 0; index <mGroupArray.size(); ++index) { mChildArray.add(tempArray); } mElv.setAdapter(new ExpandableAdapter(this)); mLayout.addView(mElv); setContentView(mLayout); } public class ExpandableAdapter extends BaseExpandableListAdapter { Activity activity; public ExpandableAdapter(Activity a) { activity = a; } public Object getChild(int groupPosition, int childPosition) { return mChildArray.get(groupPosition).get(childPosition); } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return mChildArray.get(groupPosition).size(); } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String string = mChildArray.get(groupPosition).get(childPosition); return getGenericView(string); } public Object getGroup(int groupPosition) { return mGroupArray.get(groupPosition); } public int getGroupCount() { return mGroupArray.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String string = mGroupArray.get(groupPosition); return getGenericView(string); } public TextView getGenericView(String string) { AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity); text.setLayoutParams(layoutParams); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(36, 0, 0, 0); text.setText(string); return text; } public boolean hasStableIds() { return false; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }
最终显示结果:
参考代码:http://blog.csdn.net/Sevan7/archive/2010/10/13/5937472.aspx
更多相关文章
- Android(安卓)service工具类,判断服务是否在运行
- android 爬坑之路
- LeadTools Android(安卓)入门教学——运行第一个Android(安卓)De
- 运行时权限解析以及申请的实现(可完美解决java.lang.SecurityExce
- 为什么要在 Windows 10 系统上运行 Android(安卓)应用程序?
- 在Android(安卓)Studio上测试运行,Unity发布成Android包过程中所
- Apple IOS、Android、WebOS系统体系架构对比
- 编写Android中直接可运行的二进制文件
- Android(安卓)手机上安装并运行 Ubuntu 12.04