android RecyclerView的瀑布流布局案例
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的瀑布流布局案例的更多相关文章
- Android RecyclerView(瀑布流)水平/垂直方向分割线
Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...
- RecyclerView实现瀑布流布局
RecyclerView本身提供了三个LayoutManager的实现 LinearLayoutManager GridLayoutManager StaggeredGridLayoutManager ...
- android RecyclerView的Grid布局案例
1.先创建activity_grid.xml 和 activity_grid_item.xml <?xml version="1.0" encoding="utf- ...
- android RecyclerView的Linear布局案例
1.先创建 activity_recycle_view.xml 和 activity_recycler_linear_item.xml 如下: <?xml version="1.0&q ...
- 基于RecyclerView的瀑布流实现
fragment的布局: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...
- 【Android】14.0 UI开发(五)——列表控件RecyclerView的瀑布布局排列实现
1.0 列表控件RecyclerView的瀑布布局排列实现,关键词StaggeredGridLayoutManager LinearLayoutManager 实现顺序布局 GridLayoutMan ...
- JS案例之6——瀑布流布局(1)
在实际的项目中,偶尔会用到一种布局——瀑布流布局.瀑布流布局的特点是,在多列布局时,可以保证内容区块在水平方向上不产生大的空隙,类似瀑布的效果.简单的说,在垂直列表里,内容区块是一个挨着一个的.当内容 ...
- Android RecyclerView初体验
很早之前就听说过RecyclerView这个组件了,但一直很忙没时间学习.趁着周末,就花了一天时间来学习RecyclerView. 准备工作 在Android Studio里新建一个Android项目 ...
- Android RecyclerView与ListView比较
RecyclerView 概述 RecyclerView 集成自 ViewGroup .RecyclerView是Android-support-V7版本中新增的一个Widgets,官方对于它的介绍是 ...
随机推荐
- Spark2.x(五十七):User capacity has reached its maximum limit(用户容量已达到最大限制)
背景: 目前服务器资源是43个节点,每个节点配置信息如下:24VCores 64G yarn配置情况: yarn.scheduler.minimum-allocation-mb 单个容器可申请的最小 ...
- GWAS Catalog数据库简介
GWAS Catalog The NHGRI-EBI Catalog of published genome-wide association studies EBI负责维护的一个收集已发表的GWAS ...
- ssh密匙互信操作【原创】
1.简便ssh密匙信任方法 只在一台服务器上创建ssh-keygen [root@SMSJKSRVBJ02 ~]# ssh-keygen Generating public/private rsa k ...
- C++ Java throw goto
throw goto - 国内版 Binghttps://cn.bing.com/search?FORM=U227DF&PC=U227&q=throw+goto C++ throw 代 ...
- Eclipse新项目检出后报错第一步:导入lib中的jar包【我】
新检出项目报错,第一步,先看项目 web-info下的 lib目录里的包是不是都添加到项目构建中了,可以全选先添加到项目构建中,看项目是否还在报错.
- [LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...
- java打包小记
1.一个类的 Hello.java class Hello{ public static void main(String[] agrs){ System.out.println("hell ...
- 在使用FPGA来控制DDR3/DDR2 IP 的时候两个错误的解决办法
对于熟悉Intel FPGA的老(gong)司(cheng)机(shi)来说,外部存储器的控制早已是轻车熟路,但是对于新手,DDR3/DDR2 的IP使用也许并没有那么简单,不过没关系,骏龙的培训网站 ...
- Python3中strip()、lstrip()、rstrip()用法详解
Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格) lstrip:用来去除开头字符.空白 ...
- SpringBoot系列教程web篇之Beetl环境搭建
前面两篇分别介绍了目前流行的模板引擎Freemaker和Thymeleaf构建web应用的方式,接下来我们看一下号称性能最好的国产模板引擎Beetl,如何搭建web环境 本文主要来自官方文档,如有疑问 ...