115.Android 简单的多级树结构列表+正常列表结合使用(RecyclerView) 两种方式实现(自定义adapter实现和使用BaseQuickAdapter库实现)

2023-10-28

 //一.第一种方式 自定义adapter实现:

 

 1.第一步 导入需要用到的依赖库:

//RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'
//RecyclerAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'
//刷新控件
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-28'

2.第二步 新建BaseTreeRecyclerViewAdapter类:

/**
 * @author CJF
 */
public abstract class BaseTreeRecyclerViewAdapter extends RecyclerView.Adapter {

    protected Context mContext;

    /**
     * 默认不展开
     */
    private int defaultExpandLevel = 0;

    /**
     * 展开与关闭的图片
     */
    private int iconExpand = -1, iconNoExpand = -1;

    /**
     * 存储所有的Node
     */
    protected List<NodeBean> mAllNodeBeans = new ArrayList<>();

    /**
     * 存储所有可见的Node
     */
    protected List<NodeBean> mNodeBeans = new ArrayList<>();

    protected LayoutInflater mInflater;

    /**
     * 点击的回调接口
     */
    private OnTreeClickListener treeClickListener;

    public interface OnTreeClickListener {
        void onClick(NodeBean nodeBean, int position);
    }

    /**
     * 接口监听
     *
     * @param treeClickListener
     */
    public void setOnTreeClickListener(OnTreeClickListener treeClickListener) {
        this.treeClickListener = treeClickListener;
    }

    /**
     * 接口调用
     *
     * @param nodeBean
     * @param position
     */
    public void onTreeClickListener(NodeBean nodeBean, int position) {
        if (null != treeClickListener) {
            treeClickListener.onClick(nodeBean, position);
        }
    }

    public BaseTreeRecyclerViewAdapter(RecyclerView recyclerView, Context context, List<NodeBean> datas,
                                       int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        mContext = context;
        this.defaultExpandLevel = defaultExpandLevel;
        this.iconExpand = iconExpand;
        this.iconNoExpand = iconNoExpand;

        for (NodeBean nodeBean : datas) {
            nodeBean.getChildren().clear();
            nodeBean.iconExpand = iconExpand;
            nodeBean.iconNoExpand = iconNoExpand;
        }

        /**
         * 对所有的Node进行排序
         */
        mAllNodeBeans = TreeHelper.getInstance().getSortedNodes(datas, defaultExpandLevel);

        /**
         * 过滤出可见的Node
         */
        mNodeBeans = TreeHelper.getInstance().filterVisibleNode(mAllNodeBeans);

        mInflater = LayoutInflater.from(context);
    }

    /**
     * @param mTree
     * @param context
     * @param datas
     * @param defaultExpandLevel 默认展开几级树
     */
    public BaseTreeRecyclerViewAdapter(RecyclerView mTree, Context context, List<NodeBean> datas, int defaultExpandLevel) {
        this(mTree, context, datas, defaultExpandLevel, -1, -1);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        NodeBean nodeBean = mNodeBeans.get(position);
        //        convertView = getConvertView(node, position, convertView, parent);
        // 设置内边距
        holder.itemView.setPadding(nodeBean.getLevel() * 50, 10, 10, 10);
        /**
         * 设置节点点击时,可以展开以及关闭,将事件继续往外公布
         */
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expandOrCollapse(position);
                //接口调用
                onTreeClickListener(mNodeBeans.get(position), position);
            }
        });
        onBindViewHolder(nodeBean, holder, position);
    }

    @Override
    public int getItemCount() {
        return mNodeBeans.size();
    }


    /**
     * 获取排序后所有节点
     *
     * @return
     */
    public List<NodeBean> getAllNodes() {
        if (mAllNodeBeans == null) {
            mAllNodeBeans = new ArrayList<NodeBean>();
        }
        return mAllNodeBeans;
    }

    /**
     * 获取所有选中节点
     *
     * @return
     */
    public List<NodeBean> getSelectedNode() {
        List<NodeBean> checks = new ArrayList<NodeBean>();
        for (int i = 0; i < mAllNodeBeans.size(); i++) {
            NodeBean n = mAllNodeBeans.get(i);
            if (n.isChecked()) {
                checks.add(n);
            }
        }
        return checks;
    }


    /**
     * 相应ListView的点击事件 展开或关闭某节点
     *
     * @param position
     */
    public void expandOrCollapse(int position) {
        NodeBean n = mNodeBeans.get(position);
        // 排除传入参数错误异常
        if (n != null) {
            if (!n.isLeaf()) {
                n.setExpand(!n.isExpand());
                mNodeBeans = TreeHelper.getInstance().filterVisibleNode(mAllNodeBeans);
                // 刷新视图
                notifyDataSetChanged();
            }
        }
    }

    /**
     * 设置多选
     *
     * @param nodeBean
     * @param checked
     */
    protected void setChecked(final NodeBean nodeBean, boolean checked) {
        nodeBean.setChecked(checked);
        setChildChecked(nodeBean, checked);
        if (nodeBean.getParent() != null) {
            setNodeParentChecked(nodeBean.getParent(), checked);
        }
        notifyDataSetChanged();
    }

    /**
     * 设置是否选中
     *
     * @param nodeBean
     * @param checked
     */
    public <T> void setChildChecked(NodeBean<T> nodeBean, boolean checked) {
        if (!nodeBean.isLeaf()) {
            nodeBean.setChecked(checked);
            for (NodeBean childrenNodeBean : nodeBean.getChildren()) {
                setChildChecked(childrenNodeBean, checked);
            }
        } else {
            nodeBean.setChecked(checked);
        }
    }

    private void setNodeParentChecked(NodeBean nodeBean, boolean checked) {
        if (checked) {
            nodeBean.setChecked(checked);
            if (nodeBean.getParent() != null) {
                setNodeParentChecked(nodeBean.getParent(), checked);
            }
        } else {
            List<NodeBean> childrens = nodeBean.getChildren();
            boolean isChecked = false;
            for (NodeBean children : childrens) {
                if (children.isChecked()) {
                    isChecked = true;
                }
            }
            //如果所有自节点都没有被选中 父节点也不选中
            if (!isChecked) {
                nodeBean.setChecked(checked);
            }
            if (nodeBean.getParent() != null) {
                setNodeParentChecked(nodeBean.getParent(), checked);
            }
        }
    }

    /**
     * 清除掉之前数据并刷新  重新添加
     *
     * @param mlists
     * @param defaultExpandLevel 默认展开几级列表
     */
    public void addDataAll(List<NodeBean> mlists, int defaultExpandLevel) {
        mAllNodeBeans.clear();
        addData(-1, mlists, defaultExpandLevel);
    }

    /**
     * 在指定位置添加数据并刷新 可指定刷新后显示层级
     *
     * @param index
     * @param mlists
     * @param defaultExpandLevel 默认展开几级列表
     */
    public void addData(int index, List<NodeBean> mlists, int defaultExpandLevel) {
        this.defaultExpandLevel = defaultExpandLevel;
        notifyData(index, mlists);
    }

    /**
     * 在指定位置添加数据并刷新
     *
     * @param index
     * @param mlists
     */
    public void addData(int index, List<NodeBean> mlists) {
        notifyData(index, mlists);
    }

    /**
     * 添加数据并刷新
     *
     * @param mlists
     */
    public void addData(List<NodeBean> mlists) {
        addData(mlists, defaultExpandLevel);
    }

    /**
     * 添加数据并刷新 可指定刷新后显示层级
     *
     * @param mlists
     * @param defaultExpandLevel
     */
    public void addData(List<NodeBean> mlists, int defaultExpandLevel) {
        this.defaultExpandLevel = defaultExpandLevel;
        notifyData(-1, mlists);
    }

    /**
     * 添加数据并刷新
     *
     * @param nodeBean
     */
    public void addData(NodeBean nodeBean) {
        addData(nodeBean, defaultExpandLevel);
    }

    /**
     * 添加数据并刷新 可指定刷新后显示层级
     *
     * @param nodeBean
     * @param defaultExpandLevel
     */
    public void addData(NodeBean nodeBean, int defaultExpandLevel) {
        List<NodeBean> nodeBeans = new ArrayList<>();
        nodeBeans.add(nodeBean);
        this.defaultExpandLevel = defaultExpandLevel;
        notifyData(-1, nodeBeans);
    }

    /**
     * 刷新数据
     *
     * @param index
     * @param mListNodeBeans
     */
    private void notifyData(int index, List<NodeBean> mListNodeBeans) {
        for (int i = 0; i < mListNodeBeans.size(); i++) {
            NodeBean nodeBean = mListNodeBeans.get(i);
            nodeBean.getChildren().clear();
            nodeBean.iconExpand = iconExpand;
            nodeBean.iconNoExpand = iconNoExpand;
        }
        for (int i = 0; i < mAllNodeBeans.size(); i++) {
            NodeBean nodeBean = mAllNodeBeans.get(i);
            nodeBean.getChildren().clear();
            // node.isNewAdd = false;
        }
        if (index != -1) {
            mAllNodeBeans.addAll(index, mListNodeBeans);
        } else {
            mAllNodeBeans.addAll(mListNodeBeans);
        }
        /**
         * 对所有的Node进行排序
         */
        mAllNodeBeans = TreeHelper.getInstance().getSortedNodes(mAllNodeBeans, defaultExpandLevel);
        /**
         * 过滤出可见的Node
         */
        mNodeBeans = TreeHelper.getInstance().filterVisibleNode(mAllNodeBeans);
        //刷新数据
        notifyDataSetChanged();
    }

    public abstract void onBindViewHolder(NodeBean nodeBean, RecyclerView.ViewHolder holder, final int position);

}

3.第三步 新建TreeRecyclerViewAdapter类:

/**
 * @author CJF
 */
public class TreeRecyclerViewAdapter extends BaseTreeRecyclerViewAdapter {

    //选中的回调接口
    private OnTreeCheckedChangeListener checkedChangeListener;

    public interface OnTreeCheckedChangeListener {
        void onCheckChange(NodeBean nodeBean, int position, boolean isChecked);
    }

    /**
     * 接口监听
     *
     * @param checkedChangeListener
     */
    public void setTreeCheckedChangeListener(OnTreeCheckedChangeListener checkedChangeListener) {
        this.checkedChangeListener = checkedChangeListener;
    }

    /**
     * 接口调用
     *
     * @param nodeBean
     * @param position
     * @param isChecked
     */
    public void onTreeCheckedChangeListener(NodeBean nodeBean, int position, boolean isChecked) {
        if (null != checkedChangeListener) {
            checkedChangeListener.onCheckChange(nodeBean, position, isChecked);
        }
    }

    public TreeRecyclerViewAdapter(RecyclerView recyclerView, Context context, List<NodeBean> datas, int defaultExpandLevel, int iconExpand, int iconNoExpand) {
        super(recyclerView, context, datas, defaultExpandLevel, iconExpand, iconNoExpand);
    }

    public TreeRecyclerViewAdapter(RecyclerView mTree, Context context, List<NodeBean> datas, int defaultExpandLevel) {
        super(mTree, context, datas, defaultExpandLevel);
    }

    @Override
    public void onBindViewHolder(final NodeBean nodeBean, final RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.mTreeHeaderName.setText(nodeBean.getName());
        if (nodeBean.getIcon() == -1) {
            viewHolder.mTreeHeaderExpand.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.mTreeHeaderExpand.setVisibility(View.VISIBLE);
            viewHolder.mTreeHeaderExpand.setImageResource(nodeBean.getIcon());
        }

        viewHolder.mTreeHeaderCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setChecked(nodeBean, viewHolder.mTreeHeaderCheckBox.isChecked());
                //接口调用
                onTreeCheckedChangeListener(nodeBean, position, viewHolder.mTreeHeaderCheckBox.isChecked());
            }
        });

        if (nodeBean.isChecked()) {
            viewHolder.mTreeHeaderCheckBox.setChecked(true);
        } else {
            viewHolder.mTreeHeaderCheckBox.setChecked(false);
        }

    }

    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.tree_header_item, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        inflate.setLayoutParams(params);
        return new ViewHolder(inflate);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private CheckBox mTreeHeaderCheckBox;
        private TextView mTreeHeaderName;
        private ImageView mTreeHeaderExpand;


        public ViewHolder(View itemView) {
            super(itemView);
            mTreeHeaderCheckBox = itemView.findViewById(R.id.mTreeHeaderCheckBox);
            mTreeHeaderName = itemView.findViewById(R.id.mTreeHeaderName);
            mTreeHeaderExpand = itemView.findViewById(R.id.mTreeHeaderExpand);
        }
    }

}

4.第四步 新建TreeHelper工具类:

/**
 * @author CJF
 */
public class TreeHelper {

    private volatile static TreeHelper treeHelper = null;

    public static TreeHelper getInstance() {
        if (null == treeHelper) {
            synchronized (TreeHelper.class) {
                if (null == treeHelper) {
                    treeHelper = new TreeHelper();
                }
            }
        }
        return treeHelper;
    }

    /**
     * 传入node 返回排序后的Node
     * 拿到用户传入的数据,转化为List<Node>以及设置Node间关系,然后根节点,从根往下遍历进行排序;
     *
     * @param datas
     * @param defaultExpandLevel 默认显示
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public List<NodeBean> getSortedNodes(List<NodeBean> datas, int defaultExpandLevel) {
        List<NodeBean> result = new ArrayList<NodeBean>();
        // 设置Node间父子关系
        List<NodeBean> nodeBeans = convetData2Node(datas);
        // 拿到根节点
        List<NodeBean> rootNodeBeans = getRootNodes(nodeBeans);
        // 排序以及设置Node间关系
        for (NodeBean nodeBean : rootNodeBeans) {
            addNode(result, nodeBean, defaultExpandLevel, 1);
        }
        return result;
    }

    /**
     * 过滤出所有可见的Node
     * 过滤Node的代码很简单,遍历所有的Node,只要是根节点或者父节点是展开状态就添加返回
     *
     * @param nodeBeans
     * @return
     */
    public List<NodeBean> filterVisibleNode(List<NodeBean> nodeBeans) {
        List<NodeBean> result = new ArrayList<NodeBean>();

        for (NodeBean nodeBean : nodeBeans) {
            // 如果为跟节点,或者上层目录为展开状态
            if (nodeBean.isRootNode() || nodeBean.isParentExpand()) {
                setNodeIcon(nodeBean);
                result.add(nodeBean);
            }
        }
        return result;
    }

    /**
     * 将我们的数据转化为树的节点
     * 设置Node间,父子关系;让每两个节点都比较一次,即可设置其中的关系
     */
    private List<NodeBean> convetData2Node(List<NodeBean> nodeBeans) {

        for (int i = 0; i < nodeBeans.size(); i++) {
            NodeBean n = nodeBeans.get(i);
            for (int j = i + 1; j < nodeBeans.size(); j++) {
                NodeBean m = nodeBeans.get(j);
                if (m.getPid() != null) {
                    //n时m的父节点
                    if (m.getPid().equals(n.getId())) {
                        n.getChildren().add(m);
                        m.setParent(n);
                        //m时n的父节点
                    } else if (m.getId().equals(n.getPid())) {
                        m.getChildren().add(n);
                        n.setParent(m);
                    }
                } else {
                    if (m.getPid() == n.getId()) {
                        n.getChildren().add(m);
                        m.setParent(n);
                    } else if (m.getId() == n.getPid()) {
                        m.getChildren().add(n);
                        n.setParent(m);
                    }
                }
            }
        }
        return nodeBeans;
    }

    /**
     * 获得根节点
     *
     * @param nodeBeans
     * @return
     */
    private List<NodeBean> getRootNodes(List<NodeBean> nodeBeans) {
        List<NodeBean> root = new ArrayList<NodeBean>();
        for (NodeBean nodeBean : nodeBeans) {
            if (nodeBean.isRootNode()) {
                root.add(nodeBean);
            }
        }
        return root;
    }

    /**
     * 把一个节点上的所有的内容都挂上去
     * 通过递归的方式,把一个节点上的所有的子节点等都按顺序放入
     */
    private <T> void addNode(List<NodeBean> nodeBeans, NodeBean<T> nodeBean, int defaultExpandLeval, int currentLevel) {
        nodeBeans.add(nodeBean);
        if (defaultExpandLeval >= currentLevel) {
            nodeBean.setExpand(true);
        }

        if (nodeBean.isLeaf()) {
            return;
        }
        for (int i = 0; i < nodeBean.getChildren().size(); i++) {
            addNode(nodeBeans, nodeBean.getChildren().get(i), defaultExpandLeval, currentLevel + 1);
        }
    }

    /**
     * 设置节点的图标
     *
     * @param nodeBean
     */
    private void setNodeIcon(NodeBean nodeBean) {
        if (nodeBean.getChildren().size() > 0 && nodeBean.isExpand()) {
            nodeBean.setIcon(nodeBean.iconExpand);
        } else if (nodeBean.getChildren().size() > 0 && !nodeBean.isExpand()) {
            nodeBean.setIcon(nodeBean.iconNoExpand);
        } else {
            nodeBean.setIcon(-1);
        }
    }

}

5.第五步 新建NodeBean类:

/**
 * @author CJF
 */
public class NodeBean<T> {

    /**
     * 当前节点id
     */
    private String id;

    /**
     * 父节点id
     */
    private String pid;

    /**
     * 节点数据实体类
     */
    private T data;

    /**
     * 设置开启 关闭的图片
     */
    public int iconExpand = -1, iconNoExpand = -1;

    /**
     * 节点名称
     */
    private String name;

    /**
     * 当前的级别
     */
    private int level;

    /**
     * 是否展开
     */
    private boolean isExpand = false;

    private int icon = -1;

    /**
     * 下一级的子Node
     */
    private List<NodeBean> children = new ArrayList<>();

    /**
     * 父Node
     */
    private NodeBean parent;

    /**
     * 是否被checked选中
     */
    private boolean isChecked;

    public NodeBean() {
    }

    public NodeBean(String id, String pid, String name) {
        this.id = id;
        this.pid = pid;
        this.name = name;
    }

    public NodeBean(String id, String pid, T data, String name) {
        this.id = id;
        this.pid = pid;
        this.data = data;
        this.name = name;
    }

    /**
     * 是否为根节点
     *
     * @return
     */
    public boolean isRootNode() {
        return parent == null;
    }

    /**
     * 判断父节点是否展开
     *
     * @return
     */
    public boolean isParentExpand() {
        if (parent == null) {
            return false;
        }
        return parent.isExpand();
    }

    /**
     * 是否是叶子节点
     *
     * @return
     */
    public boolean isLeaf() {
        return children.size() == 0;
    }

    /**
     * 获取当前的级别level
     */
    public int getLevel() {
        return parent == null ? 0 : parent.getLevel() + 1;
    }

    /**
     * 设置展开
     *
     * @param isExpand
     */
    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
        if (!isExpand) {
            for (NodeBean nodeBean : children) {
                nodeBean.setExpand(isExpand);
            }
        }
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getIconExpand() {
        return iconExpand;
    }

    public void setIconExpand(int iconExpand) {
        this.iconExpand = iconExpand;
    }

    public int getIconNoExpand() {
        return iconNoExpand;
    }

    public void setIconNoExpand(int iconNoExpand) {
        this.iconNoExpand = iconNoExpand;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public List<NodeBean> getChildren() {
        return children;
    }

    public void setChildren(List<NodeBean> children) {
        this.children = children;
    }

    public NodeBean getParent() {
        return parent;
    }

    public void setParent(NodeBean parent) {
        this.parent = parent;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

}

6.第六步 新建TreeHeaderView自定义View类:

/**
 * @author CJF
 */
public class TreeHeaderView extends LinearLayout {
    private TreeRecyclerViewAdapter adapter;
    private List<NodeBean> dataList = new ArrayList<>();

    public TreeHeaderView(Context context) {
        super(context);
        initView(context);
        initData();
    }

    public TreeHeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initData();
    }

    public TreeHeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initData();
    }

    private void initView(Context context) {
        //树结构布局
        View headerLayout = LayoutInflater.from(context).inflate(R.layout.tree_header_layout, this, true);
//        LinearLayoutManager manager = new LinearLayoutManager(context);
        //禁止滑动  布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(context) {
            //禁止竖向滑动 RecyclerView 为垂直状态(VERTICAL)
            @Override
            public boolean canScrollVertically() {
                return false;
            }
            //禁止横向滑动 RecyclerView 为水平状态(HORIZONTAL)
            /*@Override
            public boolean canScrollHorizontally() {
                return false;
            }*/
        };

        RecyclerView mTreeHeaderViewRecy = headerLayout.findViewById(R.id.mTreeHeaderViewRecy);
        mTreeHeaderViewRecy.setLayoutManager(manager);

        //第一个参数  ListView & RecyclerView
        //第二个参数  上下文
        //第三个参数  数据集
        //第四个参数  默认展开层级数 0为不展开
        //第五个参数  展开的图标
        //第六个参数  闭合的图标
        adapter = new TreeRecyclerViewAdapter(mTreeHeaderViewRecy, context, dataList, 0, R.drawable.svg_expand_more, R.drawable.svg_navigate_next);

        mTreeHeaderViewRecy.setAdapter(adapter);
    }

    private void initData() {
        //根节点0 1 2
        dataList.add(new NodeBean<>("0", "-1", "A 1级节点"));
        dataList.add(new NodeBean<>("1", "-1", "B 1级节点"));
        dataList.add(new NodeBean<>("2", "-1", "C 1级节点"));

//        //根节点1的二级节点
        dataList.add(new NodeBean<>("3", "0", "A 2级节点"));
        dataList.add(new NodeBean<>("4", "0", "A 2级节点"));
        dataList.add(new NodeBean<>("5", "0", "A 2级节点"));

        //根节点2的二级节点
        dataList.add(new NodeBean<>("6", "1", "B 2级节点"));
        dataList.add(new NodeBean<>("7", "1", "B 2级节点"));
        dataList.add(new NodeBean<>("8", "1", "B 2级节点"));

        //根节点3的二级节点
        dataList.add(new NodeBean<>("9", "2", "C 2级节点"));
        dataList.add(new NodeBean<>("10", "2", "C 2级节点"));
        dataList.add(new NodeBean<>("11", "2", "C 2级节点"));

        //三级节点
        dataList.add(new NodeBean<>("12", "3", "A 3级节点"));
        dataList.add(new NodeBean<>("13", "3", "A 3级节点"));
        dataList.add(new NodeBean<>("14", "3", "A 3级节点"));

        dataList.add(new NodeBean<>("15", "4", "A 3级节点"));
        dataList.add(new NodeBean<>("16", "4", "A 3级节点"));
        dataList.add(new NodeBean<>("17", "4", "A 3级节点"));

        dataList.add(new NodeBean<>("18", "5", "A 3级节点"));
        dataList.add(new NodeBean<>("19", "5", "A 3级节点"));
        dataList.add(new NodeBean<>("20", "5", "A 3级节点"));

        //四级节点
        dataList.add(new NodeBean<>("21", "12", "A 4级节点"));

        //五级节点
        dataList.add(new NodeBean<>("22", "21", "A 5级节点"));
        //六级节点
        dataList.add(new NodeBean<>("23", "22", "A 6级节点"));
        //七级节点
        dataList.add(new NodeBean<>("24", "23", "A 7级节点"));
        //八级节点
        dataList.add(new NodeBean<>("25", "24", "A 8级节点"));


        adapter.addData(dataList);

//        //获取所有节点
//        final List<Node> allNodes = mAdapter.getAllNodes();
//        for (Node allNode : allNodes) {
//            Log.e("TAG1231", "onCreate: " + allNode.getName());
//        }

        //选中状态监听
        adapter.setTreeCheckedChangeListener(new TreeRecyclerViewAdapter.OnTreeCheckedChangeListener() {
            @Override
            public void onCheckChange(NodeBean nodeBean, int position, boolean isChecked) {
                Log.e("TAG1231", "onCheckChange position: " + nodeBean.getName());

                //获取所有选中节点
                List<NodeBean> selectedNodeBean = adapter.getSelectedNode();

                StringBuilder builder = new StringBuilder();
                for (NodeBean n : selectedNodeBean) {
                    builder.append(n.getName()).append("\n");
                }
                Log.e("TAG1231", "builder: " + builder);
            }
        });

        //总item点击状态监听
        adapter.setOnTreeClickListener(new BaseTreeRecyclerViewAdapter.OnTreeClickListener() {
            @Override
            public void onClick(NodeBean nodeBean, int position) {
                Log.e("TAG1231", "setOnTreeClickListener: " + nodeBean.getName());
            }
        });
    }

}

7.第七步 新建TreeListAdapter适配器类适配普通列表数据:

/**
 * @author CJF
 */
public class TreeListAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public TreeListAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.mOppoSdkTextPosition, String.valueOf(helper.getAdapterPosition()));
        helper.setText(R.id.mOppoSdkText, item);
    }

}

8.第八步 新建TreeListActivity页面放入头布局和普通数据:

/**
 * @author CJF
 */
public class TreeListActivity extends AppCompatActivity {
    private final LinearLayoutManager manager = new LinearLayoutManager(this);
    private final TreeListAdapter adapter=new TreeListAdapter(R.layout.sdk_item);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_list);
        
        RecyclerView mTreeListRecy = findViewById(R.id.mTreeListRecy);
        mTreeListRecy.setLayoutManager(manager);
        mTreeListRecy.setAdapter(adapter);
        TreeHeaderView treeHeaderView = new TreeHeaderView(this);
        adapter.addHeaderView(treeHeaderView);

        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("数据"+i);
        }
        adapter.addData(list);
    }

}

//manifest别忘记注册activity:

<activity
    android:name=".phone.activity.TreeListActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden"
    tools:ignore="LockedOrientationActivity" />

 9.第九步 各个xml布局文件:

//activity_tree_list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/color_white"
    android:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlEnableLoadMore="true"
        app:srlEnableRefresh="true">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/mTreeListRecy"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <com.scwang.smartrefresh.layout.footer.FalsifyFooter
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50" />

        <com.scwang.smartrefresh.layout.header.FalsifyHeader
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_50" />

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</LinearLayout>

//sdk_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_5"
    android:layout_marginLeft="@dimen/dp_20"
    android:layout_marginRight="@dimen/dp_20"
    android:layout_marginTop="@dimen/dp_5"
    android:background="@drawable/selector_common_item"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/mOppoSdkTextPosition"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@drawable/selector_common_item"
        android:gravity="center"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="0"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

    <TextView
        android:id="@+id/mOppoSdkText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:background="@drawable/selector_common_item"
        android:gravity="left|center_vertical"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="text"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

</LinearLayout>

//tree_header_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_white"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mTreeHeaderViewRecy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

//tree_header_item:

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_5"
    android:layout_marginLeft="@dimen/dp_20"
    android:layout_marginRight="@dimen/dp_20"
    android:layout_marginTop="@dimen/dp_5"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:src="@drawable/svg_navigate_next"
        android:id="@+id/mTreeHeaderExpand"
        android:layout_width="@dimen/dp_50"
        android:layout_height="@dimen/dp_50"
        android:gravity="center"
        android:padding="@dimen/dp_10" />

    <CheckBox
        android:id="@+id/mTreeHeaderCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/mTreeHeaderName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="left|center_vertical"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="text"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

</LinearLayout>

10.第十步 两个svg的xml图片文件:

//svg_expand_more:

<vector android:alpha="0.85" android:autoMirrored="true"
    android:height="24dp" android:tint="#3B76EC"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#ffffffff" android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z"/>
</vector>

//svg_navigate_next:

<vector android:alpha="0.85" android:autoMirrored="true"
    android:height="24dp" android:tint="#3B76EC"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#ffffffff" android:pathData="M10,6L8.59,7.41 13.17,12l-4.58,4.59L10,18l6,-6z"/>
</vector>

//二.第二种方式使用BaseQuickAdapter库实现无限层级树结构,代码更简便:

 1.第一步 导入依赖库:

//RecyclerView
implementation 'com.android.support:recyclerview-v7:28.0.0'

//RecyclerAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'

2.第二步 新建Tree2ListActivity页面代码如下:

/**
 * @author CJF
 */
public class Tree2ListActivity extends AppCompatActivity {
    private final LinearLayoutManager manager = new LinearLayoutManager(this);
    private final TreeListAdapter adapter = new TreeListAdapter(R.layout.sdk_item);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_list);
        RecyclerView mTreeListRecy = findViewById(R.id.mTreeListRecy);
        mTreeListRecy.setLayoutManager(manager);
        mTreeListRecy.setAdapter(adapter);
        Tree2HeaderView tree2HeaderView = new Tree2HeaderView(this);
        adapter.addHeaderView(tree2HeaderView);

        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            list.add("数据" + i);
        }
        adapter.addData(list);
    }
   

//manifest注册:

<activity
    android:name=".phone.activity.Tree2ListActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="stateHidden"
    tools:ignore="LockedOrientationActivity" />

3.第三步 新建TreeListAdapter普通数据适配器:

/**
 * @author CJF
 */
public class TreeListAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public TreeListAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.mOppoSdkTextPosition, String.valueOf(helper.getAdapterPosition()));
        helper.setText(R.id.mOppoSdkText, item);
    }

}

4.第四步 新建Tree2HeaderView 自定义view 头布局:

/**
 * @author CJF
 */
public class Tree2HeaderView extends LinearLayout implements BaseQuickAdapter.OnItemClickListener {
    private final Tree2HeaderAdapter adapter=new Tree2HeaderAdapter(R.layout.tree_header_item);
    private List<NodeBean> mAllNodeBeans;
    private List<NodeBean> mNodeBeans;

    public Tree2HeaderView(Context context) {
        super(context);
        initView(context);
        initData();
    }

    public Tree2HeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initData();
    }

    public Tree2HeaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initData();
    }

    private void initView(Context context) {
        //树结构布局
        View headerLayout = LayoutInflater.from(context).inflate(R.layout.tree_header_layout, this, true);
//        LinearLayoutManager manager = new LinearLayoutManager(context);
        //禁止滑动  布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(context) {
            //禁止竖向滑动 RecyclerView 为垂直状态(VERTICAL)
            @Override
            public boolean canScrollVertically() {
                return false;
            }
            //禁止横向滑动 RecyclerView 为水平状态(HORIZONTAL)
            /*@Override
            public boolean canScrollHorizontally() {
                return false;
            }*/
        };

        RecyclerView mTreeHeaderViewRecy = headerLayout.findViewById(R.id.mTreeHeaderViewRecy);
        mTreeHeaderViewRecy.setLayoutManager(manager);
        mTreeHeaderViewRecy.setAdapter(adapter);

        adapter.setOnItemClickListener(this);
    }

    private void initData() {
        List<NodeBean> maxDataList = new ArrayList<>();
        //根节点0 1 2
        maxDataList.add(new NodeBean<>("0", "-1", "A 1级节点"));
        maxDataList.add(new NodeBean<>("1", "-1", "B 1级节点"));
        maxDataList.add(new NodeBean<>("2", "-1", "C 1级节点"));

//        //根节点1的二级节点
        maxDataList.add(new NodeBean<>("3", "0", "A 2级节点"));
        maxDataList.add(new NodeBean<>("4", "0", "A 2级节点"));
        maxDataList.add(new NodeBean<>("5", "0", "A 2级节点"));

        //根节点2的二级节点
        maxDataList.add(new NodeBean<>("6", "1", "B 2级节点"));
        maxDataList.add(new NodeBean<>("7", "1", "B 2级节点"));
        maxDataList.add(new NodeBean<>("8", "1", "B 2级节点"));

        //根节点3的二级节点
        maxDataList.add(new NodeBean<>("9", "2", "C 2级节点"));
        maxDataList.add(new NodeBean<>("10", "2", "C 2级节点"));
        maxDataList.add(new NodeBean<>("11", "2", "C 2级节点"));

        //三级节点
        maxDataList.add(new NodeBean<>("12", "3", "A 3级节点"));
        maxDataList.add(new NodeBean<>("13", "3", "A 3级节点"));
        maxDataList.add(new NodeBean<>("14", "3", "A 3级节点"));

        maxDataList.add(new NodeBean<>("15", "4", "A 3级节点"));
        maxDataList.add(new NodeBean<>("16", "4", "A 3级节点"));
        maxDataList.add(new NodeBean<>("17", "4", "A 3级节点"));

        maxDataList.add(new NodeBean<>("18", "5", "A 3级节点"));
        maxDataList.add(new NodeBean<>("19", "5", "A 3级节点"));
        maxDataList.add(new NodeBean<>("20", "5", "A 3级节点"));

        //四级节点
        maxDataList.add(new NodeBean<>("21", "12", "A 4级节点"));

        //五级节点
        maxDataList.add(new NodeBean<>("22", "21", "A 5级节点"));
        //六级节点
        maxDataList.add(new NodeBean<>("23", "22", "A 6级节点"));
        //七级节点
        maxDataList.add(new NodeBean<>("24", "23", "A 7级节点"));
        //八级节点
        maxDataList.add(new NodeBean<>("25", "24", "A 8级节点"));


        maxDataList.add(new NodeBean<>("26", "25", "A 9级节点"));
        maxDataList.add(new NodeBean<>("27", "26", "A 10级节点"));

        maxDataList.add(new NodeBean<>("28", "27", "A 11级节点"));

        maxDataList.add(new NodeBean<>("29", "28", "A 12级节点"));

        maxDataList.add(new NodeBean<>("30", "29", "A 13级节点"));

        maxDataList.add(new NodeBean<>("31", "30", "A 14级节点"));

        maxDataList.add(new NodeBean<>("32", "31", "A 15级节点"));

        maxDataList.add(new NodeBean<>("33", "32", "A 16级节点"));

        maxDataList.add(new NodeBean<>("34", "33", "A 17级节点"));


        maxDataList.add(new NodeBean<>("35", "-1", "D 1级节点"));
        maxDataList.add(new NodeBean<>("36", "-1", "E 1级节点"));
        maxDataList.add(new NodeBean<>("37", "-1", "F 1级节点"));

        maxDataList.add(new NodeBean<>("38", "35", "D 2级节点"));
        maxDataList.add(new NodeBean<>("39", "36", "E 2级节点"));
        maxDataList.add(new NodeBean<>("40", "37", "F 2级节点"));



        /**
         * 对所有的Node进行排序
         */
        mAllNodeBeans = TreeHelper.getInstance().getSortedNodes(maxDataList, 0);

        /**
         * 过滤出可见的Node
         */
        mNodeBeans = TreeHelper.getInstance().filterVisibleNode(mAllNodeBeans);
        adapter.addData(mNodeBeans);


//        //获取所有节点
//        final List<Node> allNodes = mAdapter.getAllNodes();
//        for (Node allNode : allNodes) {
//            Log.e("TAG1231", "onCreate: " + allNode.getName());
//        }

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        expandOrCollapse(adapter,position);
    }

    /**
     * 相应ListView的点击事件 展开或关闭某节点
     *
     * @param position
     */
    public void expandOrCollapse(BaseQuickAdapter adapter,int position) {
        NodeBean n = (NodeBean) adapter.getData().get(position);
        // 排除传入参数错误异常
        if (n != null) {
            if (!n.isLeaf()) {
                n.setExpand(!n.isExpand());
                mNodeBeans = TreeHelper.getInstance().filterVisibleNode(mAllNodeBeans);
                //替换视图
                adapter.replaceData(mNodeBeans);
            }
        }
    }

}

5.第五步 新建Tree2HeaderAdapter 适配器,适配树结构数据:

public class Tree2HeaderAdapter extends BaseQuickAdapter<NodeBean, BaseViewHolder> {

    public Tree2HeaderAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, NodeBean item) {
        //最大展开8级宽度距离
        int level = Math.min(item.getLevel(), 8);
        // 设置内边距
        helper.itemView.setPadding(level * 50, 10, 10, 10);
        //设置文字内容
        helper.setText(R.id.mTreeHeaderName, item.getName());
        //设置展开与收起的图片
        helper.setImageResource(R.id.mTreeHeaderExpand, item.isExpand() ? R.drawable.svg_expand_more : R.drawable.svg_navigate_next);
        //图片显示隐藏
        helper.setVisible(R.id.mTreeHeaderExpand, item.getChildren().size() > 0);
    }

}

6.第六步 新建TreeHelper工具类:

/**
 * @author CJF
 */
public class TreeHelper {

    private volatile static TreeHelper treeHelper = null;

    public static TreeHelper getInstance() {
        if (null == treeHelper) {
            synchronized (TreeHelper.class) {
                if (null == treeHelper) {
                    treeHelper = new TreeHelper();
                }
            }
        }
        return treeHelper;
    }

    /**
     * 传入node 返回排序后的Node
     * 拿到用户传入的数据,转化为List<Node>以及设置Node间关系,然后根节点,从根往下遍历进行排序;
     *
     * @param datas
     * @param defaultExpandLevel 默认显示
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public List<NodeBean> getSortedNodes(List<NodeBean> datas, int defaultExpandLevel) {
        List<NodeBean> result = new ArrayList<NodeBean>();
        // 设置Node间父子关系
        List<NodeBean> nodeBeans = convetData2Node(datas);
        // 拿到根节点
        List<NodeBean> rootNodeBeans = getRootNodes(nodeBeans);
        // 排序以及设置Node间关系
        for (NodeBean nodeBean : rootNodeBeans) {
            addNode(result, nodeBean, defaultExpandLevel, 1);
        }
        return result;
    }

    /**
     * 过滤出所有可见的Node
     * 过滤Node的代码很简单,遍历所有的Node,只要是根节点或者父节点是展开状态就添加返回
     *
     * @param nodeBeans
     * @return
     */
    public List<NodeBean> filterVisibleNode(List<NodeBean> nodeBeans) {
        List<NodeBean> result = new ArrayList<NodeBean>();

        for (NodeBean nodeBean : nodeBeans) {
            // 如果为跟节点,或者上层目录为展开状态
            if (nodeBean.isRootNode() || nodeBean.isParentExpand()) {
                setNodeIcon(nodeBean);
                result.add(nodeBean);
            }
        }
        return result;
    }

    /**
     * 将我们的数据转化为树的节点
     * 设置Node间,父子关系;让每两个节点都比较一次,即可设置其中的关系
     */
    public List<NodeBean> convetData2Node(List<NodeBean> nodeBeans) {

        for (int i = 0; i < nodeBeans.size(); i++) {
            NodeBean n = nodeBeans.get(i);
            for (int j = i + 1; j < nodeBeans.size(); j++) {
                NodeBean m = nodeBeans.get(j);
                if (m.getPid() != null) {
                    //n时m的父节点
                    if (m.getPid().equals(n.getId())) {
                        n.getChildren().add(m);
                        m.setParent(n);
                        //m时n的父节点
                    } else if (m.getId().equals(n.getPid())) {
                        m.getChildren().add(n);
                        n.setParent(m);
                    }
                } else {
                    if (m.getPid() == n.getId()) {
                        n.getChildren().add(m);
                        m.setParent(n);
                    } else if (m.getId() == n.getPid()) {
                        m.getChildren().add(n);
                        n.setParent(m);
                    }
                }
            }
        }
        return nodeBeans;
    }

    /**
     * 获得根节点
     *
     * @param nodeBeans
     * @return
     */
    public List<NodeBean> getRootNodes(List<NodeBean> nodeBeans) {
        List<NodeBean> root = new ArrayList<NodeBean>();
        for (NodeBean nodeBean : nodeBeans) {
            if (nodeBean.isRootNode()) {
                root.add(nodeBean);
            }
        }
        return root;
    }

    /**
     * 把一个节点上的所有的内容都挂上去
     * 通过递归的方式,把一个节点上的所有的子节点等都按顺序放入
     */
    public  <T> void addNode(List<NodeBean> nodeBeans, NodeBean<T> nodeBean, int defaultExpandLeval, int currentLevel) {
        nodeBeans.add(nodeBean);
        if (defaultExpandLeval >= currentLevel) {
            nodeBean.setExpand(true);
        }

        if (nodeBean.isLeaf()) {
            return;
        }
        for (int i = 0; i < nodeBean.getChildren().size(); i++) {
            addNode(nodeBeans, nodeBean.getChildren().get(i), defaultExpandLeval, currentLevel + 1);
        }
    }

    /**
     * 设置节点的图标
     *
     * @param nodeBean
     */
    private void setNodeIcon(NodeBean nodeBean) {
        if (nodeBean.getChildren().size() > 0 && nodeBean.isExpand()) {
            nodeBean.setIcon(nodeBean.iconExpand);
        } else if (nodeBean.getChildren().size() > 0 && !nodeBean.isExpand()) {
            nodeBean.setIcon(nodeBean.iconNoExpand);
        } else {
            nodeBean.setIcon(-1);
        }
    }

}

7.第七步 新建NodeBean类:

/**
 * @author CJF
 */
public class NodeBean<T> {

    /**
     * 当前节点id
     */
    private String id;

    /**
     * 父节点id
     */
    private String pid;

    /**
     * 节点数据实体类
     */
    private T data;

    /**
     * 设置开启 关闭的图片
     */
    public int iconExpand = -1, iconNoExpand = -1;

    /**
     * 节点名称
     */
    private String name;

    /**
     * 当前的级别
     */
    private int level;

    /**
     * 是否展开
     */
    private boolean isExpand = false;

    private int icon = -1;

    /**
     * 下一级的子Node
     */
    private List<NodeBean> children = new ArrayList<>();

    /**
     * 父Node
     */
    private NodeBean parent;

    /**
     * 是否被checked选中
     */
    private boolean isChecked;

    public NodeBean() {
    }

    public NodeBean(String id, String pid, String name) {
        this.id = id;
        this.pid = pid;
        this.name = name;
    }

    public NodeBean(String id, String pid, T data, String name) {
        this.id = id;
        this.pid = pid;
        this.data = data;
        this.name = name;
    }

    /**
     * 是否为根节点
     *
     * @return
     */
    public boolean isRootNode() {
        return parent == null;
    }

    /**
     * 判断父节点是否展开
     *
     * @return
     */
    public boolean isParentExpand() {
        if (parent == null) {
            return false;
        }
        return parent.isExpand();
    }

    /**
     * 是否是叶子节点
     *
     * @return
     */
    public boolean isLeaf() {
        return children.size() == 0;
    }

    /**
     * 获取当前的级别level
     */
    public int getLevel() {
        return parent == null ? 0 : parent.getLevel() + 1;
    }

    /**
     * 设置展开
     *
     * @param isExpand
     */
    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
        if (!isExpand) {
            for (NodeBean nodeBean : children) {
                nodeBean.setExpand(isExpand);
            }
        }
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getIconExpand() {
        return iconExpand;
    }

    public void setIconExpand(int iconExpand) {
        this.iconExpand = iconExpand;
    }

    public int getIconNoExpand() {
        return iconNoExpand;
    }

    public void setIconNoExpand(int iconNoExpand) {
        this.iconNoExpand = iconNoExpand;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public List<NodeBean> getChildren() {
        return children;
    }

    public void setChildren(List<NodeBean> children) {
        this.children = children;
    }

    public NodeBean getParent() {
        return parent;
    }

    public void setParent(NodeBean parent) {
        this.parent = parent;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

}

8.第八步 新建各个需要用到的xml布局文件:

//activity_tree_list:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/color_white"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mTreeListRecy"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

//sdk_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_5"
    android:layout_marginLeft="@dimen/dp_20"
    android:layout_marginRight="@dimen/dp_20"
    android:layout_marginTop="@dimen/dp_5"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/mOppoSdkTextPosition"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="0"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

    <TextView
        android:id="@+id/mOppoSdkText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:gravity="left|center_vertical"
        android:minHeight="@dimen/dp_50"
        android:padding="@dimen/dp_10"
        android:text="text"
        android:textColor="@color/black"
        android:textSize="@dimen/sp_15" />

</LinearLayout>

//tree_header_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/color_white"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mTreeHeaderViewRecy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

//tree_header_item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/dp_5"
    android:layout_marginLeft="@dimen/dp_20"
    android:layout_marginRight="@dimen/dp_20"
    android:layout_marginTop="@dimen/dp_5"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/mTreeHeaderLin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">


        <ImageView
            android:id="@+id/mTreeHeaderExpand"
            android:layout_width="@dimen/dp_50"
            android:layout_height="@dimen/dp_50"
            android:gravity="center"
            android:padding="@dimen/dp_10"
            android:src="@drawable/svg_navigate_next" />

        <TextView
            android:id="@+id/mTreeHeaderName"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:gravity="left|center_vertical"
            android:maxLines="1"
            android:minHeight="@dimen/dp_50"
            android:padding="@dimen/dp_10"
            android:text="text"
            android:textColor="@color/black"
            android:textSize="@dimen/sp_15" />

    </LinearLayout>

</LinearLayout>

9.第九步 两个svg的xml图片文件:

//svg_expand_more:

<vector android:alpha="0.85" android:autoMirrored="true"
    android:height="24dp" android:tint="#3B76EC"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#ffffffff" android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z"/>
</vector>

//svg_navigate_next:

<vector android:alpha="0.85" android:autoMirrored="true"
    android:height="24dp" android:tint="#3B76EC"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#ffffffff" android:pathData="M10,6L8.59,7.41 13.17,12l-4.58,4.59L10,18l6,-6z"/>
</vector>

//---------------------------------------------------------------END----------------------------------------------------------

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

115.Android 简单的多级树结构列表+正常列表结合使用(RecyclerView) 两种方式实现(自定义adapter实现和使用BaseQuickAdapter库实现) 的相关文章

随机推荐

  • VUE创建播发器组件并调用

    首先用vue cli创建前端项目 参考 https www cnblogs com ouyangkai p 11549290 html 新建play vue文件 编写play组件
  • 一周安全学术资讯 1-1

    本周安全学术资讯有车联网安全 AI安全 软件安全 网络安全 IoT安全以及Web与隐私安全 包括针对自动驾驶中深度估计算法的攻击 探索隐藏摄像头等 文章目录 车联网安全 针对自动驾驶系统中基于深度估计的避障的远程攻击 IoT与IIoT安全
  • npm 发包

    一 必备环境 要使用 npm 需要先安装 node js node下载地址 下载 Node js 1 npm 源管理 npm 发包必须使用 npm 的源镜像 如果你的 npm 源设置了淘宝镜像则需要切换回来 借用如下指令 1 查看 npm
  • FFmpeg 实现MP4 转m3u8

    方式一 操作简单 但是转换效率很低 cmd 直接切片命令 参数建议看官网文档 ffmpeg i input mp4 c v libx264 c a aac strict 2 f hls hls list size 2 hls time 15
  • 解决:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 14: illegal multibyte sequence

    一 背景 二 问题分析 三 问题解决 方法一 方法二 四 附加 一 背景 当我们在python中对Windows中的一个文件进行内容的读取时 出现一个错误 UnicodeDecodeError gbk codec can t decode
  • table中的tr的内容可重复复制多个

    1 效果 2 Js使用jq来解决 4 注意点 由2个table合并完成 最后的确认按钮在下一个table中 不然复制的效果不好 这个复制是复制最后一行的tr 然后加到最后一行中 应该确保最后一行的tr后面没有其他影响的东西 5 核心代码 d
  • 华为机试HJ108 求最小公倍数

    HJ108 求最小公倍数 Python 题目 解题思路 代码 结果 题目 解题思路 1 辗转相除求最大公约数 2 最小公倍数 两数之积 最大公约数 代码 辗转相除 求最大公约数 def maxgys a b if a
  • python读写matlab的.m文件

    python读写matlab的 m文件 做深度学习的项目时 原始图片一般会先转换成其他格式 方便学习框架直接读取 比如caffe的leveldb lmdb matlab的 m格式 tensorflow的tfrecords格式等 python
  • C++练习2:动态规划问题

    问题汇总 剑指 Offer 10 II 青蛙跳台阶问题 剑指 Offer 63 股票的最大利润 剑指 Offer 42 连续子数组的最大和 剑指 Offer 47 礼物的最大价值 剑指 Offer 46 把数字翻译成字符串 剑指 Offer
  • 二分查找应用

    题目信息 给定字符串 s 和 t 判断 s 是否为 t 的子序列 字符串的一个子序列是原始字符串删除一些 也可以不删除 字符而不改变剩余字符相对位置形成的新字符串 例如 ace 是 abcde 的一个子序列 而 aec 不是 进阶 如果有大
  • 一份规范的接口文档应该包括什么内容?

    1 接口文档是什么 在项目开发汇总 web项目的前后端是分离开发的 应用程序的开发 需要由前后端工程师共同定义接口 编写接口文档 之后大家都根据这个接口文档进行开发 到项目结束前都要一直维护 2 为什么要写接口文档 1 项目开发过程中前后端
  • linux下mysql中文问号_linux下mysql出现中文乱码(中文问号)的原因及解决方法

    安装完的MySQL的默认字符集为 latin1 为了要将其字符集改为用户所需要的 比如utf8 就必须改其相关的配置文件 由于linux下MySQL的默认安装目录分布在不同的文件下 不像windows一样放在同一目录下 只需修改其中的my
  • centos7.1下安装与配置cobbler

    设置源 采用网易的源和EPEL的源 http mirror bjtu edu cn fedora epel 6 i386 repoview epel release html yum y install wget vim enhanced
  • 基于pycharm的beautifulsoup4库使用教程

    基于pycharm的beautifulsoup4库使用教程 beautifulsoup4库安装 beautifulsoup4库使用 beautifulsoup4库基本元素 beautifulsoup4库的HTML查找方法 补充Json Ja
  • Mybatis如何使用druid数据连接池

    数据库连接池的作用 学习数据库连接池之前我们也应该听过线程池的 他们虽然技术分支不一样 但是池的思想都是一样的 用了享元模式 达到复用的思想 即不用每次都去创建连接对象 如果这个对象是一个很重的资源对象 比如 创建线程 是需要和操作系统申请
  • 小程序 长按录音 但单机后录音不停止的解决方案

    按钮
  • WARNING: You are using pip version 22.0.4; however, version 22.1 is available.

    问题 WARNING You are using pip version 22 0 4 however version 22 1 is available You should consider upgrading via the C Us
  • 《Java异常处理(超级详细)》

    基本介绍 异常处理就是当异常发生时 对异常处理的方式 异常处理的方式 1 try catch finally 程序员在代码中捕获发生的异常 自行处理 2 throws 将发生的异常抛出 交给调用者 方法 来处理 最顶级的处理者就是JVM m
  • 2018最新精选的Android优秀开源库和项目,很有价值

    热文导读 点击标题阅读 Android架构进阶学习路线图 吊炸天 74款APP完整源码 程序员如何进阶成为大神 1 DDComponentForAndroid 地址 https github com luojilab DDComponent
  • 115.Android 简单的多级树结构列表+正常列表结合使用(RecyclerView) 两种方式实现(自定义adapter实现和使用BaseQuickAdapter库实现)

    一 第一种方式 自定义adapter实现 1 第一步 导入需要用到的依赖库 RecyclerView implementation com android support recyclerview v7 28 0 0 RecyclerAda