本文实例讲述了Android中ExpandableListView的用法,ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下:

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

复制代码 代码如下:     android:id="@+id/linearLayout" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    androidrientation="vertical" 
    > 
     
        android:id="@+id/expandableListView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
        /> 

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

复制代码 代码如下: private List groupArray; 
private List> childArray;

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

复制代码 代码如下: groupArray = new ArrayList(); 
childArray = new ArrayList>(); 
 
groupArray.add("第一行"); 
groupArray.add("第二行"); 
 
List tempArray = new ArrayList(); 
tempArray.add("第一条"); 
tempArray.add("第二条"); 
tempArray.add("第三条"); 
 
for(int index = 0; 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));

希望本文所述对大家的Android程序设计有所帮助。

更多相关文章

  1. Android 5.0 自定义dialog 背景不透明解决方法
  2. 后台动态添加布局文件、控件与动态设置属性2
  3. 一个关于android中ListView的子控件中按钮事件的简单方法
  4. [Tools]Android Studio代码提示功能--Ctrl+Alt+Space(空格键)
  5. 使用Android常用控件与布局实现美观的登录页面
  6. Android学习心得(14) --- Android代码混淆(2)
  7. android 自定义xml属性
  8. 一网打尽__Android 开源代码合集(SwitchButton)
  9. Android自定义对话框的大小

随机推荐

  1. Android的Button监听
  2. android 设置WebView可触摸放大缩小
  3. Android打开微信小程序
  4. DataBinding 填坑总结
  5. android SQLiteDatabase之一
  6. AS gradle
  7. Android部分手机拍照上传返回为空处理
  8. Android App 多语言切换
  9. 基于ARouter的组件化开发
  10. (4.2.14)【android开源工具】常用网址