Skip to content

[Button] ButtonGroup wraps Buttons in RecyclerView, click button, button has a high chance of disappearing #4797

Open
@onlymash

Description

@onlymash

Description:

ButtonGroup wraps Buttons in RecyclerView, click button, button has a high chance of disappearing

buttongroup.mp4

Source code:

//item_grid.xml
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:layout_margin="4dp">
	<androidx.constraintlayout.widget.ConstraintLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content">
		<androidx.appcompat.widget.AppCompatImageView
			android:id="@+id/image"
			android:layout_width="match_parent"
			android:layout_height="0dp"
			app:layout_constraintDimensionRatio="1"
			app:layout_constraintTop_toTopOf="parent"
			app:srcCompat="@mipmap/ic_launcher" />
		<com.google.android.material.button.MaterialButtonGroup
			android:id="@+id/btn_group"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:gravity="center_horizontal"
			android:padding="4dp"
			app:layout_constraintTop_toBottomOf="@id/image">
			<com.google.android.material.button.MaterialButton
				android:id="@+id/btn1"
				style="?materialIconButtonOutlinedStyle"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				app:icon="@drawable/baseline_bubble_chart_24" />
			<com.google.android.material.button.MaterialButton
				android:id="@+id/btn2"
				style="?materialIconButtonOutlinedStyle"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				app:icon="@drawable/baseline_filter_center_focus_24" />
		</com.google.android.material.button.MaterialButtonGroup>
	</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>

// activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:app="http://schemas.android.com/apk/res-auto"
	xmlns:tools="http://schemas.android.com/tools"
	android:id="@+id/main"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">
	<androidx.recyclerview.widget.RecyclerView
		android:id="@+id/rv_list"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:orientation="vertical"
		app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
		app:spanCount="3" />
</FrameLayout>

class MainActivity : AppCompatActivity() {
	private val binding by lazy {
		ActivityMainBinding.inflate(layoutInflater)
	}
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		enableEdgeToEdge()
		setContentView(binding.root)
		ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
			val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout())
			v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
			insets
		}
		binding.rvList.adapter = GridAdapter()
	}
	private class GridAdapter : RecyclerView.Adapter<GridAdapter.GridViewHolder>() {
		override fun getItemCount(): Int {
			return 200
		}
		override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GridViewHolder {
			return GridViewHolder(
				ItemGridBinding.inflate(
					LayoutInflater.from(parent.context),
					parent,
					false
				)
			)
		}
		override fun onBindViewHolder(holder: GridViewHolder, position: Int) {
		}
		class GridViewHolder(private val binding: ItemGridBinding) : RecyclerView.ViewHolder(binding.root)
	}
}

Minimal sample app repro:
https://github.com/onlymash/ButtonGroupDemo

Android API version:

35

Material Library version:

1.14.0-alpha01

Device:

OnePlus 12/13 and OnePlus Pad 2 Pro

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions