1. 設置ExpandableListView 默認是展開的:
先實例化 exListView
然後
exListView.setAdapter(exlvAdapter);
//遍歷所有group,將所有項設置成默認展開
int groupCount = exListView.getCount();
for (int i=0; i<groupCount; i++) {
exListView.expandGroup(i);
};
用到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;
}
我再補充點 小點細節
如果需要實現 子條目點擊事件 在適配器中複寫
publicboolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
returntrue;
}
public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; }
和設置事件
<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,方法如下:
final ExpandableListView mExpandableListView= getExpandableListView();
mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
publicvoid onGroupExpand(int groupPosition) {
for(int i=0;i<group.size();i++){
if(groupPosition != i){
mExpandableListView.collapseGroup(i);
}
}
}
});