1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android】複数ビューの表示/非表示処理の一括化

Posted at

概要

複数ビューの表示/非表示処理を一括化する方法について。
複数ビューをグループ化する手法を説明。
親ビューの中に子ビューを複数作成し、親ビューのvisibilityを変更させる手法もあるが、ネストが深くなる。

コード

レイアウトファイルにandroidx.constraintlayout.widget.Groupを追加し、app:constraint_referenced_idsにIDをコンマ区切りで記述。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	android:id="@+id/chart"
	android:layout_width="match_parent"
	android:layout_height="match_parent">

	<TextView
		android:id="@+id/text1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="text1"
		app:layout_constraintBottom_toTopOf="@id/text2"
		app:layout_constraintEnd_toEndOf="parent"
		app:layout_constraintStart_toStartOf="parent"
		app:layout_constraintTop_toTopOf="parent"/>

	<TextView
		android:id="@+id/text2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="text2"
		app:layout_constraintBottom_toTopOf="@id/text3"
		app:layout_constraintEnd_toEndOf="parent"
		app:layout_constraintStart_toStartOf="parent"
		app:layout_constraintTop_toBottomOf="@id/text1"/>

	<TextView
		android:id="@+id/text3"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="text3"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintEnd_toEndOf="parent"
		app:layout_constraintStart_toStartOf="parent"
		app:layout_constraintTop_toBottomOf="@id/text2"/>

	<androidx.constraintlayout.widget.Group
		android:id="@+id/group"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		app:constraint_referenced_ids="text1,text2,text3"/>
</androidx.constraintlayout.widget.ConstraintLayout>

処理側のコード

MainActivity.kt
binding.group.visibility = GONE
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?