Android dynamically add and remove controls/layouts

I. Introduction

        Recently, I have been studying how to use RecyclerView's secondary list. The effects that need to be achieved are as follows.

        Then I checked some blogs and felt that the implementation method was too complicated, and this method was not particularly recommended, so I asked others for advice and got an implementation method that felt good. The idea of ​​implementation is: the entire page is a ScrollView, and the height of the LinearLayout in the ScrollView is set to wrap_content, and then a Layout interface is dynamically added (the content of this interface is a title and RecyclerView).

2. Example

        To illustrate that the above solution is possible, I implemented a simple demo. This demo can dynamically add a Layout interface, and can also remove the added Layout interface.

        The code of Activity is as follows. In the following code, I mainly set a global variable new_view, so that when I create a new Layout interface, I can record it, and then when I want to delete it, I can quickly delete it. This Layout interface. Adding a Layout interface is implemented through the addView(View view) method of the container (for example, LinearLayout, RelativeLayout or FrameLayout); deleting the Layout interface is implemented through the removeView(View view) method of the container.

package com.cs.blackbox;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class DynamicAddAndDelActivity extends AppCompatActivity {
    View new_view = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_add_del);

        LinearLayout ll = findViewById(R.id.dad_ll_main);
        Button bt_add = findViewById(R.id.dad_bt_add);
        Button bt_delete = findViewById(R.id.dad_bt_delete);
        LayoutInflater li = LayoutInflater.from(this);

        bt_add.setOnClickListener(v -> {
            new_view = li.inflate(R.layout.item_demo, null, false);
            addItemView(ll, new_view);
        });
        bt_delete.setOnClickListener(v -> {
            removeItemView(ll, new_view);
            new_view = null;
        });
    }

    private void addItemView(LinearLayout ll, View view) {
        if (view != null) {
            ll.addView(view);
        }
    }

    private void removeItemView(LinearLayout ll, View view) {
        if (view != null) {
            ll.removeView(view);
        }
    }
}

        The interface is as follows, very simple

        The corresponding xml code is as follows. The main thing is to set a LinearLayout (direction is vertical) as the container. In this case, when adding the layout, it is appended at the end.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dad_ll_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DynamicAddAndDelActivity">
    <Button
        android:id="@+id/dad_bt_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="add" />
    <Button
        android:id="@+id/dad_bt_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="delete" />
</LinearLayout>

        The Layout interface I want to add is as follows

        The corresponding xml code is as follows

<?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="50dp"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:text="扫描" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:text="解析" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="visible">
            <ImageView
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_gravity="center"
                android:background="@android:drawable/btn_star_big_on" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="图片"
                android:textSize="20sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

3. Effect

        The first picture is the default view, the second picture is when ADD is clicked, and the third picture is when DELETE is clicked.

        

4. Summary

        Although the above code does not have the content of RecyclerView, it is the same if you replace the specific control with this one. The final addition is achieved through LinearLayout's addView(View view) method, and the deletion is also achieved through LinearLayout's removeView(View view). So if your entire interface needs to dynamically add or delete layouts or controls, you need to set global variables. RecyclerView secondary lists can be saved and managed through a List data structure.

5. Reference materials

        1. Android development notes: Android dynamically adding and deleting controls

        2. Android dynamically adds layout-simple usage of LayoutInflater

Guess you like

Origin blog.csdn.net/qq_36158230/article/details/132406759