Android StaggeredGridLayoutManager 水平流布局

**首先展示下布局页面Android StaggeredGridLayoutManager 水平流布局_第1张图片
**Android StaggeredGridLayoutManager 水平流布局_第2张图片
Android StaggeredGridLayoutManager 水平流布局_第3张图片
下面展示一些 主要代码。

RecyclerView recyclerView=(RecyclerView) findViewById(R.id.recycler_view);
        //这里可以设置 页面是垂直or水平滚动 调节最后的参数 HORIZONTAL 或 Veritcal
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.HORIZONTAL);
       // GridLayoutManager layoutManager =new GridLayoutManager(this,GridLayoutManager.HORIZONTAL,30, android.R.color.black);

        recyclerView.setLayoutManager(layoutManager);

        FruitAdapter adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);

这里的代码主要是创建StaggeredGridLayoutManage 实例并设参,第一个参数是设置有显示的有几行,把6和上面展示图片对比,可以发现一个页面刚好可以放6行,有兴趣也可以自己试试改几天数字,第二个参数 这里设置了水平滚动, 还有竖直滚动可以设置

new Fruit(getRandomLengthName("Grape"), R.drawable.grape_pic);
            fruitList.add(grape);
            Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"), R.drawable.pineapple_pic);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit(getRandomLengthName("Strawberry"), R.drawable.strawberry_pic);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit(getRandomLengthName("Cherry"), R.drawable.cherry_pic);
            fruitList.add(cherry);
            Fruit mango = new Fruit(getRandomLengthName("Mango"), R.drawable.mango_pic);
            fruitList.add(mango);
        }

    }

    private String getRandomLengthName(String name) {
        Random random = new Random();
      //  int length = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
      //  for (int i = 0; i < length; i++) {
        for (int i = 0; i < 1; i++) {
            builder.append(name);
        }
        return builder.toString();
    }

标注–学习交流代码修改参考自《第一行代码》
链接: https://download.csdn.net/download/qq_40514113/21045914

你可能感兴趣的:(安卓,复习笔记,android)