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樹形列表
即可!
在這裏插入圖片描述
感謝閱讀!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章