ReCyclerView实现瀑布流布局

2017-05-15  本文已影响0人  willho

一.导入ReCyclerView包到项目

打开工程目录

添加compile'com.android.support:recyclerview-v7:25.0.1'

0

添加

二.创建实体类

类似自定义listview

3.创建适配器

1.继承于RecyclerView.Adapter<Adapter.ViewHolder>

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>

2.创建静态内部类ViewHolder extends RecyclerView.ViewHolder

static classViewHolderextendsRecyclerView.ViewHolder

3.重写onCreateViewHolder方法(设置事件监听)

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent,intviewType)

4.重写onBindViewHolder方法

@Override

public void onBindViewHolder(ViewHolder holder,intposition)

5.重写getItemCount()方法

@Override

public int getItemCount()

完整代码

package com.example.hp.c3title;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.StaggeredGridLayoutManager;import com.example.hp.c3title.Fruit;import java.util.ArrayList;import java.util.List;import java.util.Random;public class MainActivity extends AppCompatActivity {    private ListfruitList = new ArrayList();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initFruits();

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 initFruits() {

for (int i = 0; i < 2; i++) {

Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple_pic);

fruitList.add(apple);

Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana_pic);

fruitList.add(banana);

Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.orange_pic);

fruitList.add(orange);

Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.watermelon_pic);

fruitList.add(watermelon);

Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.pear_pic);

fruitList.add(pear);

Fruit grape = 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++) {

builder.append(name);

}

return builder.toString();

}

}

三.MianActivity中实例化

1.实例化RecyclerView

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

2.创建并设置布局管理器

(可用其他布局 StaggeredGridLayoutManager为瀑布流)

参数3为创建3列的瀑布流,

StaggeredGridLayoutManager layoutManager =new

StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);

recyclerView.setLayoutManager(layoutManager);

3.创建并设置适配器

FruitAdapter adapter =newFruitAdapter(fruitList);

recyclerView.setAdapter(adapter);

完整代码

package com.example.recyclerviewtest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.StaggeredGridLayoutManager;import java.util.ArrayList;import java.util.List;import java.util.Random;public class MainActivity extends AppCompatActivity {    private ListfruitList = new ArrayList();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initFruits();

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 initFruits() {

for (int i = 0; i < 2; i++) {

Fruit apple = new Fruit(getRandomLengthName("Apple"), R.drawable.apple_pic);

fruitList.add(apple);

Fruit banana = new Fruit(getRandomLengthName("Banana"), R.drawable.banana_pic);

fruitList.add(banana);

Fruit orange = new Fruit(getRandomLengthName("Orange"), R.drawable.orange_pic);

fruitList.add(orange);

Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"), R.drawable.watermelon_pic);

fruitList.add(watermelon);

Fruit pear = new Fruit(getRandomLengthName("Pear"), R.drawable.pear_pic);

fruitList.add(pear);

Fruit grape = 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++) {

builder.append(name);

}

return builder.toString();

}

}

上一篇 下一篇

猜你喜欢

热点阅读