如何在Android Recyclerview中按时间顺序排列项目

问题描述 投票:0回答:1

我有一个带有 Cardview 的 Recyclerview,我使用代码设置布局

recyclerView = binding.recyclerView;
        recyclerView.setHasFixedSize(true);

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);

不幸的是,这些项目的显示方式很奇怪。第一个项目位于下排,而第二个项目不在其旁边,而是在上排的上方,如屏幕截图中所示。我想要的是将项目按时间顺序排列为 1、2、3、4、5 等,直到它们不再适合上一行(接下来应填充第二行)。你能告诉我该怎么做吗?我也尝试过

layoutManager.setReverseLayout(true);
但这只是垂直镜像了这些项目。

java android android-recyclerview
1个回答
0
投票

您必须根据方向对列表进行排序,例如

val orientation = resources.configuration.orientation
    
// Set different sorting orders based on the orientation
if (orientation == android.content.res.Configuration.ORIENTATION_PORTRAIT) {
     recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
     adapter.setData(getSortedDataForPortrait())
} else {
     recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
     adapter.setData(getSortedDataForLandscape())
}
© www.soinside.com 2019 - 2024. All rights reserved.