Android动态添加和删除控件/布局

一、引言

        最近在研究RecyclerView二级列表的使用方法,需要实现的效果如下。

        然后查了一些博客,觉得实现方式太过复杂,而且这种方式也不是特别受推荐,所以请教了别人,得到了一种感觉还不错的实现方式。实现的思路为:整个页面是一个ScrollView,而ScrollView里面的LinearLayout的高度设置为wrap_content,然后动态地添加一个Layout界面(这个界面的内容为一个title和RecyclerView)。

二、示例

        为了说明上面的方案可以,我实现了一个简单的demo。该demo可以动态地添加一个Layout界面,同时也可以把添加的Layout界面去掉。

        Activity的代码如下。在下面的这段代码中,我主要是设置了一个全局变量new_view,这样,当我在new一个Layout界面时就可以将它记录下来,然后当我想要删除它的时候,也可以快速实现删除这个Layout界面。增加Layout界面通过容器(例如,LinearLayout、RelativeLayout或FrameLayout)的addView(View view)方法实现;删除Layout界面则通过容器的removeView(View view)方法实现。

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);
        }
    }
}

        界面如下,很简单

        对应的xml代码如下。主要是将一个LinearLayout(方向是vertical)设置为容器。这样的话,在添加布局的时候就是以追加的方式在后面追加进去。

<?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>

        我想要增加的Layout界面如下

        对应的xml代码如下

<?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>

三、效果

        第一张是默认视图,第二张是点击了ADD,第三张是点击DELETE

        

四、总结

        上面的代码虽然没有RecyclerView的内容,但是如果将具体的控件换成这个是一样的,最终增加是通过LinearLayout的addView(View view)方法实现,而删除也是通过LinearLayout的removeView(View view)实现。所以如果你整个界面需要动态地增加或者删除布局or控件,就需要设置全局的变量。RecyclerView二级列表可以通过一个List数据结构来对其进行保存和管理。

五、参考资料

        1、Android开发笔记: Android动态添加、删除控件

        2、Android动态添加布局-LayoutInflater简单用法

猜你喜欢

转载自blog.csdn.net/qq_36158230/article/details/132406759