android RecyclerView的瀑布流布局案例

android RecyclerView的瀑布流布局案例,第1张

概述1、先创建 activity_water_fall.xml和 activity_water_fall_item.xml<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apkes/android"xml

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的瀑布流布局案例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110377.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存