附上效果图

1.先加入需要的依赖

2.瀑布流的适配器

3.修改图片圆角类

4.创建一个实体类

5.MainActivity的主页代码

6.MainActivity的布局文件

7.列表布局文件

8.最后附上项目

加入需要的依赖

implementation 'com.android.support:recyclerview-v7:25.3.1' //设置瀑布流列表
implementation 'com.github.bumptech.glide:glide:4.9.0' //获取图片以及圆角 implementation 'com.google.android.material:material:1.0.0' //卡片效果

 在加入网络权限

<uses-permission android:name="android.permission.INTERNET"/>

瀑布流的适配器

 1 public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
 2
 3     private Context context;
 4     private List<Menu> list;//数据
 5     private List<Integer> heightList;//装产出的随机数
 6
 7     public RecyclerViewAdapter(Context context, List<Menu> list) {
 8         this.context = context;
 9         this.list = list;
10         //记录为每个控件产生的随机高度,避免滑回到顶部出现空白
11         heightList = new ArrayList<>();
12         for (int i = 0; i < list.size(); i++) {
13             int height = new Random().nextInt(300) + 500;//[100,300)的随机数
14             heightList.add(height);
15         }
16     }
17
18     @Override
19     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
20         //找到item的布局
21         View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
22         return new MyViewHolder(view);//将布局设置给holder
23     }
24
25     @Override
26     public int getItemCount() {
27         return list.size();
28     }
29
30     @Override
31     public void onBindViewHolder(final MyViewHolder holder, final int position) {
32         //填充数据
33         RequestOptions options = new RequestOptions()
34                 .centerCrop()
35                 .placeholder(R.mipmap.ic_launcher_round) //预加载图片
36                 .error(R.drawable.ic_launcher_foreground) //加载失败图片
37                 .priority(Priority.HIGH) //优先级
38                 .diskCacheStrategy(DiskCacheStrategy.NONE) //缓存
39                 .transform(new GlideRoundTransform(5)); //圆角
40         Glide.with(context).load(list.get(position).getImage()).apply(options).into(holder.imageView);
41         holder.menu.setText(list.get(position).getName());
42         holder.price.setText(list.get(position).getPrice());
43         //由于需要实现瀑布流的效果,所以就需要动态的改变控件的高度了
44         ViewGroup.LayoutParams params = holder.imageView.getLayoutParams();
45         params.height = heightList.get(position);
46         holder.imageView.setLayoutParams(params);
47
48     }
49
50     class MyViewHolder extends RecyclerView.ViewHolder {
51         ImageView imageView;
52         TextView menu,price;
53
54         public MyViewHolder(View itemView) {
55             super(itemView);
56             imageView = (ImageView) itemView.findViewById(R.id.imageView);
57             menu = (TextView)itemView.findViewById(R.id.text_menu);
58             price = (TextView)itemView.findViewById(R.id.text_money);
59         }
60     }
61 }

修改图片圆角类

public class GlideRoundTransform extends BitmapTransformation {
    private static float radius = 0f;

    public GlideRoundTransform() {
        this(4);
    }

    public GlideRoundTransform(int dp) {
        super();
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }


    @Override
    protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
        //变换的时候裁切
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
        return roundCrop(pool, bitmap);
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {

    }


    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) {
            return null;
        }
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        //左上角、右上角圆角
//        RectF rectRound = new RectF(0f, 100f, source.getWidth(), source.getHeight());
//        canvas.drawRect(rectRound, paint);
        return result;
    }
}

创建一个实体类

5.MainActivity的主页代码

6.MainActivity的布局文件

7.列表布局文件

8.最后附上项目

01-24 05:13