強大的滾動控件——RecyclerView(三)瀑布流效果

RecyclerView將佈局排列交給了LayoutManager,LayoutManager中制定了一套可擴展的接口。
瀑布流的實現是使用了StaggeredGridLayoutManager
首先還是先來修改一下xml中的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">
    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>
    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginTop="10dp"/>


</LinearLayout>

看過我前幾章的朋友應該能看出來,我只是在width等地方動了一下

  • activity.class
       RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);




 private void initFruit() {
        for (int i =0;i<2;i++) {
            Fruit apple = new Fruit(getRandomLengthName("a"), R.mipmap.ic_launcher);
            fruitList.add(apple);
            Fruit banana = new Fruit(getRandomLengthName("banana"), R.mipmap.ic_launcher);
            fruitList.add(banana);
            Fruit oragen = new Fruit(getRandomLengthName("orange"), R.mipmap.ic_launcher);
            fruitList.add(oragen);
            Fruit watermelon = new Fruit(getRandomLengthName("watermelon"), R.mipmap.ic_launcher);
            fruitList.add(apple);
            Fruit pear = new Fruit(getRandomLengthName("pear"), R.mipmap.ic_launcher);
            fruitList.add(pear);
            Fruit grape = new Fruit(getRandomLengthName("grape"), R.mipmap.ic_launcher);
            fruitList.add(grape);
            Fruit pineapple = new Fruit(getRandomLengthName("pineapple"), R.mipmap.ic_launcher);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit(getRandomLengthName("strawberry"), R.mipmap.ic_launcher);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit(getRandomLengthName("cherry"), R.mipmap.ic_launcher);
            fruitList.add(cherry);
            Fruit a = new Fruit("a", R.mipmap.ic_launcher);
            fruitList.add(a);
            Fruit b = new Fruit("b", R.mipmap.ic_launcher);
            fruitList.add(b);
            Fruit c = new Fruit("c", R.mipmap.ic_launcher);
            fruitList.add(c);
            Fruit d = new Fruit("d", R.mipmap.ic_launcher);
            fruitList.add(d);
            Fruit e = new Fruit("e", R.mipmap.ic_launcher);
            fruitList.add(e);
            Fruit f = new Fruit("f", R.mipmap.ic_launcher);
            fruitList.add(f);
            Fruit g = new Fruit("g", R.mipmap.ic_launcher);
            fruitList.add(g);
            Fruit h = new Fruit("h", R.mipmap.ic_launcher);
            fruitList.add(h);
        }

    }

    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++){
            builder.append(name);
        }
        return builder.toString();

    }

在onCreat方法中我們創建了一個StaggeredGridLayoutManager的實例。它接收兩個參數,第一個是指定佈局的列數,第二個參數用於指定佈局的排列方向,傳入StaggeredGridLayoutManager.VERTICAL表示讓佈局縱向排列。

爲了使瀑布流更加直觀的呈現,使用了getRandomLengthName()這個方法,使用了Random對象來創建一個1到20之間的隨機數,然後將參數傳入的字符重複隨機遍

github地址:https://github.com/skysunlei/RecyclerViewBeat
效果圖
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章