文章目錄
- 先上效果圖
- 前言
- 逐層addView方式
先上效果圖

前言
前些天有個朋友問我,要實作一個樹狀的串列要怎么做,根據一個完全符合規則但是卻很頭疼的一個Json決議來實作,見下格式,對于有些Android開發者來說,這個Json或許并不友好,沒有辦法直接轉成物體類,其實這一串Json決議映射成可折疊串列也并不難!
代碼已提交至Github:https://github.com/ThirdGoddess/AndroidTree
{
"code":"200",
"message":"success",
"data":[
{
"id":"1001",
"title":"編號1",
"next":[
{
"id":"10011",
"title":"編號1-1"
},
{
"id":"10012",
"title":"編號1-2",
"next":[
{
"id":"100121",
"title":"編號1-2-1",
"next":[
{
"id":"1001211",
"title":"編號1-2-1-1"
},
{
"id":"1001212",
"title":"編號1-2-1-2"
},
{
"id":"1001213",
"title":"編號1-2-1-3"
},
{
"id":"1001214",
"title":"編號1-2-1-4"
},
{
"id":"1001215",
"title":"編號1-2-1-5"
}
]
},
{
"id":"100122",
"title":"編號1-2-2"
},
{
"id":"100123",
"title":"編號1-2-3",
"next":[
{
"id":"1001231",
"title":"編號1-2-3-1"
},
{
"id":"1001232",
"title":"編號1-2-3-2"
},
{
"id":"1001233",
"title":"編號1-2-3-3"
},
{
"id":"1001234",
"title":"編號1-2-3-4"
},
{
"id":"1001235",
"title":"編號1-2-3-5"
}
]
}
]
},
{
"id":"10013",
"title":"編號1-3"
}
]
},
{
"id":"1002",
"title":"編號2"
},
{
"id":"1003",
"title":"編號3"
},
{
"id":"1004",
"title":"編號4",
"next":[
{
"id":"10041",
"title":"編號4-1"
},
{
"id":"10042",
"title":"編號4-2"
}
]
},
{
"id":"1005",
"title":"編號5"
}
]
}
拿到這一串不確定層級的Json該想什么?用什么去決議?該用什么控制元件?
逐層addView方式
其實可以直接使用Gson決議,不過這個物體類要自己手寫一下:
package com.example.myapplication;
import java.util.List;
public class DataBean {
private String code;
private String message;
private List<Data> data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public static class Data {
private String id;
private String title;
private List<Data> next;//重點在這里
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Data> getNext() {
return next;
}
public void setNext(List<Data> next) {
this.next = next;
}
}
}
( OpenParam.json為那個json字串 )
使用Gson決議:
Kotlin:
val dataBean = Gson().fromJson(OpenParam.json, DataBean().javaClass)
Java:
DataBean dataBean = new Gson().fromJson(OpenParam.json, DataBean.class)
既然決議出來了,之后可以通過遞回來逐漸addView()的方式實作,判斷next欄位是否為null即可!但要在遞回開始之前,先要分析一下布局!
既然要逐級嵌套,先來一個LinearLayout,當然這個串列是可滑動的,外層嵌套一個ScrollView即可,Activity布局那就是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<LinearLayout
android:id="@+id/treeLayout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
之后要分析每一個條目,有兩種情況,一種是帶子布局的item,一種是不帶子布局的item,當遇到有嵌套的情況,即存在next欄位,就可以使用帶子布局的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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/flag"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="32dp"
android:padding="8dp"
android:src="@mipmap/open" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingRight="32dp"
android:textColor="#333333" />
</LinearLayout>
<LinearLayout
android:id="@+id/nextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:orientation="vertical" />
</LinearLayout>
不帶子布局的:
<?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"
android:orientation="vertical">
<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="72dp"
android:layout_marginRight="32dp"
android:gravity="center_vertical"
android:textColor="#333333" />
</LinearLayout>
之后便是根據物體類來遞回實作,回圈遍歷,判斷是否存在next欄位而做出兩種情況,如在37行到69行之間代碼!存在子節點使用帶有子布局的item,反之使用另一個!
package com.example.myapplication
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.LayoutInflater.from
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isGone
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_text.view.*
import kotlinx.android.synthetic.main.item_tree.view.*
class MainActivity : AppCompatActivity() {
lateinit var objectAnimator: ObjectAnimator
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//決議Json
val dataBean = Gson().fromJson(OpenParam.json, DataBean().javaClass)
//創建View
createView(dataBean.data, treeLayout)
}
/**
* 遞回創建布局
*/
private fun createView(dataList: MutableList<DataBean.Data>, linearLayout: LinearLayout) {
for (i in 0 until dataList.size) {
val title = dataList[i].title
val next = dataList[i].next
if (null != next) {
val childLayout = from(this).inflate(R.layout.item_tree, null, false)
childLayout.title.text = title
//展開和關閉的點擊事件
childLayout.title.setOnClickListener {
if (childLayout.nextLayout.isGone) {
//展開
childLayout.nextLayout.visibility = View.VISIBLE
//添點展開影片
objectAnimator = ObjectAnimator.ofFloat(childLayout.flag, "rotation", 0f)
objectAnimator.duration = 400
objectAnimator.start()
} else {
//隱藏
childLayout.nextLayout.visibility = View.GONE
//添點關閉影片
objectAnimator = ObjectAnimator.ofFloat(childLayout.flag, "rotation", -90f)
objectAnimator.duration = 400
objectAnimator.start()
}
}
createView(next, childLayout.nextLayout)
linearLayout.addView(childLayout)
} else {
val textLayout = from(this).inflate(R.layout.item_text, null, false)
textLayout.info.text = title
linearLayout.addView(textLayout)
}
}
}
}
這樣便實作了,這種適用于常規的折疊串列,如果遇到需要加載更多的情況下,可以直接判斷ScrollView是否滾動到底部,并且上次的網路加載是否完成,達成條件則再次呼叫27行代碼進行插入即可!這里不再多做解釋,不懂的朋友可以直接評論區問我!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/254090.html
標籤:其他
