RecyclerView之瀑布流
Fruit.Class FruitAdapter.Class activity_main.Xml和地址里的一样
http://blog.csdn.net/cccrunner/article/details/70500197
MainActivity Class
public class MainActivity extends AppCompatActivity { List<Fruit> fruitlist = new ArrayList<Fruit>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits(); RecyclerView recyclerView = (RecyclerView) findViewById(recycler_view); //创建StaggeredGridLayoutManager 的实例,构造函数第一个参数 用于指定布局的列数,传入3表示会把布局分成3列 //第二个参数用于指定布局的排列方向,传入StaggeredGridLayoutManager.VERTICAL表示会让布局纵向排列,
//最后把创建好的实例设置到recyclerView当中就可以了
StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager
(3,StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); FruitAdapter adapter = new FruitAdapter(fruitlist); recyclerView.setAdapter(adapter); for(int i=0;i<fruitlist.size();i++){ Log.i("info",fruitlist.get(i).getFruitImageId()+" "+fruitlist.get(i).getFruitName()); } } void initFruits(){ for(int i=0;i<2;i++){ Fruit apple = new Fruit(getRandomLengthName("Apple"),R.drawable.apple); fruitlist.add(apple); Fruit banana = new Fruit(getRandomLengthName("Banana"),R.drawable.banana); fruitlist.add(banana); Fruit cherry = new Fruit(getRandomLengthName("Cherry"),R.drawable.cherry); fruitlist.add(cherry); Fruit grape = new Fruit(getRandomLengthName("Grape"),R.drawable.grape); fruitlist.add(grape); Fruit mango = new Fruit(getRandomLengthName("Mango"),R.drawable.mango); fruitlist.add(mango); Fruit orange = new Fruit(getRandomLengthName("Orange"),R.drawable.orange); fruitlist.add(orange); Fruit pear = new Fruit(getRandomLengthName("Pear"),R.drawable.pear); fruitlist.add(pear); Fruit pineapple = new Fruit(getRandomLengthName("Pineapple"),R.drawable.pineapple); fruitlist.add(pineapple); Fruit strawberry = new Fruit(getRandomLengthName("Strawberry"),R.drawable.strawberry); fruitlist.add(strawberry); Fruit watermelon = new Fruit(getRandomLengthName("Watermelon"),R.drawable.watermelon); fruitlist.add(watermelon); } } //得到传入字符的随机数 private String getRandomLengthName(String name) { Random random = new Random(); int length = random.nextInt(20)+1; //创建1-20之间的随机数 StringBuilder builder = new StringBuilder(); for(int i=0;i<length;i++){ builder.append(name); } return builder.toString(); } }
fruit.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="vertical" > <ImageView android:id="@+id/fruit_image" android:src="@drawable/apple" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:text="苹果" android:textColor="#000" /> </LinearLayout>