万能ExpandableListAdapter适配器

转载请声明: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<ChildData, 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"?>
<RelativeLayout xmlns: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" >

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:groupIndicator="@null"
        />

</RelativeLayout>
ic_arrow_expanded.9.png



ic_arrow_uexpanded.9.png



孩子布局

adapter_child.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/childtxt"
        />
</LinearLayout>
分组布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/groupIcon"
        android:src="@drawable/ic_arrow_uexpanded"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/grouptxt"
        android:text="分组"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        />
</LinearLayout>


MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ExpandableListView expandableListView;
    private CommonExpandableListAdapter<ChildData, 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<ChildData, 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<ChildData>  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'
}






好的,这里提供一个简单的示例代码,使用一个 `List` 存储父列表项和子列表项的数据: ```java // 父列表项和子列表项的数据 List<Object> dataList = new ArrayList<>(); dataList.add("父列表1"); dataList.add("子列表1-1"); dataList.add("子列表1-2"); dataList.add("子列表1-3"); dataList.add("父列表2"); dataList.add("子列表2-1"); dataList.add("子列表2-2"); dataList.add("父列表3"); dataList.add("子列表3-1"); dataList.add("子列表3-2"); dataList.add("子列表3-3"); // 构造适配器 ListAdapter adapter = new ListAdapter(dataList); listView.setAdapter(adapter); ``` 其中,`ListAdapter` 是自定义的一个适配器类,继承自 `BaseAdapter`,可以根据自己的需求进行定制。在适配器中,需要实现以下几个方法: ```java @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { if (dataList.get(position) instanceof String) { // 父列表项 return 0; } else { // 子列表项 return 1; } } @Override public int getViewTypeCount() { return 2; } @Override public View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); if (type == 0) { // 构造父列表项视图 return parentView; } else { // 构造子列表项视图 return childView; } } ``` 其中,`getCount()` 返回列表项的个数,`getItem(int position)` 返回指定位置的数据,`getItemId(int position)` 返回指定位置的 ID,`getItemViewType(int position)` 返回指定位置的视图类型,这里通过判断数据类型来确定是父列表项还是子列表项,`getViewTypeCount()` 返回视图类型的个数,这里有两种视图类型,分别是父列表项和子列表项,`getView(int position, View convertView, ViewGroup parent)` 构造列表项的视图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值