Android 之ExpandableListView幾個特殊的屬性

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.