1. 設置ExpandableListView 默認是展開的:
先實例化exListView (ExpandableListView所有數據齊全后可用,否則報錯)
exListView.setAdapter(exlvAdapter); //遍歷所有group,將所有項設置成默認展開 intgroupCount = exListView.getCount(); for (inti=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>
android:groupIndicator="@drawable/groupIndicator_selector"
3. 將默認的箭頭修改到右邊顯示:
1)首先ExpandableListViewelistview;
elistview.setGroupIndicator(null);//將控件默認的左邊箭頭去掉,
2)在自定義的繼承自BaseExpandableListAdapter的adapter中有一個方法
/** * 父類view */ @Override ublic 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; }
expandablelistview響應onGroupClick監聽:
設置expandablelistview.setOnGroupClickListener()
折疊和展開事件,可以設置setOnGroupCollapseListener和setOnGroupExpandListener
ExpandableListView中包含多個group,想要展開一個group時,其他group都關閉:
3)expandablelistview的Group點擊事件,onGroupClick的返回值false展開,true不展開(如果配合默認展開,就會固定展開不收縮)
tt_list.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.putString("mac", mac); // b.putString("deviceId", mDeviceId); 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 } });