登录

去注册

登录

注册

去登录

注册

Android多级选择控件,单级单选,单级多选,多级单选,多级多选

Tomzem   2018-11-06   收藏

MultiLevelSelect

:apple:多级选择控件,支持:单级单选,单级多选,多级单选,多级多选等

使用说明

依赖

在app/build.gradle中添加:

dependencies {
    implementation 'com.github.Tomzem:MultiLevelSelect:1.0.1'
}

还需要在Project的build.gradle中添加:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

调用

在activity_main.xml中只用写一个listView,其余什么都不需要:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_select"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.constraint.ConstraintLayout>

在java中,和正常的使用Adapter一样(默认情况多级多选):

        mLvMultiSelect = findViewById(R.id.lv_select);
        mItems = new ArrayList<>();
        initData();
				
        mAdapter = new ListViewAdapter(mLvMultiSelect, this, mItems, 0);
        mLvMultiSelect.setAdapter(mAdapter);
	//点击选中监听
        mAdapter.setCheckedChangeListener(new OnTreeNodeCheckedChangeListener() {
            @Override
            public void onCheckChange(String id, String name, int position, boolean isChecked) {
                List<Node> selectedNode = mAdapter.getSelectedNode();
                for (Node node : selectedNode){
                    Log.d(TAG, "selectedNode: " + node.getId());
                }
            }
        });

    private void initData() {
        List<Item> items = new ArrayList<>();
        items.add(new Item(0, -1, "菜单1"));
        items.add(new Item(1, -1, "菜单2"));
        items.add(new Item(2, -1, "菜单3"));
        items.add(new Item(3, -1, "菜单4"));

        items.add(new Item(4, 0, "菜单1-1"));
        items.add(new Item(5, 0, "菜单1-2"));
        items.add(new Item(6, 0, "菜单1-3"));

        items.add(new Item(7, 1, "菜单2-1"));
        items.add(new Item(8, 1, "菜单2-2"));
        items.add(new Item(9, 1, "菜单2-3"));

        items.add(new Item(10, 2, "菜单3-1"));
        items.add(new Item(11, 2, "菜单3-2"));
        items.add(new Item(12, 2, "菜单3-3"));

        items.add(new Item(14, 3, "菜单4-1"));
        items.add(new Item(15, 3, "菜单4-2"));
        items.add(new Item(16, 3, "菜单4-3"));

        mItems.addAll(items);
    }
}

实际效果

Adapter构造方法

ListViewAdapter(ListView listView, Context context, List<T> datas, int defaultExpandLevel)
ListViewAdapter(ListView listView, Context context, List<T> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand)      
ListViewAdapter(ListView listView, Context context, List<T> datas, int defaultExpandLevel,int layoutItem, int iconExpand, int iconNoExpand)

参数说明:

参数名称含 义
ListView listViewListView
Context context上下文
List datas数据源
int defaultExpandLevel默认展开级别(0)
int layoutItemItem自定义布局
int iconExpand展开图标
int iconNoExpand收缩图标

API说明

API 名称含 义
setSingleCheck(boolean singleCheck)设置是否单选(默认false)
getSelectedNode()获取所有选中节点
getSelectedChildNode()获取所有选中子节点(不含父节点)
addData(T t)添加单个item
addData(T t, int defaultExpandLevel)添加单个item,并设置展开级别
addData(List mlists)添加更多数据
addData(List mlists, int defaultExpandLevel)添加更多数据,并设置展开级别
addData(int index, List mlists)在index位置添加更多数据
addData(int index, List mlists, int defaultExpandLevel)在index位置添加更多数据,并设置展开级别
addDataAll(List mlists, int defaultExpandLevel)清空之前数据,重新加载数据,并设置展开级别
expandOrCollapse(int position)展开或收缩第position项(父级别)

注意事项

1.Item当中必须要有的两个属性:

   ***private int id; //当前itemID
   ***private int pid; //当前item父ID
   private String name;

2.item需要展示的文字:

    @Override
    public String toString() {
        return this.name;
    }

项目地址:https://github.com/Tomzem/MultiLevelSelect