1. 程式人生 > >recyclerview實現瀑布流佈局

recyclerview實現瀑布流佈局

先看效果,這裡我用的背景圖片,


首先匯入依賴

compile 'com.android.support:recyclerview-v7:26.1.0'
下面就是程式碼,首先是佈局
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bwie.com.pubuliu.MainActivity">

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

</RelativeLayout>


recycler_item_layout佈局
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView"
        android:textSize="30sp"
        android:gravity="center"
        android:layout_margin="3dp"
        android:background="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
</LinearLayout>
下面就是程式碼
首先是MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

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


public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<String> mDatas;
    private MyRecyclerViewAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.rv);
        //StaggeredGridLayoutManager瀑布流佈局
        StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);
        initData();
        adapter = new MyRecyclerViewAdapter(MainActivity.this, mDatas);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        mDatas = new ArrayList<String>();
        //用for迴圈來設定你要做多少個圖片,
        for (int i = 0; i < 100; i++){
            mDatas.add("i");
        }
    }

}
下面是MyRecyclerViewAdapter介面卡類
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;
import java.util.Random;

/**
 * Created by BAIPEI on 2017/12/5.
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<String> list;//資料
    private List<Integer> heightList;//裝產出的隨機數
    public MyRecyclerViewAdapter(Context context,List<String> list){
        this.context = context;
        this.list = list;
        //記錄為每個控制元件產生的隨機高度,避免滑回到頂部出現空白
        heightList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int height = new Random().nextInt(200) + 100;//[100,300)的隨機數
            heightList.add(height);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.recycler_item_layout,parent,false);
        return new MyViewHolder(view);//將佈局設定給holder
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        holder.itemView.setTag(list.get(position)+"");
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height=heightList.get(position);
        holder.itemView.setLayoutParams(params);
    }

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

    private class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public MyViewHolder(View view) {
            super(view);
            textView =itemView.findViewById(R.id.textView);
        }

    }
    interface OnRecyclerItemClickListener {
        public void onItemClick(View view, int position);
    }
}

一個簡單的瀑布流就出來了