Android - GridLayoutManager - 垂直加载列表中的项目?

Android - GridLayoutManager - Load items in list vertically?

我有一个清单,假设有 20 个项目。我希望能够像这样加载它们

1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
10 20

我正在使用 RecyclerViewGridLayoutManager 使用 GridLayoutManager(context, 2),它加载项目如下

1 2
3 4
5 6
7 8
....

这不是我想要的输出

GridLayoutManager总是教RecyclerView从左到右,从上到下画画。此视图的原因默认比例为垂直(或水平)。它无法计算在此动态情况下应显示多少行。我有一个解决方案给你。但是,您不应该保留此业务逻辑。

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

public class SampleAdapter extends RecyclerView.Adapter<ViewHolder> {
    private final Context context;
    private List<Integer> listLeft;
    private List<Integer> listRight;

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

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

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

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

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

    @Override
    public int getItemCount() {
        return listLeft.size() + listRight.size();
    }

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

您可以像这样实现逻辑。会很顺利

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, pos: Int) {
            var arraySize = 21
            var half = 0
            if (array % 2 == 0)
                half = array % 2
            else half = (array % 2 + 0.5).toInt()
            if (pos  % 2 == 0)
                print(pos)
            else print(half + pos)
        }

实现此目的的最简单方法是根据所需顺序对 ArrayList 进行排序,将数组列表传递给 rearrangeTheArrayList 函数获取新的 ArrayList 并将其传递给回收器视图实现上述顺序的适配器。

 public ArrayList<Integer> rearrangeTheArrayList(ArrayList<Integer> integerArrayList) {
    ArrayList<Integer> resultArrayList = new ArrayList<>();
    int halfLength = 0;
    if (integerArrayList.size() % 2 == 1) {
        halfLength = (integerArrayList.size() / 2) + 1;
    } else {
        halfLength = integerArrayList.size() / 2;
    }

    for (int i = 0; i < halfLength; i++) {
        resultArrayList.add(integerArrayList.get(i));
        if ( (i + halfLength)<(integerArrayList.size() )) {
            resultArrayList.add(integerArrayList.get(i + halfLength));
        }
    }
    return resultArrayList;
}

GridLayoutManager 与LinearLayout 不同,默认方向是VERTICAL,所以你应该设置HORIZONTAL,意味着如果space 不够,更多的项目将水平排列,比如这个:

recyclerView.setLayoutManager(new GridLayoutManager(activity, 2, GridLayoutManager.HORIZONTAL, false));