码迷,mamicode.com
首页 > 其他好文 > 详细

ExpandableListView

时间:2015-07-28 18:08:28      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

1.设置ExpandableListView 默认是展开的: 

先实例化exListView 然后 

exListView.setAdapter(exlvAdapter);   
//遍历所有group,将所有项设置成默认展开  
 intgroupCount = exListView.getCount();   
for (inti=0; i<groupCount; i++)  
 {   
       exListView.expandGroup(i);  
 }; 

2.只展开一个组,关闭其他组,expandablelistview响应onGroupClick监听:设置expandablelistview.setOnGroupClickListener()
折叠和展开事件,可以设置setOnGroupCollapseListener和setOnGroupExpandListener

exListView .setOnGroupExpandListener(new OnGroupExpandListener() {
 
                    @Override
                    public void onGroupExpand(int groupPosition) {
                        for (int i = 0, count = onlineUsersListView
                                .getExpandableListAdapter().getGroupCount(); i < count; i++) {
                            if (groupPosition != i) {// 关闭其他分组
                                exListView.collapseGroup(i);
                            }
                        }
                    }
                });

 

3.默认展开第一个分组:

exListView.expandGroup(0);

4.去掉ExpandableListView 默认的箭头

exListView .setGroupIndicator(null); 

5.将默认的箭头修改到右边显示: 
(1)首先ExpandableListViewelistview;  

exListView.setGroupIndicator(null);//将控件默认的左边箭头去掉,

 (2)在自定义的继承自BaseExpandableListAdapter的adapter中有一个方法

 /** * 父类view */ @Override 
public View getGroupView(intgroupPosition, booleanisExpanded, View convertView, ViewGroup parent)
 { Log.i("zhaoxiong","parent view"); 
      LinearLayoutparentLayout=(LinearLayout) View.inflate(context, R.layout.wowocoupons_parent_item, null); 
     TextViewparentTextView=(TextView)parentLayout.findViewById(R.id.parentitem);
     parentTextView.setText(parentlist.get(groupPosition)); 
     ImageViewparentImageViw=(ImageView) parentLayout.findViewById(R.id.arrow); 
     //判断isExpanded就可以控制是按下还是关闭,同时更换图片
    if(isExpanded){ 
        parentImageViw.setBackgroundResource(R.drawable.arrow_down); 
     }else{ 
         parentImageViw.setBackgroundResource(R.drawable.arrow_up); }  
      return parentLayout;
 }

6.expandablelistview的Group点击事件,onGroupClick的返回值false展开,true不展开

exListView.setOnGroupClickListener(new OnGroupClickListener() {
            
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                IsFlag=true;

                if(adapter.getGroupData().get(groupPosition).getList().size()==1){
                    Bundle b=new Bundle();
                    b.putInt("saveIndex", 0);
                    b.putSerializable("datalist", adapter.getGroupData().get(groupPosition).getList());
                    Intent i=new Intent(WappushBindingActivity.this,VideoPlayerActivity.class);
                    i.putExtras(b);    
                    startActivity(i);
                }
//                int groupCount = tt_list.getCount();   
//                for (int i=0; i<groupCount; i++){   
//                    if(i!=GroupPosition)
//                        tt_list.collapseGroup(i);  
//                 }; 
//                Log.v("xulongheng*WappushBind*tt_list", "onGroupClick:"+previousX+"/"+previousY);
                return true;   //默认为false,设为true时,点击事件不会展开Group
            }
        });

7.demo:

效果:

技术分享

布局文件

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/LinearLayout1"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
    <ExpandableListView   
        android:id="@id/android:list"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        style="@style/myExpandableListView"  
        android:drawSelectorOnTop="false"  
        />  
    <TextView  
        android:id="@id/android:empty"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:text="no data"  
        />  
</LinearLayout>

 

第一级条目布局
group.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
    <TextView   
        android:id="@+id/groupTo"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:paddingLeft="40px"  
        android:paddingTop="3px"  
        android:paddingBottom="3px"  
        android:textSize="26sp"   
        android:text="No data"  
        />  
</LinearLayout>

 

第二级条目布局

child.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" >  
    <TextView   
        android:id="@+id/childTo"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:paddingLeft="50px"  
        android:paddingTop="5px"  
        android:paddingBottom="5px"  
        android:textSize="20sp"  
        android:text="No data"/>  
  
</LinearLayout>

在drawable-mdpi中定义my_expander_group.xml

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >  
    <item android:drawable="@drawable/ic_picture" ></item>  
</selector> 

MainActivity.java

package org.wwj.view;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import android.app.ExpandableListActivity;  
import android.os.Bundle;  
import android.widget.SimpleExpandableListAdapter;  
  
public class MainActivity extends ExpandableListActivity {  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        //定义一个List,该List对象为一级条目提供数据  
        List<Map<String, String>> groups = new ArrayList<Map<String,String>>();  
        Map<String, String> group1 = new HashMap<String, String>();  
        group1.put("group", "第一组");  
        Map<String, String> group2 = new HashMap<String, String>();  
        group2.put("group", "第二组");  
        groups.add(group1);  
        groups.add(group2);  
          
      //定义一个List,该List对象为第一个一级条目提供二级条目的数据  
        List<Map<String, String>> child1 = new ArrayList<Map<String,String>>();  
        Map<String, String> child1data1 = new HashMap<String, String>();  
        child1data1.put("child", "第一条");  
        child1.add(child1data1);  
        Map<String, String> child1data2 = new HashMap<String, String>();  
        child1data2.put("child", "第二条");  
        child1.add(child1data2);  
          
        //定义一个List,该List对象为第二个一级条目提供二级条目的数据  
        List<Map<String, String>> child2 = new ArrayList<Map<String,String>>();  
        Map<String, String> child2data1 = new HashMap<String, String>();  
        child2data1.put("child", "第三条");  
        child2.add(child2data1);  
        Map<String, String> child2data2 = new HashMap<String, String>();  
        child2data2.put("child", "第四条");  
        child2.add(child2data2);  
          
          
        //定义一个List,该List对象存储所有的二级条目的数据  
        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();  
        childs.add(child1);  
        childs.add(child2);  
          
          
        //生成一个SimpleExpandableListAdapter对象  
        //1.context  
        //2.一级条目的数据  
        //3.用来设置一级条目样式的布局文件  
        //4.指定一级条目数据的key  
        //5.指定一级条目数据显示控件的id  
        //6.指定二级条目的数据  
        //7.用来设置二级条目样式的布局文件  
        //8.指定二级条目数据的key  
        //9.指定二级条目数据显示控件的id  
        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this  
                , groups  
                , R.layout.group  
                , new String[]{"group"}  
                , new int[] {R.id.groupTo}  
                , childs  
                , R.layout.child  
                , new String[]{"child"}  
                , new int[] {R.id.childTo}  
                );  
      //将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity  
        setListAdapter(adapter);  
          
    }  
}  

参考链接:http://blog.csdn.net/wwj_748/article/details/7866584

ExpandableListView

标签:

原文地址:http://www.cnblogs.com/butterfly-clover/p/4683167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!