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. Unity 现代室内环境场景高效流程视频教程 附源文件 中文字幕

    大小1.83G,中文字幕 章节内容:1.项目介绍2.准备maya工程文件3.素材资产(assets)结构4.管理unity中的maya资产5.整合场景6.unity游戏对象及材质7.第一人称控制器8. ...

  2. JAVA:使用栈实现一个队列

    使用栈实现一个队列,需要弄清楚栈和队列的区别: 栈:先进后出: 队列:先进先出. 实现思路: 1)通过两个栈(pushStack / popStack)对倒,确保 popStack 栈的出栈顺序与队列 ...

  3. 单细胞数据整合方法 | Comprehensive Integration of Single-Cell Data

    操作代码:https://satijalab.org/seurat/ 依赖的算法 CCA CANONICAL CORRELATION ANALYSIS | R DATA ANALYSIS EXAMPL ...

  4. PyTorch 实战:计算 Wasserstein 距离

    PyTorch 实战:计算 Wasserstein 距离 2019-09-23 18:42:56 This blog is copied from: https://mp.weixin.qq.com/ ...

  5. 000 okhttp3的Get使用

    一:概述 1.说明 java与android都可以使用. 是网络请求的开源框架. square公司开发,用于替代HttpUrlConnection和Apache  HttpClient 2.优点 支持 ...

  6. String.format()详细用法

    String.format()字符串常规类型格式化的两种重载方式 format(String format, Object… args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字 ...

  7. laravel 解决 Please provide a valid cache path 问题

    这是因为laravel的缓存路径没有找到 laravel缓存文件路径是在 config/cache.php中设置,默认存在storage文件夹中 解决: 创建目录: mkdir -p storage/ ...

  8. vscode failed to excute git

    将代码提交到github时 提示 “failed to excute git” 解决:配置git git config --global user.email "youremailid@ex ...

  9. Ubuntu查看与结束任务进程

    1.打开终端 2.敲  ps -ef  查出进程的编号(就是PID那列) 3.输入 kill PID 即可(如果PID是123456,则kill 123456) 例如: 我想把splash关闭,直接输 ...

  10. @Component和@Configuration作为配置类的差别

    https://blog.csdn.net/long476964/article/details/80626930 虽然Component注解也会当做配置类,但是并不会为其生成CGLIB代理Class ...