Code Monkey home page Code Monkey logo

multilevelselect's Introduction

MultiLevelSelect

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

使用说明

依赖

在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 listView ListView
Context context 上下文
List datas 数据源
int defaultExpandLevel 默认展开级别(0)
int layoutItem Item自定义布局
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;
    }

multilevelselect's People

Contributors

tomzem avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.