Recyclerview GridLayoutManger - 如何更改商品的订单?显示彼此相邻的两个列表

时间:2022-10-20 11:46:08

Basically I have 2 lists, 1 to 10, and I want to display them in recyclerview one next to the other, something like this:

基本上我有2个列表,1到10,我想在recyclerview中将它们显示在另一个旁边,如下所示:

1  1
2  2
3  3
.  .
.  .
10 10

What I have tried is GridLayoutManager with 2 columns, but what I'm getting is this:

我试过的是GridLayoutManager有2列,但我得到的是:

Recyclerview GridLayoutManger  - 如何更改商品的订单?显示彼此相邻的两个列表

You can see it's populating grid from left to right, but I want it to go from top to bottom and when it gets to the new list, displays it in the 2nd column.

您可以看到它从左到右填充网格,但我希望它从上到下,当它到达新列表时,将其显示在第二列中。

Any advice how can i make that?

任何建议我怎么能这样做?

I'm using recyclerviewMergeAdapter because I thought it's a good approach. If there's better solution in this situation let me know.

我正在使用recyclerviewMergeAdapter,因为我认为这是一个很好的方法。如果在这种情况下有更好的解决方案,请告诉我。

private final PolazakAdapterRv adapter1;
private final PolazakAdapterRv adapter2;
private final RecyclerViewMergeAdapter mergeAdapter;

@BindView(R.id.recyclerViewWorkDays)
RecyclerView recyclerView;


public DaysFragmentView(Context context) {
    super(context);
    inflate(getContext(), R.layout.fragment_work_days, this);
    ButterKnife.bind(this);

    mergeAdapter = new RecyclerViewMergeAdapter();

    adapter1 = new PolazakAdapterRv(getContext());
    adapter2 = new PolazakAdapterRv(getContext());

    mergeAdapter.addAdapter(adapter1);
    mergeAdapter.addAdapter(adapter2);

    recyclerView.setAdapter(mergeAdapter);
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
    recyclerView.setLayoutManager(gridLayoutManager);
}


public void setRecyclerViewAdapter(List<Integer>[] lists) {
    List<Integer> list1 = lists[0];
    List<Integer> list2 = lists[1];

    adapter1.setMap(list1);
    adapter2.setMap(list2);

    mergeAdapter.notifyDataSetChanged();
    Timber.d("RecyclerView updated!");
}


PolazakAdapterRv:

public class PolazakAdapterRv extends RecyclerView.Adapter<PolazakHolder> {

private final Context context;
private List<Integer> list;

public PolazakAdapterRv(Context context) {
    this.context = context;
    this.list = new ArrayList<>();
}


public void setMap(List<Integer> list) {
    this.list = list;
}

@Override
public PolazakHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context)
            .inflate(R.layout.rv_polazak, parent, false);
    return new PolazakHolder(view);
}

@Override
public void onBindViewHolder(PolazakHolder holder, int position) {
    holder.textViewPolazakHour.setText(list.get(position).toString());
}

@Override
public int getItemCount() {
    return list.size();
}

1 个解决方案

#1


0  

All you need is one adapter with two view types and a simple logic for indexing the right thing. Sample code:

您只需要一个具有两种视图类型的适配器和一个用于索引正确事物的简单逻辑。示例代码:

public class PolazakAdapterRv extends RecyclerView.Adapter<PolazakHolder> {
private final Context context;
private List<Integer> list1;
private List<Integer> list2;

public PolazakAdapterRv(Context context) {
    this.context = context;
}

public void setList1(List<Integer> list) {
    this.list1 = list;
}

public void setList2(List<Integer> list) {
    this.list2 = list;
}

@Override
public PolazakHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context)
            .inflate(R.layout.rv_polazak, parent, false);
    return new PolazakHolder(view);
}

@Override
public void onBindViewHolder(PolazakHolder holder, int position) {
    int index = position / 2; // assumes there are two lists of equal lengths
    int viewType = getItemViewType(position);
    if (viewType == TYPE_1) {
        holder.textViewPolazakHour
        .setText(list1.get(index).toString());
    }
    else if (viewType == TYPE_2) {
        holder.textViewPolazakHour
        .setText(list2.get(index).toString());
    }
}

@Override
public int getItemCount() {
    return list1.size() + list2.size();
}

@Override
public int getItemViewType(int position) {
    return position % 2 == 0 ? VIEW_TYPE_1 : VIEW_TYPE_2;
}

Breaking it down:

打破它:

The one adapter has the two lists you want to display so they all scroll together.

一个适配器具有您要显示的两个列表,因此它们全部滚动在一起。

You have two view types that describe each list.

您有两种描述每个列表的视图类型。

Since you want two columns to show each list separately, logically this just means you alternate what you show in each cell. So when the position is even (left column) you return VIEW_TYPE_1. When the position is even (right column) you return VIEW_TYPE_2.

由于您希望两列分别显示每个列表,因此逻辑上这意味着您可以替换每个单元格中显示的内容。因此,当位置为偶数(左列)时,您将返回VIEW_TYPE_1。当位置为偶数(右列)时,返回VIEW_TYPE_2。

The total count in the adapter is simply the total count of both lists.

适配器中的总计数只是两个列表的总计数。

The view layout is the same (I guess) so you can use the same onCreateViewHolder.

视图布局是相同的(我猜)所以你可以使用相同的onCreateViewHolder。

Finally, when binding the viewholder, you just need to know which view type you're working with based on the index and which model object to use. So just ask your getViewType method for the right view type to know which list to use, and simply divide the position by 2 for the right index into the list. Whether position is even or odd, int division will yield the proper index into the given list.

最后,在绑定视图时,您只需要根据索引以及要使用的模型对象知道您正在使用哪种视图类型。因此,只需询问右侧视图类型的getViewType方法即可知道要使用的列表,并将右侧索引的位置除以2。无论位置是偶数还是奇数,int除法将在给定列表中产生适当的索引。

Hope that helps!

希望有所帮助!

#1


0  

All you need is one adapter with two view types and a simple logic for indexing the right thing. Sample code:

您只需要一个具有两种视图类型的适配器和一个用于索引正确事物的简单逻辑。示例代码:

public class PolazakAdapterRv extends RecyclerView.Adapter<PolazakHolder> {
private final Context context;
private List<Integer> list1;
private List<Integer> list2;

public PolazakAdapterRv(Context context) {
    this.context = context;
}

public void setList1(List<Integer> list) {
    this.list1 = list;
}

public void setList2(List<Integer> list) {
    this.list2 = list;
}

@Override
public PolazakHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context)
            .inflate(R.layout.rv_polazak, parent, false);
    return new PolazakHolder(view);
}

@Override
public void onBindViewHolder(PolazakHolder holder, int position) {
    int index = position / 2; // assumes there are two lists of equal lengths
    int viewType = getItemViewType(position);
    if (viewType == TYPE_1) {
        holder.textViewPolazakHour
        .setText(list1.get(index).toString());
    }
    else if (viewType == TYPE_2) {
        holder.textViewPolazakHour
        .setText(list2.get(index).toString());
    }
}

@Override
public int getItemCount() {
    return list1.size() + list2.size();
}

@Override
public int getItemViewType(int position) {
    return position % 2 == 0 ? VIEW_TYPE_1 : VIEW_TYPE_2;
}

Breaking it down:

打破它:

The one adapter has the two lists you want to display so they all scroll together.

一个适配器具有您要显示的两个列表,因此它们全部滚动在一起。

You have two view types that describe each list.

您有两种描述每个列表的视图类型。

Since you want two columns to show each list separately, logically this just means you alternate what you show in each cell. So when the position is even (left column) you return VIEW_TYPE_1. When the position is even (right column) you return VIEW_TYPE_2.

由于您希望两列分别显示每个列表,因此逻辑上这意味着您可以替换每个单元格中显示的内容。因此,当位置为偶数(左列)时,您将返回VIEW_TYPE_1。当位置为偶数(右列)时,返回VIEW_TYPE_2。

The total count in the adapter is simply the total count of both lists.

适配器中的总计数只是两个列表的总计数。

The view layout is the same (I guess) so you can use the same onCreateViewHolder.

视图布局是相同的(我猜)所以你可以使用相同的onCreateViewHolder。

Finally, when binding the viewholder, you just need to know which view type you're working with based on the index and which model object to use. So just ask your getViewType method for the right view type to know which list to use, and simply divide the position by 2 for the right index into the list. Whether position is even or odd, int division will yield the proper index into the given list.

最后,在绑定视图时,您只需要根据索引以及要使用的模型对象知道您正在使用哪种视图类型。因此,只需询问右侧视图类型的getViewType方法即可知道要使用的列表,并将右侧索引的位置除以2。无论位置是偶数还是奇数,int除法将在给定列表中产生适当的索引。

Hope that helps!

希望有所帮助!