1、先创建activity_grid.xml 和 activity_grid_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=".GridActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv2" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="103dp" tools:layout_editor_absoluteY="44dp" /> </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=".GridItemActivity"> <TextView android:id="@+id/textView3" android:layout_width="50dp" android:layout_height="50dp" android:text="@string/textview2" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </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.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class GridAdapter extends RecyclerView.Adapter<GridAdapter.GridViewHolder> { private Context context; public GridAdapter(Context context){ this.context = context; } @NonNull @Override public GridAdapter.GridViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new GridViewHolder(LayoutInflater.from(context).inflate(R.layout.activity_grid_item,parent,false)); } @Override public void onBindViewHolder(@NonNull GridAdapter.GridViewHolder holder, final int position) { holder.tv.setText("Hello world"); holder.tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context,"click..."+position,Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return 30; } class GridViewHolder extends RecyclerView.ViewHolder{ private TextView tv; public GridViewHolder(@NonNull View itemView) { super(itemView); tv = itemView.findViewById(R.id.textView3); } } }
3、GridActivity.java
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; public class GridActivity extends AppCompatActivity { private RecyclerView rv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grid); rv = findViewById(R.id.rv2); rv.setLayoutManager(new GridLayoutManager(GridActivity.this,3)); rv.setAdapter(new GridAdapter(GridActivity.this)); } }
4、效果: