android横向瀑布流布局,android RecyclerView的瀑布流布局案例
1、先创建activity_water_fall.xml 和activity_water_fall_item.xmlxmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:..
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目录里面:
4、效果:
标签:androidx,WaterFallViewHolder,案例,import,android,RecyclerView,public
来源: https://www.cnblogs.com/zoro-zero/p/11407884.html
更多推荐
所有评论(0)