1、先创建 activity_water_fall.xml 和 activity_water_fall_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".WaterFallActivity"> <androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="207dp"
tools:layout_editor_absoluteY="62dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:layout_height="wrap_content"
tools:context=".WaterFallItemActivity"> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/ivdesc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/p1" />
</androidx.constraintlayout.widget.ConstraintLayout>

2、创建Adapter

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import androidx.recyclerview.widget.RecyclerView; public class WaterFallAdapter extends RecyclerView.Adapter<WaterFallAdapter.WaterFallViewHolder> {
private Context context; public WaterFallAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public WaterFallAdapter.WaterFallViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new WaterFallViewHolder(LayoutInflater.from(context).inflate(R.layout.activity_water_fall_item,parent,false));
} @Override
public void onBindViewHolder(@NonNull WaterFallAdapter.WaterFallViewHolder holder, int position) {
if(position %2==0) {
holder.iv.setImageResource(R.drawable.p1);
}else{
holder.iv.setImageResource(R.drawable.p2);
}
} @Override
public int getItemCount() {
return 60;
} class WaterFallViewHolder extends RecyclerView.ViewHolder{ private ImageView iv;
public WaterFallViewHolder(@NonNull View itemView) {
super(itemView); iv = itemView.findViewById(R.id.imageView);
}
}
}

3、WaterFallActivity.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager; import android.os.Bundle; public class WaterFallActivity extends AppCompatActivity {
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water_fall); rv = findViewById(R.id.rv3);
rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
rv.setAdapter(new WaterFallAdapter(WaterFallActivity.this));
}
}

ps:图片路径,直接复制到drawable目录里面:

4、效果:

android RecyclerView的瀑布流布局案例的更多相关文章

  1. Android RecyclerView(瀑布流)水平/垂直方向分割线

     Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...

  2. RecyclerView实现瀑布流布局

    RecyclerView本身提供了三个LayoutManager的实现 LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager ...

  3. android RecyclerView的Grid布局案例

    1.先创建activity_grid.xml 和 activity_grid_item.xml <?xml version="1.0" encoding="utf- ...

  4. android RecyclerView的Linear布局案例

    1.先创建 activity_recycle_view.xml 和 activity_recycler_linear_item.xml 如下: <?xml version="1.0&q ...

  5. 基于RecyclerView的瀑布流实现

    fragment的布局: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  6. 【Android】14.0 UI开发(五)——列表控件RecyclerView的瀑布布局排列实现

    1.0 列表控件RecyclerView的瀑布布局排列实现,关键词StaggeredGridLayoutManager LinearLayoutManager 实现顺序布局 GridLayoutMan ...

  7. JS案例之6——瀑布流布局(1)

    在实际的项目中,偶尔会用到一种布局——瀑布流布局.瀑布流布局的特点是,在多列布局时,可以保证内容区块在水平方向上不产生大的空隙,类似瀑布的效果.简单的说,在垂直列表里,内容区块是一个挨着一个的.当内容 ...

  8. Android RecyclerView初体验

    很早之前就听说过RecyclerView这个组件了,但一直很忙没时间学习.趁着周末,就花了一天时间来学习RecyclerView. 准备工作 在Android Studio里新建一个Android项目 ...

  9. Android RecyclerView与ListView比较

    RecyclerView 概述 RecyclerView 集成自 ViewGroup .RecyclerView是Android-support-V7版本中新增的一个Widgets,官方对于它的介绍是 ...

随机推荐

  1. javassist使用全解析

    Java 字节码以二进制的形式存储在 .class 文件中,每一个 .class 文件包含一个 Java 类或接口.Javaassist 就是一个用来 处理 Java 字节码的类库.它可以在一个已经编 ...

  2. Cucumber介绍

    Cucumber是一个提供能让我们都理解的普通语言,通过普通语言来描述的测试用例,并支持行为驱动开发的测试工具.Cucumber支持大多数变成语言,如Ruby.Java和Python等. 官方地址:h ...

  3. LinQ中List,取某个字段,然后用逗号拼接

    string htDetails = string.Join(",", DemoList.Select(t => t.id).Distinct().ToArray());

  4. js 模糊搜索

    function fuzzysearch (needle, haystack) { var hlen = haystack.length; var nlen = needle.length; if ( ...

  5. [LeetCode] 6. ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  6. 本地dev环境,运行时用node模块自动读取并整合文件

    const http = require('http'); const fs = require('fs'); const path = require('path'); const glob = r ...

  7. linux线程间同步方式总结梳理

    线程间一般无需特别的手段进行通信,由于线程间能够共享数据结构,也就是一个全局变量能够被两个线程同时使用.只是要注意的是线程间须要做好同步! 使用多线程的理由: 1. 一个是和进程相比,它是一种非常&q ...

  8. 【vim小小记】vim的复制粘贴(包括系统剪贴板)

    1.vim常用复制粘贴命令 Vim的复制粘贴命令无疑是y (yank),p(paster),加上yy,P PS: vim有个很有意思的约定(我觉得是一种约定),就是某个命令的大小写都是实现某种功能,只 ...

  9. [十一集训] Day1 (2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018))

    A Altruistic Amphibians 原题 题目大意: n只青蛙在高度为d的井中,每只有跳跃距离.重量和高度,每只青蛙可以借助跳到别的青蛙的背上而跳出井,每只青蛙能承受的最大重量是自身重量, ...

  10. 1.jvm思维导图

    点击图片可查看高清图