1. 程式人生 > >關於expandListview去掉標題,圖示的在右邊顯示,,預設展開

關於expandListview去掉標題,圖示的在右邊顯示,,預設展開

1. 設定ExpandableListView 預設是展開的: 
先例項化exListView 然後 
  1. exListView.setAdapter(exlvAdapter);   
  2. //遍歷所有group,將所有項設定成預設展開
  3.  intgroupCount = exListView.getCount();   
  4. for (inti=0; i<groupCount; i++)  
  5.  {   
  6.        exListView.expandGroup(i);  
  7.  };   

2. 去掉ExpandableListView 預設的箭頭 
用到ExpandableListView時有個箭頭圖示系統自帶的在你自定義佈局也不能去掉只要設定一個屬性即可,如下: 

settingLists.setGroupIndicator(null); ~~~~~~~~~~~~~~~~~此處就是設定自定義的箭頭圖示的。置空則沒有了。  
也可以自定義(但是位置還是在那個地方不推薦)如下: 
首先,自定義一個expandablelistviewselector.xml檔案,具體內容如下: Java程式碼 
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <selectorxmlns:android="http://schemas.android.com/apk/res/android">
  3.      <itemandroid:state_expanded
    ="true"android:drawable="@drawable/expandablelistviewindicatordown"/>
  4.       <itemandroid:drawable="@drawable/expandablelistviewindicator"/>
  5.  </selector>

加一句程式碼如下: 

  1. settingLists.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));    
  2. 或xml設定:  
  3.     android:groupIndicator="@drawable/groupIndicator_selector"


大功告成 

3. 將預設的箭頭修改到右邊顯示: 

1首先ExpandableListViewelistview;  

elistview.setGroupIndicator(null);//將控制元件預設的左邊箭頭去掉,

 2在自定義的繼承自BaseExpandableListAdapter的adapter中有一個方法

  1. /** * 父類view */@Override
  2. ublic View getGroupView(intgroupPosition, booleanisExpanded, View convertView, ViewGroup parent)  
  3. { Log.i("zhaoxiong","parent view");   
  4.      LinearLayoutparentLayout=(LinearLayout) View.inflate(context, R.layout.wowocoupons_parent_item, null);   
  5.     TextViewparentTextView=(TextView)parentLayout.findViewById(R.id.parentitem);  
  6.     parentTextView.setText(parentlist.get(groupPosition));   
  7.     ImageViewparentImageViw=(ImageView) parentLayout.findViewById(R.id.arrow);   
  8.     //判斷isExpanded就可以控制是按下還是關閉,同時更換圖片
  9.    if(isExpanded){   
  10.        parentImageViw.setBackgroundResource(R.drawable.arrow_down);   
  11.     }else{   
  12.         parentImageViw.setBackgroundResource(R.drawable.arrow_up); }    
  13.      return parentLayout;  
  14. }  

expandablelistview響應onGroupClick監聽:設定expandablelistview.setOnGroupClickListener()
摺疊和展開事件,可以設定setOnGroupCollapseListener和setOnGroupExpandListener


ExpandableListView中包含多個group,想要展開一個group時,其他group都關閉:
  1. exList.setOnGroupExpandListener(new OnGroupExpandListener() {    
  2.         @Override
  3.         publicvoid onGroupExpand(int groupPosition) {    
  4.             for (int i = 0; i < getData().size(); i++) {    
  5.                 if (groupPosition != i) {    
  6.                     exList.collapseGroup(i);    
  7.                 }    
  8.             }    
  9.         }    
  10.     });  
3.expandablelistview的Group點選事件,onGroupClick的返回值false展開,true不展開
  1.          tt_list.setOnGroupClickListener(new OnGroupClickListener() {  
  2.             @Override
  3.             publicboolean onGroupClick(ExpandableListView parent, View v,  
  4.                     int groupPosition, long id) {  
  5.                 IsFlag=true;  
  6.                 if(adapter.getGroupData().get(groupPosition).getList().size()==1){  
  7.                     Bundle b=new Bundle();  
  8.                     b.putInt("saveIndex"0);  
  9. //                  b.putString("mac", mac);
  10. //                  b.putString("deviceId", mDeviceId);
  11.                     b.putSerializable("datalist", adapter.getGroupData().get(groupPosition).getList());  
  12.                     Intent i=new Intent(WappushBindingActivity.this,VideoPlayerActivity.class);  
  13.                     i.putExtras(b);   
  14.                     startActivity(i);  
  15.                 }  
  16. //              int groupCount = tt_list.getCount();   
  17. //              for (int i=0; i<groupCount; i++){   
  18. //                  if(i!=GroupPosition)
  19. //                      tt_list.collapseGroup(i);  
  20. //               }; 
  21. //              Log.v("xulongheng*WappushBind*tt_list", "onGroupClick:"+previousX+"/"+previousY);
  22.                 returntrue;   //預設為false,設為true時,點選事件不會展開Group
  23.             }  
  24.         });