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


227

我知道这RecyclerView已经取代了旧版ListView和的功能GridView。我正在寻找一个非常基本的示例,该示例显示了使用的最小网格设置RecyclerView。我不是在寻找冗长的教程风格说明,而只是一个最小的示例。我认为模仿旧版GridView的最简单的网格将包含以下功能:

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

Answers:


556

简短答案

对于已经熟悉设置RecyclerView列表的人来说,好消息是创建网格的过程基本相同。你只需要使用一个GridLayoutManager,而不是LinearLayoutManager当你设置RecyclerView了。

recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));

如果您需要更多帮助,请查看以下示例。

完整的例子

以下是一个最小示例,如下图所示。

在此处输入图片说明

从一个空的活动开始。您将执行以下任务来添加RecyclerView网格。您需要做的就是将代码复制并粘贴到每个部分中。以后您可以自定义它以满足您的需求。

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

更新Gradle依赖项

确保以下依赖项位于您的应用程序gradle.build文件中:

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

您可以更新的版本号无论是最新的

创建活动布局

将添加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>

创建网格单元布局

RecyclerView网格中的每个单元只会有一个TextView。创建一个新的布局资源文件。

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>

创建适配器

RecyclerView需要一个适配器来填充每个单元中的意见与您的数据。创建一个新的Java文件。

MyRecyclerViewAdapter.java

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private String[] mData;
    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
    @NonNull 
    public ViewHolder onCreateViewHolder(@NonNull 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(@NonNull ViewHolder holder, int position) {
        holder.myTextView.setText(mData[position]);
    }

    // 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 = 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);
    }
}

笔记

  • 尽管不是绝对必要的,但我包括了用于侦听单元格上的单击事件的功能。这在过去是可用的GridView,是常见的需求。如果不需要,可以删除此代码。

在活动中初始化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 = 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

已完成

而已。您应该现在就可以运行您的项目,并获得类似于顶部的图像。

继续

圆角

自动装配柱

进一步研究


2
@MarianPaździoch,是的,我只是举了一个最小的例子。它肯定可以使用一些美化工作。我将在以后的某个时间尝试更新此答案。
Suragch

1
我登录只是为了指出,像您这样的人一直在使该门户网站保持活力并不断前进。非常感谢
VarunJoshi129

1
@androiddeveloper,网格项从左到右,从上到下进行布局。当屏幕上无法容纳更多项目时,垂直滚动。
Suragch

13
未来的读者,让我为您节省一些时间,关键是recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
daka,2018年

1
@daka,好点。我编辑了答案,以便一开始就包含此内容。
Suragch

7

尽管我喜欢并欣赏Suragch的回答,但我想留下一个注释,因为我发现编码AdapterMyRecyclerViewAdapter)来定义和公开Listener方法onItemClick并不是最好的方法,因为没有使用类封装正确地。因此,我的建议是让适配器仅处理侦听操作(这是他的目的!),并将其与使用适配器的活动分开(MainActivity)。所以这就是我设置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
    @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);
    }
}

请注意,onItemClick现在定义的方法MyRecyclerViewAdapter是您要为收到的事件/动作编写任务代码的地方。

为了完成此转换,只需要进行很小的更改: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);
    }
}

3
如果该活动确实需要监听点击事件该怎么办?例如,将数据传递给演示者,根据单击的项目进行一些逻辑,跟踪等
。–艾哈迈德·法德利

我同意适配器应该处理单击事件,因为适配器中包含带有数据的项目。@AhmadFadli如果您需要在适配器的主机(片段或活动)中做一些工作,则应使用所需的方法创建一个回调接口。您的主机实现此接口。然后,将主机实例传递给Adapter的构造函数。具有主机实例后,可以在需要时从适配器调用它的方法。您的主机会收到回调。当您需要使用ActionMode时,通常会使用它。当您长单击以选择项目并使用ActionBar按钮时。
Kirill Karmazin

我不同意并认为应在托管中处理单击事件Activity。因为只有它的点击听众可知道有关Activity意见和其他FragmentsActivities等等适配器只能发送点击事件上的水平。它应该具有ItemClickListener 包含许多事件的接口,适配器可以产生许多事件。该解决方案的编写时间更早:stackoverflow.com/a/40563598/2914140
CoolMind

4

您必须将您的recyclerview layoutmanager设置为Gridlayout模式,为此只需在要设置RecyclerView LayoutManager时更改代码:

注意:将所需的列数替换为### HELP ###

   recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),###HELP###));
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.