Android 之ExpandableListView几个特殊的属性

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


先实例化 exListView

然后

  exListView.setAdapter(exlvAdapter);

  //遍历所有group,将所有项设置成默认展开

  int groupCount = exListView.getCount();

  for (int i=0; i<groupCount; i++) {

      exListView.expandGroup(i);

      };



2. 去掉ExpandableListView 默认的箭头


 用到ExpandableListView时有个箭头图标系统自带的在你自定义布局也不能去掉只要设置一个属性即可,如下:

 settingLists.setGroupIndicator(null);  ~~~~~~~~~~~~~~~~~此处就是设置自定义的箭头图标的。置空则没有了。



也可以自定义(但是位置还是在那个地方不推荐)如下:

首先,自定义一个expandablelistviewselector.xml文件,具体内容如下:
Java代码

<?xml version="1.0" encoding="utf-8"?>  

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

   <item android:state_expanded="true" android:drawable="@drawable/expandablelistviewindicatordown" />  

   <item android:drawable="@drawable/expandablelistviewindicator" />  

</selector>  

加一句代码如下:

settingLists.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));

大功告成

3. 将默认的箭头修改到右边显示:



1首先 ExpandableListView elistview;

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

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

/**
* 父类view
*/
   @Override
   public View getGroupView(int groupPosition, boolean isExpanded,
           View convertView, ViewGroup parent) {
       Log.i("zhaoxiong","parent view");
           LinearLayout parentLayout=(LinearLayout) View.inflate(context, R.layout.wowocoupons_parent_item, null);
           TextView parentTextView=(TextView) parentLayout.findViewById(R.id.parentitem);
           parentTextView.setText(parentlist.get(groupPosition));
           ImageView parentImageViw=(ImageView) parentLayout.findViewById(R.id.arrow);

//判断isExpanded就可以控制是按下还是关闭,同时更换图片
       if(isExpanded){
               parentImageViw.setBackgroundResource(R.drawable.arrow_down);
           }else{
               parentImageViw.setBackgroundResource(R.drawable.arrow_up);
           }


       return parentLayout;
   }

我再补充点 小点细节
如果需要实现 子条目点击事件 在适配器中复写

[java] view plain copy print ?
  1. publicboolean isChildSelectable(int groupPosition, int childPosition) {    

  2. // TODO Auto-generated method stub  

  3. returntrue;    

  4.        }    

public boolean isChildSelectable(int groupPosition, int childPosition) {  
            // TODO Auto-generated method stub  
            return true;  
        }  

和设置事件
[java] view plain copy print ?
  1. <BR>ExpandableListView().setOnChildClickListener(new OnChildClickListener() {<BR><BR>@Override<BR>publicboolean onChildClick(ExpandableListView parent, View v,<BR>int groupPosition, int childPosition, long id) {}<BR>  


ExpandableListView().setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {}



想让界面更加的人性化,就要实现很多的效果,比如只展开一个group,在点击下个group的同时,关闭之前的group

在一个ExpandableListView,如何实现只展开一个group,方法如下:
[java] view plain copy print ?
  1. final ExpandableListView mExpandableListView= getExpandableListView();  

  2.     mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {  


  3. @Override

  4. publicvoid onGroupExpand(int groupPosition) {  

  5. for(int i=0;i<group.size();i++){  

  6. if(groupPosition != i){  

  7.                        mExpandableListView.collapseGroup(i);  

  8.                    }  

  9.                }  


  10.        }  

  11.    });  


你可能感兴趣的:(android)