ExpandableListView和ListView有很多相似之處,E可以實現qq那樣的下拉列表。如效果圖
佈局代碼:
<ExpandableListView
android:layout_width="match_parent"
android:id="@+id/expandable_lv"
android:listSelector="#ffbaaa"
android:background="#ffffff"
android:cacheColorHint="#c92c2c"
android:layout_height="match_parent">
</ExpandableListView>
group.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:id="@+id/group_tv"
android:gravity="center"
android:text="1122"
android:layout_height="48dp"/>
</LinearLayout>
child.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/child_iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:padding="6dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="111111111"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="111111111"/>
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="111111111"/>
</LinearLayout>
</LinearLayout>
MianActivity
public class MainActivity extends AppCompatActivity {
private LayoutInflater inflater;
private ExpandableListView exLv;
private List<StrBean> groupList;
private List<ChildBean> childList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exLv = (ExpandableListView) findViewById(R.id.expandable_lv);
inflater = LayoutInflater.from(MainActivity.this);
groupList = new ArrayList<>();
childList = new ArrayList<>();
initData();
// exLv.setBottom(10);
exLv.setAdapter(new MyAdapter(groupList,MainActivity.this));
}
private void initData() {
for (int i = 0; i < 5; i++) {
StrBean strBean = new StrBean();
strBean.setChildSize(8);
strBean.setStrName("深航"+i);
for (int j = 0; j < 8; j++) {
ChildBean childBean = new ChildBean();
childBean.setPrice(1+j+"萬");
childBean.setSecondPrice(1+"萬");
childBean.setTitle("商務卡");
childList.add(childBean);
}
strBean.setList(childList);
groupList.add(strBean);
}
}
}
最重要的 adpter
public class MyAdapter extends BaseExpandableListAdapter {
private List<StrBean> groupList;
Context context;
private Object childHolder;
public MyAdapter(List<StrBean> groupList, Context context) {
this.groupList = groupList;
this.context = context;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return groupList.get(groupPosition).getChildSize();
}
@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groupList.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = null;
GroupHolder groupHolder = null;
if (convertView != null) {
view = convertView;
groupHolder = (GroupHolder) view.getTag();
} else {
view = View.inflate(context, R.layout.group, null);
groupHolder = new GroupHolder();
groupHolder.exTv = (TextView) view.findViewById(R.id.group_tv);
view.setTag(groupHolder);
}
groupHolder.exTv.setText(groupList.get(groupPosition).getStrName());
return view;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = null;
ChildHolder childHolder = null;
if (convertView != null) {
view = convertView;
childHolder = (ChildHolder) view.getTag();
} else {
view = View.inflate(context, R.layout.child, null);
childHolder = new ChildHolder();
childHolder.iv = (ImageView) view.findViewById(R.id.child_iv);
childHolder.priceTv = (TextView) view.findViewById(R.id.tv1);
childHolder.scPriceTv = (TextView) view.findViewById(R.id.tv2);
childHolder.titleTv = (TextView) view.findViewById(R.id.tv3);
view.setTag(childHolder);
}
childHolder.scPriceTv.setText(groupList.get(groupPosition).getList().get(childPosition).getSecondPrice());
childHolder.titleTv.setText(groupList.get(groupPosition).getList().get(childPosition).getTitle());
childHolder.priceTv.setText(groupList.get(groupPosition).getList().get(childPosition).getPrice());
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupHolder{
TextView exTv;
}
class ChildHolder{
ImageView iv;
TextView priceTv;
TextView scPriceTv;
TextView titleTv;
}
}
前面介紹過ExpandableListView與ListView類似,所以ListView Adapter中存在的方法,ExpandableListView Adapter必定存在,只是Group和Child分別重寫了ListView Adapter中的方法,同時新增加了兩個方法,分別是hasStableIds() 和isChildSelectable(int groupPosition, int childPosition),所以,ExpandableListView Adapter中總共重寫了10個方法。
其中要注意hasStableIds() 和isChildSelectable(int groupPosition, int childPosition)這兩個方法。hasStableIds() 主要是用來判斷ExpandableListView內容id是否有效的(返回true or false),系統會跟據id來確定當前顯示哪條內容,也就是firstVisibleChild的位置。而isChildSelectable(int groupPosition, int childPosition)用來判斷某Group某個child是否可可選。我們可以添加條件控制某Group某個child可點或不可點擊。當不加任何條件直接返回false,所有的組的child均不可點擊。
關於expandablelistview的點擊事件,直接添加點擊事件即可。需要注意的是:
- 當設置setOnGroupClickListener監聽並讓其返回true時,所有Group消費點擊事件,事件均不能分發傳遞給child(換言之,設置setOnChildClickListener不起任何作用)。
2.默認設置完Group以及child,Group左邊會默認有以上下切換的圖標,假如你有強迫症可以通過mListView.setGroupIndicator(null)去除。
3.前面已經簡單說明了isChildSelectable(int groupPosition, int childPosition)方法的作用,所以當我們需要child可點擊時,必須將setOnGroupClickListener和isChildSelectable對應設置爲false和true。
4.說到這裏也許讀者會問,到底還是沒有說出圖一的做法。現在對圖一進行詳細的介紹。圖一主要是將Group設置爲不能收縮並且使其默認展開(即設置setOnGroupClickListener返回true,並且添加源碼中setAdapter後三行代碼)。