转载请声明:http://blog.csdn.net/yoyo_newbie/article/details/50350975
源码下载地址:https://github.com/Sam474850601/CommonExpandableListAdapterDemo
或者引入地址: compile 'com.github.sam474850601:fastutils:1.0.3'
为了避免每次都要写重复代码的BaseExpandableListAdapter,所以写了通用的adapter
假设我要实现这个效果:
用通用的adapter这样的代码轻松搞定
//设置适配器 expandableListView.setAdapter(commonExpandableListAdapter = new CommonExpandableListAdapter, GroupData>(this,R.layout.adapter_child, R.layout.adapter_group ) { @Override protected void getChildView(ViewHolder holder, int groupPositon, int childPositon, boolean isLastChild, ChildData data) { TextView textView = holder.getView(R.id.childtxt);//孩子名字 textView.setText(data.childName); } @Override protected void getGroupView(ViewHolder holder, int groupPositon, boolean isExpanded, GroupData data) { TextView textView = holder.getView(R.id.grouptxt);//分组名字 ImageView arrowImage = holder.getView(R.id.groupIcon);//分组箭头 textView.setText(data.groupName); //根据分组是否展开设置自定义箭头方向 arrowImage.setImageResource(isExpanded?R.drawable.ic_arrow_expanded :R.drawable.ic_arrow_uexpanded); } }); expandableListView.setAdapter(commonExpandableListAdapter);
完整代码:
activity_main.xml
设置不要自带的箭头,下面自己添加箭头
xml version="1.0" encoding="utf-8"?>ic_arrow_expanded.9.pngxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listView" android:groupIndicator="@null" />
ic_arrow_uexpanded.9.png
孩子布局
adapter_child.xml
xml version="1.0" encoding="utf-8"?>分组布局xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/childtxt" />
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:padding="@dimen/activity_horizontal_margin" android:gravity="center_vertical" android:layout_height="wrap_content"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/groupIcon" android:src="@drawable/ic_arrow_uexpanded" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/grouptxt" android:text="分组" android:layout_marginLeft="@dimen/activity_horizontal_margin" />
MainActivity.java
public class MainActivity extends AppCompatActivity { private ExpandableListView expandableListView; private CommonExpandableListAdapter, GroupData> commonExpandableListAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); expandableListView = (ExpandableListView) findViewById(R.id.listView); //设置适配器 expandableListView.setAdapter(commonExpandableListAdapter = new CommonExpandableListAdapter , GroupData>(this,R.layout.adapter_child, R.layout.adapter_group ) { @Override protected void getChildView(ViewHolder holder, int groupPositon, int childPositon, boolean isLastChild, ChildData data) { TextView textView = holder.getView(R.id.childtxt);//孩子名字 textView.setText(data.childName); Log.e("test", data.childName); } @Override protected void getGroupView(ViewHolder holder, int groupPositon, boolean isExpanded, GroupData data) { TextView textView = holder.getView(R.id.grouptxt);//分组名字 ImageView arrowImage = holder.getView(R.id.groupIcon);//分组箭头 textView.setText(data.groupName); //根据分组是否展开设置自定义箭头方向 arrowImage.setImageResource(isExpanded?R.drawable.ic_arrow_expanded :R.drawable.ic_arrow_uexpanded); } }); expandableListView.setAdapter(commonExpandableListAdapter); //添加测试数据 addTestData(); } /** * 分组数据 */ class GroupData { String groupName; } /** * 孩子数据 */ class ChildData { String childName; } private void addTestData() { for (int i = 0; i < 5; i++) { GroupData groupData = new GroupData(); groupData.groupName = "分组-"+i; commonExpandableListAdapter.getGroupData().add(groupData); } for (int i = 0; i < commonExpandableListAdapter.getGroupCount(); i++) { List temp = new ArrayList<>(); for (int j = 0; j < 20; j++) { ChildData childData = new ChildData(); childData.childName = "第"+i+"组内容-"+j; temp.add(childData); } commonExpandableListAdapter.getChildrenData().add(temp); } for (int i = 0; i < commonExpandableListAdapter.getGroupCount(); i++) { Log.i("test", commonExpandableListAdapter.getGroupData().get(i).groupName); for (int j = 0; j < 20; j++) { //Log.e("test", commonExpandableListAdapter.getChildrenData().get(i).get(j).childName); } } commonExpandableListAdapter.notifyDataSetChanged(); } }
dependencies {compile 'com.github.sam474850601:fastutils:1.0.3'
}