Recycler实现瀑布流

(开发环境Android studio)

首先,在开发环境中添加引用(在外层的build.gradle文件下的dependencies里面添加如下引用)

implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'

创造我们需要的Java Bean对象类 myDogs.java

对应代码如下:

public class myDogs {
    
    private String dogName;
    
    private int imgId;

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public int getImgId() {
        return imgId;
    }

    public void setImgId(int imgId) {
        this.imgId = imgId;
    }
}

对于我们的RecyclelerView子项,我们创建子项布局文件

my_dog_item.xml(对于子项,我们使用cardview布局,这也就是第刚开始的时候第二行引用的作用)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="5dp"
    android:padding="4dp"
    app:cardBackgroundColor="#ffffffff"
    app:cardCornerRadius="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/myDog_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/myDog_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>

其中:

cardCornerRadius="15dp"

保证每一个子项拥有15dp的圆角

然后创建适配器myDogAdapter.java从RecyclerView.Adapter<>继承

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

import java.util.List;

public class myDogAdapter extends RecyclerView.Adapter<myDogAdapter.myDogViewHolder> {

    private Context mcontext;

    private List<myDog> myDogs;

    public myDogAdapter(List<myDog> myDogs , Context context){
        this.myDogs = myDogs;
        this.mcontext = context;
    }

    @NonNull
    @Override
    public myDogAdapter.myDogViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_dog_item,viewGroup,false);
        return new myDogViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull myDogAdapter.myDogViewHolder holder, int position) {
        myDog myDogDemo = myDogs.get(position);
        holder.myDogImage.setImageResource(myDogDemo.getImgId());
        holder.myDogName.setText(myDogDemo.getDogName());
    }

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


    class myDogViewHolder extends RecyclerView.ViewHolder{
        ImageView myDogImage;
        TextView myDogName;
        public myDogViewHolder(@NonNull View itemView) {
            super(itemView);
            myDogImage = itemView.findViewById(R.id.myDog_image);
            myDogName = itemView.findViewById(R.id.myDog_name);
        }
    }
}

然后,编写MainActivity里面的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">

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

</LinearLayout>

这里面只放置了一个RecyclerView并让它填充布局

然后我们开始编写MainActivity.java里面的内容

package edu.henu.www.recyclelerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MainActivity extends AppCompatActivity {

    //创建与布局中相对应的对象;
    private RecyclerView recyclerView;
    //创建用于显示的对象集;
    private List<myDog> myDogs = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //将布局中相应的对象传入
        recyclerView = this.findViewById(R.id.recycler_view);

        //设置瀑布流核心代码,设置瀑布流列数,并设置瀑布流方向;
        StaggeredGridLayoutManager recyclerViewLayoutManager = new StaggeredGridLayoutManager(2 , StaggeredGridLayoutManager.VERTICAL);

        //设置布局管理器
        recyclerView.setLayoutManager(recyclerViewLayoutManager);

        //初始化对象;
        initDogs();

        //创建Adapter对象;
        myDogAdapter adapter = new myDogAdapter(this.myDogs , this);

        //设置适配器
        recyclerView.setAdapter(adapter);

    }

    private void initDogs(){
        myDog dog;
        for (int i = 0; i < 10; i++) {
            dog = new myDog("小狗狗" + i,R.drawable.d1);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i ,R.drawable.d2);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d3);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d4);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d5);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d6);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d6);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d7);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d8);
            myDogs.add(dog);
            dog = new myDog("小狗狗" + i,R.drawable.d9);
            myDogs.add(dog);
        }
    }


}

图片资源需要自己填充到Android项目目录下

 实现效果如图

 

 

参考文章:https://www.jianshu.com/p/02be426fda0a

 

posted @ 2019-04-05 13:49  阳光雨后  阅读(299)  评论(0编辑  收藏  举报