You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中实现ListView与GridView的高效平滑切换问题

Android ListView与GridView视图切换优化问题

我在Android中实现ListView与GridView的视图切换功能,已完成基础实现,但切换效果生硬,容易引起用户反感。以下是相关代码、实际效果与预期效果:

相关代码

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

效果对比

实际效果

实际效果展示

预期效果

预期效果展示

内容的提问来源于stack exchange,提问作者Devarshi Rawal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 19:46:34