如何根据条件在RecyclerView上安排物品?

时间:2019-08-06 07:20:18

标签: android android-recyclerview

如果第3列从后端Recyclerview的第一行中选择前三项,第二行中选择四项,则我想在RecyclerView上显示项目,如果第4列的第一行中的前四项和第二排五个。

如何在RecyclerView

中排列以上列表
   recyclerViewLineItem.setHasFixedSize(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerViewLineItem.setLayoutManager(linearLayoutManager);

enter image description here

3 个答案:

答案 0 :(得分:1)

如果列数大于1,则使用GridLayoutManager

GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, 2/*column count*/);
recyclerView.setLayoutManager(gridLayoutManager);

&用于水平滚动,请使用此

new GridLayoutManager(getActivity(), 1 /*column count*/, GridLayoutManager.HORIZONTAL, false)

答案 1 :(得分:1)

这实际上很容易。您需要使用GridLayoutManager而不是通常的LinearLayoutManager。 在GridLayoutManager中,您可以根据项目数选择所需的列数。让我告诉你:

int spanCount = 0 ; //This is the number of column you need, set to 0 at first
if(yourItemCount == 3){
spanCount = 3; //set to 3 columns
};
if(yourItemCount == 5){
spanCount = 4; //set to 4 columns, and the 5th item will be at the second row
}

//now pass the spanCount to the GridLayoutManager
GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, spanCount);
recyclerView.setLayoutManager(gridLayoutManager);

希望有帮助。 干杯!

答案 2 :(得分:0)

您可以尝试从适配器传递位置,并且可以在活动/片段中创建一种根据传递的值显示数据的方法。

    private void CheckColumn(RecyclerView recyclerView) {

    int count = recyclerView.getChildCount();
    for (int i = 0; i < count; i++) {
        View view = recyclerView.getChildAt(i);

       // Your code to show grid items here
    }  

}