效果
实现代码
需要的jar包
com.android.support:recyclerview-v7:28.0.0
com.android.support:cardview-v7:28.0.0
activitymain的xml代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
android:layout_margin="5dp"
android:padding="4dp"
app:cardBackgroundColor="#ffffffff"
app:cardCornerRadius="15dp"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/MyRec"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</android.support.v7.widget.CardView>
bean包类
package com.example.administrator.pubuliu.bean;
public class MyData {
/**
* 名字
*/
private String name;
/**
* 图片id
*/
private int imageId;
public MyData(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
适配器的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88ffffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/MyImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/MyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
适配器
package com.example.administrator.pubuliu.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.pubuliu.R;
import com.example.administrator.pubuliu.bean.MyData;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private ArrayList<MyData> datas;
public MyAdapter(Context context, ArrayList<MyData> datas) {
this.context = context;
this.datas = datas;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//加载布局文件
View v = View.inflate(context, R.layout.item, null);
ViewHolder holder = new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
//对holder赋值
MyData myData = datas.get(i);
viewHolder.MyImg.setImageResource(myData.getImageId());
viewHolder.MyName.setText(myData.getName());
}
@Override
public int getItemCount() {
return datas.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView MyImg;
private TextView MyName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
MyImg = itemView.findViewById(R.id.MyImg);
MyName = itemView.findViewById(R.id.MyName);
}
}
}
activity的代码
package com.example.administrator.pubuliu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import com.example.administrator.pubuliu.adapter.MyAdapter;
import com.example.administrator.pubuliu.bean.MyData;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<MyData> datas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.MyRec);
//使用瀑布流 第一个参数为列数 第二个参数为排列方向
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
//初始化数据
initData();
//设置适配器
MyAdapter adapter = new MyAdapter(this,datas);
recyclerView.setAdapter(adapter);
}
private void initData() {
MyData myData;
//进行for循环添加数据
for (int i = 0; i < 20; i++) {
myData = new MyData("美女", R.drawable.a);
datas.add(myData);
myData = new MyData("美女", R.drawable.b);
datas.add(myData);
myData = new MyData("美女", R.drawable.c);
datas.add(myData);
myData = new MyData("美女", R.drawable.d);
datas.add(myData);
}
}
}
以上