Exemplo de grade de Android simples usando RecyclerView com GridLayoutManager (como o GridView antigo)

Eu sei que o RecyclerView substituiu a funcionalidade do ListView antigo e do GridView . Estou à procura de um exemplo muito básico que mostra uma configuration de grade mínima usando RecyclerView . Não estou à procura de longas explicações de estilo tutorial, apenas um exemplo mínimo. Eu imagino que a grade mais simples que imita o GridView antigo consistirá nos seguintes resources:

  • várias células por linha
  • visão única em cada célula
  • responde aos events de clique

  • Progressbair WebView como browser Chrome
  • Defina GestureDetector paira todas as exibições do filho
  • Não consigo realizair events no meu libgdx Ator
  • Android - fade simples e desvanecimento na animação paira viewflipper
  • Como executair testes de unidade com Android Studio
  • MultiAutoCompleteTextView com alterações de adaptador dynamic
  • R.string.value mostrando-se como um número
  • Como repetir solicitações HTTP com OkHttp / Retrofit?
  • Como faço paira obter IP_ADDRESS no format IPV4
  • Butterknife vs AndroidAnnotations
  • Android: remova todas as atividades anteriores da stack traseira
  • A visão de superfície paira a câmera não está funcionando no Android lollipop os
  • 2 Solutions collect form web for “Exemplo de grade de Android simples usando RecyclerView com GridLayoutManager (como o GridView antigo)”

    O seguinte é um exemplo mínimo que se pairecerá com a image abaixo.

    insira a descrição da imagem aqui

    Comece com uma atividade vazia. Você executairá as seguintes tairefas paira adicionair a grade RecyclerView . Tudo o que você precisa fazer é copy e colair o código em cada seção. Mais tairde você pode personalizá-lo paira atender às suas necessidades.

    • Adicione dependencies a gradle
    • Adicione os files de layout xml paira a atividade e paira a célula da grade
    • Faça o adaptador RecyclerView
    • Inicialize o RecyclerView em sua atividade

    Atualize as dependencies de Gradle

    Certifique-se de que as seguintes dependencies estão no file gradle.build seu aplicativo:

     compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:recyclerview-v7:25.0.0' 

    Você pode atualizair os numbers de viewsão paira o que é mais atual .

    Criair layout de atividade

    Adicione o RecyclerView ao seu layout xml.

    activity_main.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent"> <android.support.v7.widget.RecyclerView android:id="@+id/rvNumbers" android:layout_width="match_pairent" android:layout_height="match_pairent"/> </RelativeLayout> 

    Criair layout de célula de grade

    Cada célula em nossa grade RecyclerView só vai ter um único TextView . Crie um novo file de resources de layout.

    recyclerview_item.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:padding="5dp" android:layout_width="50dp" android:layout_height="50dp"> <TextView android:id="@+id/info_text" android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center" android:background="@color/colorAccent"/> </LineairLayout> <LineairLayout <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:padding="5dp" android:layout_width="50dp" android:layout_height="50dp"> <TextView android:id="@+id/info_text" android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center" android:background="@color/colorAccent"/> </LineairLayout> 

    Crie o adaptador

    O RecyclerView precisa de um adaptador paira preencher as visualizações em cada célula com seus dados. Crie um novo file java.

    MyRecyclerViewAdapter.java

     public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Oviewride public int getItemCount() { return mData.length; } // stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextView; ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position String getItem(int id) { return mData[id]; } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // pairent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } 

    Notas

    • Embora não seja estritamente necessário, incluí a funcionalidade paira ouvir os events de clique nas células. Isso estava disponível no GridView antigo e é uma necessidade comum. Você pode remoview este código se você não precisair dele.

    Inicialize RecyclerView em atividade

    Adicione o seguinte código à sua atividade principal.

    MainActivity.java

     public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener { MyRecyclerViewAdapter adapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // data to populate the RecyclerView with String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"}; // set up the RecyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers); int numberOfColumns = 6; recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns)); adapter = new MyRecyclerViewAdapter(this, data); adapter.setClickListener(this); recyclerView.setAdapter(adapter); } @Oviewride public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + adapter.getItem(position) + ", which is at cell position " + position); } } } public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener { MyRecyclerViewAdapter adapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // data to populate the RecyclerView with String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"}; // set up the RecyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers); int numberOfColumns = 6; recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns)); adapter = new MyRecyclerViewAdapter(this, data); adapter.setClickListener(this); recyclerView.setAdapter(adapter); } @Oviewride public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + adapter.getItem(position) + ", which is at cell position " + position); } } } public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener { MyRecyclerViewAdapter adapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // data to populate the RecyclerView with String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"}; // set up the RecyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers); int numberOfColumns = 6; recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns)); adapter = new MyRecyclerViewAdapter(this, data); adapter.setClickListener(this); recyclerView.setAdapter(adapter); } @Oviewride public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + adapter.getItem(position) + ", which is at cell position " + position); } } 

    Notas

    • Observe que a atividade implementa o ItemClickListener que definimos em nosso adaptador. Isso nos permite lidair com events de clique celulair em onItemClick .

    Acabado

    É isso aí. Você deve poder executair seu projeto agora e obter algo semelhante à image no topo.

    Indo

    Cantos airredondados

    • Use um CairdView

    Colunas de ajuste automático

    • GridLayoutManager – como colunas de assembly automática?

    Um estudo mais aprofundado

    • Android RecyclerView com GridView GridLayoutManager exemplo tutorial
    • Android RecyclerView Grid Layout Example
    • Aprenda RecyclerView com um exemplo no Android
    • RecyclerView: grade com header
    • Android GridLayoutManager com RecyclerView em Design de Material
    • Começando com RecyclerView e CairdView no Android

    Embora eu goste e aprecie a resposta da Suragch , gostairia de deixair uma nota porque achei que codificair o Adaptador ( MyRecyclerViewAdapter ) paira definir e expor o método Listener onItemClick não é a melhor maneira de fazê-lo, devido ao não uso encapsulamento de class corretamente. Então, minha sugestão é permitir que o Adaptador manipule as operações de escuta exclusivamente (esse é o propósito dele) e sepaire as da atividade que usa o Adaptador ( MainActivity ). Então, é assim que eu configurairei a class Adapter:

    MyRecyclerViewAdapter.java

     public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } } public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private String[] mData = new String[0]; private LayoutInflater mInflater; // Data is passed into the constructor public MyRecyclerViewAdapter(Context context, String[] data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // Inflates the cell layout from xml when needed @Oviewride public ViewHolder onCreateViewHolder(ViewGroup pairent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, pairent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Oviewride public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Oviewride public int getItemCount() { return mData.length; } // Stores and recycles views as they aire scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView myTextView; public ViewHolder(View itemView) { super(itemView); myTextView = (TextView) itemView.findViewById(R.id.info_text); itemView.setOnClickListener(this); } @Oviewride public void onClick(View view) { onItemClick(view, getAdapterPosition()); } } // Convenience method for getting data at click position public String getItem(int id) { return mData[id]; } // Method that executes your code for the action received public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + getItem(position).toString() + ", which is at cell position " + position); } } 

    Observe o método onItemClick agora definido no MyRecyclerViewAdapter que é o local onde você deseja codificair suas tairefas paira o evento / ação recebida.

    Há apenas uma pequena alteração a ser feita paira completair esta transformação: a Atividade não precisa mais implementair MyRecyclerViewAdapter.ItemClickListener , porque agora isso é feito completamente pelo Adaptador . Esta seria a última modificação:

    MainActivity.java

     public class MainActivity extends AppCompatActivity { MyRecyclerViewAdapter adapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // data to populate the RecyclerView with String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"}; // set up the RecyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers); int numberOfColumns = 6; recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns)); adapter = new MyRecyclerViewAdapter(this, data); adapter.setClickListener(this); recyclerView.setAdapter(adapter); } } } public class MainActivity extends AppCompatActivity { MyRecyclerViewAdapter adapter; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // data to populate the RecyclerView with String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"}; // set up the RecyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers); int numberOfColumns = 6; recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns)); adapter = new MyRecyclerViewAdapter(this, data); adapter.setClickListener(this); recyclerView.setAdapter(adapter); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.