使用RecyclerView和GridLayoutManager的简单的Android网格示例(如旧的GridView)

我知道RecyclerView已经replace了旧的ListViewGridView 。 我正在寻找一个非常基本的例子,显示使用RecyclerView最小的网格设置。 我不是在寻找长时间的教程风格的解释,只是一个简单的例子。 我想象模仿旧的GridView最简单的网格将包括以下function:

  • 每行多个单元格
  • 每个单元格中的单个视图
  • 响应点击事件

以下是一个最小的例子,看起来像下面的图片。

在这里输入图像描述

开始一个空的活动。 您将执行以下任务来添加RecyclerView网格。 所有你需要做的是复制和粘贴每个部分的代码。 稍后,您可以对其进行定制以适应您的需求。

  • 添加依赖到gradle
  • 添加活动和网格单元格的XML布局文件
  • 制作RecyclerView适配器
  • 在您的活动中初始化RecyclerView

更新Gradle依赖关系

确保以下依赖关系在您的应用程序gradle.build文件中:

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

您可以将版本号更新为最新的版本号。

创build活动布局

RecyclerView添加到您的xml布局。

activity_main.xml中

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

创build网格单元布局

我们的RecyclerView网格中的每个单元格都将只有一个TextView 。 创build一个新的布局资源文件。

recyclerview_item.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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_parent" android:layout_height="match_parent" android:gravity="center" android:background="@color/colorAccent"/> </LinearLayout> 

创build适配器

RecyclerView需要一个适配器来填充数据的每个单元格中的视图。 创build一个新的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 @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, parent, false); return new ViewHolder(view); } // binds the data to the textview in each cell @Override public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // total number of cells @Override public int getItemCount() { return mData.length; } // stores and recycles views as they are 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); } @Override 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; } // parent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } } 

笔记

  • 虽然不是绝对必要的,但是我还包含了监听单元格上的单击事件的function。 这在旧的GridView是可用的,并且是常见的需求。 如果你不需要,你可以删除这段代码。

在Activity中初始化RecyclerView

将以下代码添加到您的主要活动中。

MainActivity.java

 public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener { MyRecyclerViewAdapter adapter; @Override 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); } @Override public void onItemClick(View view, int position) { Log.i("TAG", "You clicked number " + adapter.getItem(position) + ", which is at cell position " + position); } } 

笔记

  • 请注意,该活动实现了我们在适配器中定义的ItemClickListener 。 这使我们能够处理onItemClick单元格单击事件。

成品

而已。 你现在应该可以运行你的项目,并得到类似于顶部图像的东西。

继续

圆angular

  • 使用CardView

自动配合色谱柱

  • GridLayoutManager – 如何自动适应列?

进一步研究

  • 使用GridView GridLayoutManager示例教程的Android RecyclerView
  • Android RecyclerView网格布局示例
  • 通过Android中的示例学习RecyclerView
  • RecyclerView:带表头的网格
  • 在材质devise中使用RecyclerView的Android GridLayoutManager
  • 在Android上开始使用RecyclerView和CardView

虽然我喜欢并赞赏Suragch的回答 ,但我想留下一个注释,因为我发现编码适配器MyRecyclerViewAdapter )来定义和公开侦听器方法onItemClick不是最好的方式来做到这一点,因为没有使用类封装正确。 所以我的build议是让适配器单独处理Listening操作(这是他的目的!),并将它们与使用AdapterMainActivity )的Activity分开。 所以这就是我将如何设置适配器类:

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 @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.recyclerview_item, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } // Binds the data to the textview in each cell @Override public void onBindViewHolder(ViewHolder holder, int position) { String animal = mData[position]; holder.myTextView.setText(animal); } // Total number of cells @Override public int getItemCount() { return mData.length; } // Stores and recycles views as they are 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); } @Override 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); } } 

请注意,现在在MyRecyclerViewAdapter中定义的onItemClick方法是您希望为收到的事件/操作编写任务的地方。

为了完成这个转换,只需做很小的改动: Activity不需要再实现MyRecyclerViewAdapter.ItemClickListener了,因为现在完全由Adapter完成了。 这将是最后的修改:

MainActivity.java

 public class MainActivity extends AppCompatActivity { MyRecyclerViewAdapter adapter; @Override 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); } }