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;
   }

我再補充點 小點細節
如果需要實現 子條目點擊事件 在適配器中複寫

  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;  
        }  

和設置事件
  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,方法如下:
  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.    });  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章