Android之RecycleView(1)
Android之RecycleView(2)
瀑布流
PuRecycleViewActivity.java以及对应页面布局
package com.example.test0508.RecyclerView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.test0508.R;
public class PuRecycleViewActivity extends AppCompatActivity {
private RecyclerView mRvPu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pu_recycle_view);
mRvPu = findViewById(R.id.rv_pu);
mRvPu.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
mRvPu.setAdapter(new StagerredAdaptor(PuRecycleViewActivity.this));
mRvPu.addItemDecoration(new MyDecoration());
}
class MyDecoration extends RecyclerView.ItemDecoration{
@Override
public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int gap = getResources().getDimensionPixelSize(R.dimen.dividerHeight2) ;
outRect.set(gap,gap,gap,gap);
}
}
}
class StagerredAdaptor extends RecyclerView.Adapter<StagerredAdaptor.PuViewHolder>{
Context mContext;
public StagerredAdaptor(Context mContext) {
this.mContext = mContext;
}
@NonNull
@Override
public StagerredAdaptor.PuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new PuViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_pu_item,parent,false));
}
@Override
public void onBindViewHolder(@NonNull StagerredAdaptor.PuViewHolder holder, int position) {
if (position % 2 == 0){
Glide.with(mContext).load("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1589103035&di=dc1056a67b57cbfd46d2dd6f35a3f2ef&src=http://img.mingxing.com/upload/thumb/2015/09-25/0-eLomMg.jpg").into(holder.imageView);
}else {
holder.imageView.setImageResource(R.drawable.like_filled);
}
}
@Override
public int getItemCount() {
return 30;
}
class PuViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
public PuViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.iv_pu);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_pu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorGreen"
/>
</LinearLayout>
layout_pu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
>
<ImageView
android:id="@+id/iv_pu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
/>
</LinearLayout>