使用RecyclerView生成树形结构
时间: 2023-07-11 14:15:23 浏览: 144
以下是使用 RecyclerView 生成树形结构的示例代码,你可以根据自己的需求进行调整。
1. 定义数据模型:
```java
public class TreeNode {
private String name;
private int id;
private int parentId;
private List<TreeNode> children;
// 省略 getter 和 setter 方法
}
```
2. 创建适配器:
```java
public class TreeAdapter extends RecyclerView.Adapter<TreeAdapter.ViewHolder> {
private List<TreeNode> nodeList;
public TreeAdapter(List<TreeNode> nodeList) {
this.nodeList = nodeList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_node, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
TreeNode node = nodeList.get(position);
holder.nameTextView.setText(node.getName());
holder.expandImageView.setImageResource(
node.getChildren() != null && node.getChildren().size() > 0 ?
R.drawable.ic_expand : R.drawable.ic_collapse);
holder.itemView.setOnClickListener(v -> {
if (node.getChildren() != null && node.getChildren().size() > 0) {
int index = holder.getAdapterPosition();
if (nodeList.subList(index + 1, index + node.getChildren().size() + 1).isEmpty()) {
nodeList.addAll(index + 1, node.getChildren());
notifyItemRangeInserted(index + 1, node.getChildren().size());
} else {
nodeList.removeAll(node.getChildren());
notifyItemRangeRemoved(index + 1, node.getChildren().size());
}
holder.expandImageView.setImageResource(
nodeList.get(index + 1).getChildren() != null &&
nodeList.get(index + 1).getChildren().size() > 0 ?
R.drawable.ic_expand : R.drawable.ic_collapse);
}
});
}
@Override
public int getItemCount() {
return nodeList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
ImageView expandImageView;
ViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.name);
expandImageView = itemView.findViewById(R.id.expand);
}
}
}
```
3. 创建布局文件 item_node.xml:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/expand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_expand" />
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp" />
</LinearLayout>
```
4. 在 Activity 或 Fragment 中使用 RecyclerView:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private TreeAdapter treeAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<TreeNode> nodeList = createTree();
treeAdapter = new TreeAdapter(nodeList);
recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(treeAdapter);
}
private List<TreeNode> createTree() {
List<TreeNode> nodeList = new ArrayList<>();
TreeNode root = new TreeNode();
root.setName("Root");
root.setId(0);
root.setParentId(-1);
TreeNode node1 = new TreeNode();
node1.setName("Node 1");
node1.setId(1);
node1.setParentId(0);
TreeNode node2 = new TreeNode();
node2.setName("Node 2");
node2.setId(2);
node2.setParentId(0);
TreeNode node11 = new TreeNode();
node11.setName("Node 1.1");
node11.setId(11);
node11.setParentId(1);
TreeNode node12 = new TreeNode();
node12.setName("Node 1.2");
node12.setId(12);
node12.setParentId(1);
TreeNode node21 = new TreeNode();
node21.setName("Node 2.1");
node21.setId(21);
node21.setParentId(2);
TreeNode node22 = new TreeNode();
node22.setName("Node 2.2");
node22.setId(22);
node22.setParentId(2);
node1.setChildren(Arrays.asList(node11, node12));
node2.setChildren(Arrays.asList(node21, node22));
root.setChildren(Arrays.asList(node1, node2));
nodeList.add(root);
return nodeList;
}
}
```
以上代码实现了一个简单的树形结构,可以通过点击节点来展开或折叠其子节点列表。
阅读全文