欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

recyclerview实现瀑布流布局

程序员文章站 2022-06-08 16:30:18
...

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

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);
    }
}

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