I am trying to achieve a view switch that enables user to switch between "List view" and "Grid view" in Android. However, I have achieved this but I am not satisfied with results since there is a high probability that user will find it annoying.
Please find the code below as well as expected and actual results :
fragment_dashboard.xml :
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButtonViewGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="true"
android:padding="5dp"
app:checkedButton="@id/buttonGridView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonListView"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/list_view" />
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonGridView"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/grid_view" />
</com.google.android.material.button.MaterialButtonToggleGroup>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewNotes"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:adapter="@{adapter}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toggleButtonViewGroup"
tools:itemCount="5"
tools:listitem="@layout/item_note" />
NotesListViewModel.kt
fun addOnButtonCheckedListener(toggleGroup: MaterialButtonToggleGroup,recyclerView: RecyclerView,mContext: Context){
recyclerView.layoutManager = StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL)
toggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked ->
if (isChecked){
when(checkedId){
R.id.buttonListView ->{
recyclerView.layoutManager = LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false)
}
R.id.buttonGridView ->{
recyclerView.layoutManager = StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL)
}
}
}
}
}
DashboardFragment.kt
@AndroidEntryPoint
class DashboardFragment: Fragment() {
private lateinit var binding: FragmentDashboardBinding
private val viewModel: NotesListViewModel by viewModels()
private lateinit var adapter: NotesListAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
binding = FragmentDashboardBinding.inflate(inflater,container,false)
binding.navController = findNavController()
binding.viewModel = viewModel
adapter = NotesListAdapter(viewModel.getNotes(),findNavController())
binding.adapter = adapter
viewModel.addOnButtonCheckedListener(binding.toggleButtonViewGroup,binding.recyclerViewNotes,requireActivity())
return binding.root
}
override fun onResume() {
super.onResume()
adapter.notifyItemRangeChanged(0,viewModel.getNotes().size)
}
}
1. Actual result :
2. Expected result :