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

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">

android:id="@+id/rv3"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:layout_editor_absoluteX="207dp"

tools:layout_editor_absoluteY="62dp" />

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">

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" />

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 {

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目录里面:

0f7feebce2201b83894d17925bf9a241.png

4、效果:

c57d1ad2b547f8defa07333c60507cad.png

标签:androidx,WaterFallViewHolder,案例,import,android,RecyclerView,public

来源: https://www.cnblogs.com/zoro-zero/p/11407884.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐