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后三行代码)。