//item用卡片视图布局来做
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
android:background="@color/white"
app:layout_constraintCircleRadius="5dp">
<ImageView
android:id="@+id/pu_imagView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/p1"/>
</androidx.cardview.widget.CardView>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".recyclerView.PuRecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/puRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
public class PuRecyclerViewActivity extends AppCompatActivity {
private RecyclerView puRecyclerView;
private List<Integer> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pu_recycler_view);
puRecyclerView = findViewById(R.id.puRecyclerView);
data = new ArrayList<>();
data.add(R.drawable.p1);
data.add(R.drawable.p2);
data.add(R.drawable.p3);
data.add(R.drawable.p4);
data.add(R.drawable.p5);
data.add(R.drawable.p6);
data.add(R.drawable.p7);
data.add(R.drawable.p8);
data.add(R.drawable.p9);
puRecyclerView.setAdapter(new PuAdapter(data,pos -> {
Toast.makeText(this,"click "+pos,Toast.LENGTH_SHORT).show();
}));
puRecyclerView.setLayoutManager
(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
}
}