recyclerview实现瀑布流布局

先看效果,这里我用的背景图片,

recyclerview实现瀑布流布局_第1张图片


首先导入依赖

compile 'com.android.support:recyclerview-v7:26.1.0'

下面就是代码,首先是布局
 
  
 
  
activity_main
 
  


    





recycler_item_layout布局
 
  

    
下面就是代码 
  
首先是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 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();
        //用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 list;//数据
    private List heightList;//装产出的随机数
    public MyRecyclerViewAdapter(Context context,List 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);
    }
}

一个简单的瀑布流就出来了





你可能感兴趣的:(recyclerview实现瀑布流布局)