1. 程式人生 > >Android RecyclerView 的基本使用--瀑布流

Android RecyclerView 的基本使用--瀑布流

前面兩篇文章主要講了用RecyclerView實現ListView和GridView,這篇文章主要探討一下RecyclerView 的 瀑布流使用。利用RecyclerView可以很方便地實現瀑布流。那麼如何實現?其實你什麼都不用做,只要使用StaggeredGridLayoutManager我們就已經實現了,只是上面的item佈局我們使用了固定的高度,下面我們僅僅在介面卡的onBindViewHolder方法中為我們的item設定個隨機的高度。具體方法如下:

1.主Activity的佈局和item的佈局如下:

    主Activity的佈局沒有變化     item的佈局為了明顯,加了個margin和設定了background屬性。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ahuang.recyclerview.StaggeredViewActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
</pre><pre code_snippet_id="1561535" snippet_file_name="blog_20160121_3_8563476" name="code" class="java"><pre name="code" class="java"><?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:background="#FFC125"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:text="1"
        />
</LinearLayout>


2.介面卡程式碼如下:   主要是定義了一個list存放高度資訊,隨機生成高度存放在List中。   在onBindViewHolder()裡獲得控制元件屬性,並將隨機生成的高度資訊作用於控制元件的高度中。   然後在新增資料。
package com.example.ahuang.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ahuang on 2016/1/19.
 */
public class StaggeredHomeAdapter extends RecyclerView.Adapter<StaggeredHomeAdapter.MyViewHolder>{

    Context context;
    List<String> list;

    private List<Integer> mHeight;  //  定義了一個集合,存放高度資訊

    public  StaggeredHomeAdapter(Context context,List<String> list){
        this.context=context;
        this.list=list;
        mHeight=new ArrayList<Integer>();
        for(int i=0;i<list.size();i++){
            mHeight.add((int)(100+Math.random()*300));       //對高度資訊隨機賦值
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder=new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.lv_item_layout,parent,false));

        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ViewGroup.LayoutParams lp=holder.tv.getLayoutParams();  //獲取TextView的屬性
        lp.height=mHeight.get(position);                  //獲得高度屬性,並將隨機生成的高度屬性作用於高度
        holder.tv.setLayoutParams(lp);                      //設定新的屬性
        holder.tv.setText(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv=(TextView) itemView.findViewById(R.id.textView);
        }
    }
}
3.主Activity中實現。   新增一下程式碼即可實現。
mRecyclerView=(RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

mRecyclerView.setAdapter(new StaggeredHomeAdapter(this, list));

效果圖                

ItemAnimator

RecyclerView 可以配置item增加刪除的動畫。

預設情況下,RecyclerView使用的是

mRecyclerView.setItemAnimator(new DefaultItemAnimator());