For multi-level nesting, we can use an ordinary RecyclerView and use different types of ViewHolder to display different levels of data.

For multi-level nesting, we can use an ordinary RecyclerView and use different types of ViewHolder to display different levels of data.

The specific implementation is as follows:

  1. Define data model

In order to display multi-level nested data, we need a data structure to save different levels of data. The following data structures can be used:

public abstract class MultiLevelItem {

    private int level;

    public MultiLevelItem(int level) {
        this.level = level;
    }

    public int getLevel() {
        return level;
    }

    // 这里定义了每个层级的不同类型,以及每个类型的ViewHolder
    public abstract int getItemType();

    public abstract RecyclerView.ViewHolder onCreateViewHolder(View itemView);

    public abstract void onBindViewHolder(RecyclerView.ViewHolder holder);
}

MultiLevelItem is an abstract class that can define the data structure of each level and return different data types, ViewHolder classes and view types according to the level.

2. Create RecyclerView.Adapter

Create an adapter for rendering data into RecyclerView.

public class MultiLevelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MultiLevelItem> items;

    public MultiLevelAdapter(List<MultiLevelItem> items) {
        this.items = items;
    }

    @Override
    public int getItemViewType(int position) {
        return items.get(position).getItemType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
        switch (viewType) {
            case R.layout.item_level1:
                return new Level1ViewHolder(itemView);
            case R.layout.item_level2:
                return new Level2ViewHolder(itemView);
            default:
                throw new IllegalArgumentException("Invalid view type");
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        items.get(position).onBindViewHolder(holder);
    }

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

    // 这里定义不同类型的ViewHolder
    public static class Level1ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;

        public Level1ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }

        public void setTitle(String title) {
            tvTitle.setText(title);
        }
    }

    public static class Level2ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;

        public Level2ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
        }

        public void setTitle(String title) {
            tvTitle.setText(title);
        }
    }
}

MultiLevelAdapter inherits from RecyclerView.Adapter, which implements the getItemViewType() and onCreateViewHolder() methods to return different ViewHolder classes according to the view type.

3. Add data and display

In the onCreate() method of Activity or Fragment, create a MultiLevelAdapter instance and add data to the adapter.

Let's say we have 2 Level 1 projects and some Level 2 projects associated with them.

List<MultiLevelItem> items = new ArrayList<>();

items.add(new Level1Item("Item 1"));
items.add(new Level2Item("Sub Item 1"));
items.add(new Level2Item("Sub Item 2"));

items.add(new Level1Item("Item 2"));
items.add(new Level2Item("Sub Item 1"));
items.add(new Level2Item("Sub Item 2"));

MultiLevelAdapter adapter = new MultiLevelAdapter(items);
recyclerView.setAdapter(adapter);

MultiLevelAdapter populates the RecyclerView with a list of items.

In the Level1Item and Level2Item data model classes, implement the getItemType(), onCreateViewHolder() and onBindViewHolder() methods to return the view type and instantiate/bind different types of ViewHolder.

In this way, multi-level nesting of RecyclerView can be implemented very simply and elegantly.

Guess you like

Origin blog.csdn.net/ck3345143/article/details/130204405