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

时间:2019-02-26 07:17:50

标签: android android-recyclerview grid-layout gridlayoutmanager

我有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
....

这不是我想要的输出

4 个答案:

答案 0 :(得分:1)

最简单的方法是按照所需的顺序对ArrayList进行排序,将您的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;
}

答案 1 :(得分:0)

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

Draw order

答案 2 :(得分:0)

您可以实现这样的逻辑。会顺利运行

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

答案 3 :(得分:0)

GridLayoutManager 与 LinearLayout 不同,默认方向是 VERTICAL,所以你应该设置 HORIZONTAL,意思是如果空间不够,更多的项目会水平排列,如下所示:

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