zoukankan      html  css  js  c++  java
  • Android自定义ExpandableListView

    ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组可以单独展开。

    重要方法:

    expandGroup (int groupPos) ;//在分组列表视图中 展开一组,
    setSelectedGroup (int groupPosition) ;//设置选择指定的组。
    
    setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);//设置选择指定的子项。
    
    getPackedPositionGroup (long packedPosition);//返回所选择的组
    
    getPackedPositionForChild (int groupPosition, int childPosition) ;//返回所选择的子项
    
    getPackedPositionType (long packedPosition);//返回所选择项的类型(Child,Group)
    
    isGroupExpanded (int groupPosition);//判断此组是否展开

     

    expandableListView.setDivider();这个是设定每个Group之间的分割线。

      expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标。

      expandableListView.collapseGroup(int group); 将第group组收起

    ExpandableListAdapter

    一个接口,将基础数据链接到一个ExpandableListView。 此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。

    1.重要方法

        getChildId (int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。

        getChildrenCount (int groupPosition) 返回在指定Group的Child数目。

    如下案例:

     

     

       
       实现这样的效果需要自定义一个Adapter,自定义的Adapter继承BaseExpandableListAdapter,重写getGroupView和

    getChildView方法时实例化自己的布局文件就可以了。下面是实现代码:

    主布局文件  main.xml

    <?xmlversion="1.0"encoding="utf-8"?>
    
       <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"                                                                        
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="vertical"
           android:background="#ffffff"
           >
    
           <ExpandableListView
               android:id="@+id/elist"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:background="#ffffff"
            />
    
      </LinearLayout>

    子视图布局文件 child_layout.xml

     <?xmlversion="1.0"encoding="utf-8"?>
    
       <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"                                                                         
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="30dp"
            >
    
            <ImageView
                android:id="@+id/civ"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/ren"
                android:padding="5dp"
             />
    
            <TextView
                android:id="@+id/ctv"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:padding="5dp"
                android:textColor="#000000"
             />
    
       </LinearLayout> 

    分组视图布局文件 group_layout.xml

    <?xmlversion="1.0"encoding="utf-8"?>
    
        <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"                                                                     
            android:id="@+id/grlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                  android:id="@+id/gtv"
                  android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:paddingLeft="20dp"
                  android:textColor="#000000"
             />
    
            <ImageView
                  android:id="@+id/giv"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:layout_centerVertical="true"
                  android:paddingRight="10dp"
                  android:src="@drawable/jia"
             />
    
        </RelativeLayout> 

    自定义适配器 MyElistAdapter.java

      publicclass MyElistAdapter extends BaseExpandableListAdapter {                                                                                       
    
             // 分组数据
            private String[] group = { "A组", "B组", "C组", "D组" }; 
            private String[][] child = { { "A01", "A02", "A03" }, 
                    { "B01", "B02", "B03" }, { "C01", "C02", "C03" }, 
                    { "D04", "D05", "D06" } }; 
            private Context mContext; 
    
            public MyElistAdapter(Context mContext) { 
                    super(); 
                    this.mContext = mContext; 
            } 
    
            @Override 
            public int getGroupCount() { 
                return group.length; 
            } 
    
            @Override 
            public int getChildrenCount(int groupPosition) { 
                    return child[groupPosition].length; 
            } 
    
            @Override 
            public Object getGroup(int groupPosition) { 
                    return group[groupPosition]; 
            } 
    
            @Override 
            public Object getChild(int groupPosition, int childPosition) { 
                    return child[groupPosition][childPosition]; 
            } 
    
            @Override 
            public long getGroupId(int groupPosition) { 
                    return groupPosition; 
            } 
    
            @Override 
            public long getChildId(int groupPosition, int childPosition) { 
                    returnchildPosition; 
            } 
    
            @Override 
            public boolean hasStableIds() { 
                    return true; 
            } 
    
            @Override 
            public View getGroupView(int groupPosition, boolean isExpanded, 
            View convertView, ViewGroup parent) { 
                    // 实例化布局文件
                    RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext)     
                    .inflate(R.layout.group_layout, null); 
                     ImageView iv = (ImageView) glayout.findViewById(R.id.giv); 
                     // 判断分组是否展开,分别传入不同的图片资源
                    if (isExpanded) { 
                            iv.setImageResource(R.drawable.jian); 
                    } else { 
                            iv.setImageResource(R.drawable.jia); 
                    } 
                   TextView tv = (TextView) glayout.findViewById(R.id.gtv); 
                    tv.setText(this.getGroup(groupPosition).toString()); 
                    return glayout; 
             } 
    
             @Override 
            public View getChildView(int groupPosition, int childPosition, 
            boolean isLastChild, View convertView, ViewGroup parent) { 
                    // 实例化布局文件
                    LinearLayout clayout = (LinearLayout) LayoutInflater.from(mContext)              
                    .inflate(R.layout.child_layout, null); 
                    TextView tv = (TextView) clayout.findViewById(R.id.ctv); 
                    tv.setText(getChild(groupPosition, childPosition).toString()); 
                    return clayout; 
            } 
    
            @Override 
            public boolean isChildSelectable(int groupPosition, int childPosition) { 
                    return true; 
            } 
        } 

    MainActivity.java

    publicclass ExpandableTestActivity extends Activity {                                                                                                           
    
            private ExpandableListView elistview; 
    
            @Override 
            public void onCreate(Bundle savedInstanceState) { 
                    super.onCreate(savedInstanceState); 
                    setContentView(R.layout.main); 
                    elistview = (ExpandableListView) findViewById(R.id.elist);                               
                    //这里要把系统自带的图标去掉 
                    elistview.setGroupIndicator(null); 
                    elistview.setAdapter(new ElistAdapter(this)); 
                    // elistview.setChildDivider(null); 
                    // elistview.setDivider(null); 
             } 
    
        } 

     

     

  • 相关阅读:
    Redis 主从复制架构配置及原理
    IPTABLES详解(10):IPTABLES自定义链
    ipset
    Atitit 数据库核心技术index索引技术 btree hash lsm fulltxt目录1.1. HASH
    Atitit 存储引擎核心技术 总结目录1. 表的存储有三个文件:结构+数据+索引 12. 页式管理
    Atitit 为什么oracle这类大型数据库比mysql的性能机制目录1. 分区机制差别 11.1. Join算
    Atitit 存储与数据库性能调优流程目录1. 数据库出现性能瓶颈,对外表现有几个方面:
    ATITIT db perf enhs 数据库性能优化 目录 第一章 Cache类 1 第一节 查询cache 1 第二节 Update cache 2 第三节 内存表机制 零时表 2 第四节 雾
    Atitit 核心技术有哪些一般 目录 第一章 Rest调用交互 2 第二章 2 第三章 Cmd调用交互 2 第四章 2 第五章 爬虫技术 2 第一节 Httpclient 2 第二节 Html
    Atitit保证架构超前性 前瞻性 目录 第一章 为什么需要修改代码 1 第一节 业务增加功能 1 第二节 增加字段 1 第三节 增加表数据需要查询 修改 1 第四节 类库升级 1 第二章 简单抽象
  • 原文地址:https://www.cnblogs.com/xuewater/p/2631021.html
Copyright © 2011-2022 走看看