Android实现树形结构列表Recyclerview

Android实现树形列表Recyclerview

先看看效果
在这里插入图片描述
因为最近需要用到这样的树形列表,网上找了好几圈,但是都是比较杂乱,然后自己参考网上资料自己做了一个,可以实现多层。

这是用Recyclerview做的原理非常简单,需要的可以去研究研究。
在这里插入图片描述
首先需要添加Recyclerview的依赖

implementation 'androidx.recyclerview:recyclerview:1.1.0-beta04'

MainActivity.java

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    TreeListAdapter treeListAdapter;
    List<TreeData> treeData = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //注意这里的顺序不能乱 必须按照层级关系添加

        treeData.add(new TreeData("父级1","1","0" ,0,true));
        treeData.add(new TreeData("子级1","2","1" ,1,true));
        treeData.add(new TreeData("子级的子级1","3","2" ,2,false));
        treeData.add(new TreeData("子级的子级2","4","0" ,2,false));
        treeData.add(new TreeData("父级2","5","0" ,0,true));
        treeData.add(new TreeData("子级2","6","0" ,1,false));
        treeData.add(new TreeData("子级3","7","0" ,1,false));


        recyclerView =findViewById(R.id.tree_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        treeListAdapter = new TreeListAdapter(MainActivity.this,treeData);
        recyclerView.setAdapter(treeListAdapter);
    }
}

布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/tree_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

//列表项类TreeData.java

//列表项类
public class TreeData {
    private String  name,code,code_id;
    private Boolean hasChild;
    private int Level;
    //               名字  当前的ID 上一级的ID 层级  是否可以展开
    public TreeData(String name,String code ,String code_id ,int Level,Boolean hasChild)
    {
        this.name=name;
        this.code=code;
        this.code_id=code_id;
        this.Level=Level;
        this.hasChild=hasChild;
    }
    public int getLevel() {
        return Level;
    }

    public Boolean getHasChild() {
        return hasChild;
    }

    public String getName() {
        return name;
    }

    public String getCode() {
        return code;
    }

    public String getCode_id() {
        return code_id;
    }

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

    public void setCode(String code) {
        this.code = code;
    }

    public void setCode_id(String code_id) {
        this.code_id = code_id;
    }

    public void setHasChild(Boolean hasChild) {
        this.hasChild = hasChild;
    }

    public void setLevel(int level) {
        Level = level;
    }
}

由于代码比较长就不贴上来啦,有需要的话去下载哦!
需要该资料可以关注公众号:智慧小巷
回复:Android树形列表
即可!
在这里插入图片描述
感谢阅读!

原创文章 19 获赞 4 访问量 5130

猜你喜欢

转载自blog.csdn.net/dwh1314/article/details/105299730