广告
返回顶部
首页 > 资讯 > 移动开发 >Android中ExpandableListView的用法实例
  • 924
分享到

Android中ExpandableListView的用法实例

expandablelistviewAndroid 2022-06-06 10:06:33 924人浏览 独家记忆
摘要

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

本文实例讲述了Android中ExpandableListView的用法,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));

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

您可能感兴趣的文章:Android 关于ExpandableListView刷新问题的解决方法Android UI控件ExpandableListView基本用法详解Android改变ExpandableListView的indicator图标实现方法分享Android中ExpandableListView控件使用教程android使用ExpandableListView控件实现小说目录效果的例子Android ExpandableListView展开列表控件使用实例Android ExpandableListView长按事件的完美解决办法Android之IphoneTreeView带组指示器的ExpandableListView效果Android之带group指示器的ExpandableListView(自写)Android中ExpandableListView使用示例详解


--结束END--

本文标题: Android中ExpandableListView的用法实例

本文链接: https://www.lsjlt.com/news/26896.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作