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. 微信小程序上架需要增值电信业务经营许可证ICP?

    很多小程序的开发者最近都遇到了类似的问题,那就是辛辛苦苦开发出来的小程序上线不了,要求提供一些特殊资质,比方说:增值电信业务许可证(下面有数十种分类),网络文化经营许可证等类似证件,这对于创业团队来说 ...

  2. Android 系统属性-SystemProperties详解***

    创建与修改android属性用Systemproperties.set(name, value),获取android属性用Systemproperties.get(name),需要注意的是androi ...

  3. 第2课第5节_Java面向对象编程_异常_P【学习笔记】

    摘要:韦东山android视频学习笔记  java的异常处理的原则如下: 1.我们先写一个没有对异常处理的程序,在进行除法运算的时候,除数是非零的话,运行时没有问题的,但是除数为零的时候,运行就会有问 ...

  4. mysql skip-grant-tables 后要多次重启 和验证登录检查确认密码生效

    mysql  skip-grant-tables 后要多次重启  和验证登录检查确认密码生效

  5. SUSE操作系统,如何查看操作系统版本?

    背景描述: 今天需要统计操作系统版本,我在其中一台主机上执行cat /etc/redhat-release发现没有这个,应该知道不是redhat系统,然后想,怎么查来着,忘了,找了下,再此记录下. # ...

  6. Docker CentOS / Ubuntu容器开启 SSH 服务

    Docker CentOS / Ubuntu容器开启 SSH 服务 在CentOS容器内执行 yum install passwd openssl openssh-server -y # Ubuntu ...

  7. openstack虚拟机获取不到ip

    一.现象描述: openstack平台中创建虚拟机后,虚拟机在web页面中显示获取到了ip,但是打开虚拟机控制台后查看网络状态,虚拟机没有ip地址,下图为故障截图: 二.分析思路: (1)查看neut ...

  8. [LeetCode] 62. Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. [LeetCode] 218. The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  10. 高级UI-自定义控件

    自定义控件在Android开发中有着大量的运用,为了做出符合项目的效果很多时候需要自定义控件,这里就使用两个自定义控件,来说明自定义控件的使用流程 仿QQ侧滑 之前使用DrawerLayout和Nav ...