GridRecyclerViewActivity
package com.example.firstapp.recyclerview; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.graphics.Rect; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.example.firstapp.R; public class GridRecyclerViewActivity extends AppCompatActivity { private RecyclerView mRvGrid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_grid_recycler_view ); mRvGrid=findViewById( R.id.rv_grid ); mRvGrid.setLayoutManager( new GridLayoutManager( GridRecyclerViewActivity.this,3 ) );//可以规定你一行想展示几个 mRvGrid.setAdapter( new GridAdapter( GridRecyclerViewActivity.this, new GridAdapter.OnItemClickListener() { @Override public void onclick(int pos) { Toast.makeText( GridRecyclerViewActivity.this, "click"+pos, Toast.LENGTH_SHORT ).show(); } } ) ); } }