实现效果图:
(1)在xml文件里加入
<ExpandableListView android:childDivider="#E8E8E8" android:groupIndicator="@null" android:divider="#E8E8E8" android:dividerHeight="1dp" android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent"/>
(2)在activity_main里加
expandableListView = (ExpandableListView) findViewById(R.id.elv); /* 1.1 创建一个adapter实例*/ MyExpandableListViewAdapter adapter = new MyExpandableListViewAdapter(getActivity()); /* 1. 设置适配器*/ expandableListView.setAdapter(adapter);
(3)新建MyExpandableListViewAdapter适配器
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ImageView; import android.widget.TextView; /** * Created by lenovo on 2018/5/11. */ public class MyExpandableListViewAdapter extends BaseExpandableListAdapter { private Context context; /* 布局填充器*/ private LayoutInflater inflater; private String[] group = new String[]{ "最新代金券", "最新投票", "最新回答"}; /* 构造,因为布局填充器填充布局需要使用到Context,通过构造来传递 */ public MyExpandableListViewAdapter (Context context){ this.context = context; inflater = LayoutInflater.from(context); } /*组数*/ @Override public int getGroupCount() { return group.length; } /* 指定组的子Item数*/ @Override public int getChildrenCount(int groupPosition) { if (groupPosition==0){ return 0; }else if(groupPosition==1){ return 1; }else { return 2; } } /*组数据*/ @Override public Object getGroup(int groupPosition) { return group[groupPosition]; } /*返回子选项的数据*/ @Override public Object getChild(int groupPosition, int childPosition) { return 0; } /*组ID*/ @Override public long getGroupId(int groupPosition) { return groupPosition; } /*子ID*/ @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } /*ID是否唯一*/ @Override public boolean hasStableIds() { return true; } /* 组选项的视图处理 */ @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { /* 填充布局*/ View view = inflater