ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下:

首先:在layout的xml文件中定义一个ExpandableListView

<LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical"> <ExpandableListView android:id="@+id/expandableListView" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>

定义两个List,用来存放控件中Group/Child中的String

private List<String> groupArray; private List<List<String>> childArray;

对这两个List进行初始化,并插入一些数据

groupArray = new ArrayList<String>(); childArray = new ArrayList<List<String>>(); groupArray.add("第一行"); groupArray.add("第二行"); List<String> tempArray = new ArrayList<String>(); tempArray.add("第一条"); tempArray.add("第二条"); tempArray.add("第三条"); for(int index = 0; index <groupArray.size(); ++index){ childArray.add(tempArray); }

定义ExpandableListView的Adapter

//ExpandableListView的Adapter public class ExpandableAdapter extends BaseExpandableListAdapter { Activity activity; public ExpandableAdapter(Activity a) { activity = a; } public Object getChild(int groupPosition, int childPosition) { return childArray.get(groupPosition).get(childPosition); } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return childArray.get(groupPosition).size(); } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String string = childArray.get(groupPosition).get(childPosition); return getGenericView(string); } // group method stub public Object getGroup(int groupPosition) { return groupArray.get(groupPosition); } public int getGroupCount() { return groupArray.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String string = groupArray.get(groupPosition); return getGenericView(string); } // View stub to create Group/Children 's View public TextView getGenericView(String string) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity); text.setLayoutParams(layoutParams); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position 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; } }

最后个定义好的ExpandableListView添加Adapter

ExpandableListView expandableListView =(ExpandableListView)findViewById(R.id.expandableListView); expandableListView.setAdapter(new ExpandableAdapter(Main.this)

更多相关文章

  1. Android(安卓)RecyclerView嵌套RecyclerView并使用SwipeRefreshL
  2. android:activity销毁后,fragment使用控件,空指针
  3. Android属性动画与自定义属性动画
  4. Android(安卓)Studio 自定义设置注释模板——诺诺"涂鸦"记忆
  5. (4.2.4)【android开源组件】Android(安卓)省市县 三级联动(android-
  6. android自定义camera以及uri和文件路径之间的转换
  7. 替代CardView实现圆角图的第三方控件
  8. android自定义相机添加自定义水印
  9. android 自定义view中onMeasure()理解

随机推荐

  1. 浅析Binder(六)——Java服务启动
  2. Android查询:模拟键盘鼠标事件(adb shell
  3. Android NavigationBar隐藏与浮层
  4. 创建 Android 库
  5. 带weight的LinearLayout嵌套RecyclerView
  6. [Android]Toolbar使用详解(三)——源码解析
  7. RK3288[android 7.1]调试笔记 去掉桌面上
  8. android静态图片和动态壁纸
  9. Android(安卓)Studio 自动检查依赖库是否
  10. android-apt plugin is incompatible wit