Using RecycleView to implement Dialog embedded multi row and multi column CheckBox

Using RecycleView to implement Dialog embedded multi row and multi column CheckBox


First, put an effect picture (please ignore the wrong title)

Implementation ideas
First of all, define a dialog file. Because we don't know the specific number of checkboxes, define a recycleView to dynamically load data. If you want to achieve only one row of checkboxes, you can put checkboxes directly in the item to solve the problem. There are many lovesickness tutorials on the Internet. No more details here. If you want to put multiple column components in the item, you need to define a recycleView and then Define components to use

First, define an xml layout file of dialog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorWhite"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="40dp">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="department"
            android:textSize="24sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_checkboxList"
            android:layout_width="match_parent"
            android:layout_height="240dp" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="360dp"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:padding="10dp">

        <CheckBox
            android:id="@+id/cb_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All election"
            android:textSize="16sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="end">

            <TextView
                android:id="@+id/tv_close"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:background="@drawable/bg_blue_round_rect_8dp"
                android:padding="5dp"
                android:text="cancel"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/tv_submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_blue_round_rect_8dp"
                android:padding="5dp"
                android:text="Determine"
                android:textSize="16sp" />
        </LinearLayout>

    </LinearLayout>


Write the item layout of recycleView on the first floor

<?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="wrap_content"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

</LinearLayout>

Then there is the layout of the item in the second level recycleView, where you can place the components you need

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <CheckBox
        android:id="@+id/cb_change"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Set a property in the first tier adapter

recyclrView.setLayoutManager(new GridLayoutManager(mContext, 2));

The number 2 is used to control the number of columns

The rest is written like an ordinary adapter

For the first time, if you can't write a good blog, please forgive me a lot. If you don't understand, please reply below. I hope you can make progress together. Don't spray thanks

Published 1 original article · praised 0 · visited 4
Private letter follow

Tags: Android xml encoding REST

Posted on Fri, 07 Feb 2020 10:49:55 -0500 by SmokyBarnable