RecyclerView中GridLayoutManager网格的应用

1.创建一个item布局:find_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white">
    <TextView
        android:id="@+id/tv_item"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="50dp"/>
LinearLayout>

2.创建一个Entity实体:DiseaseBankEntity

package com.alcode.ykapp.entity;

public class DiseaseBankEntity {
    private String id;
    private String title;
//使用快捷键alt+insert获取get set方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

3.新建activity_disease_bank.xml作为主Activity的布局文件,并使用android.support.v7.widget.RecyclerView组件,并设计页面。


<LinearLayout 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"
    android:background="#f5f5f5"
    android:orientation="vertical"
    tools:context=".view.DiseaseBankActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/rounded_bank"
        android:drawableLeft="@mipmap/search"
        android:hint="搜索" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_bank"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:background="@color/white" />

LinearLayout>

4.添加依赖
在在模块build.gradle里面添加 compile ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.40’,RecyclerView万能适配器**

5.新建DiseaseBankActivity作为程序的主Activity

public class DiseaseBankActivity extends AppCompatActivity {
//创建一创建一个数组模拟数据源
    private List list = new ArrayList<>();
    //获取组件
    @BindView(R.id.rv_bank)
    RecyclerView rvBank;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_disease_bank);
        ButterKnife.bind(this);

        initData();

        //设置布局的方式
        GridLayoutManager layoutManager = new GridLayoutManager(this,4);
        rvBank.setLayoutManager(layoutManager);
        //直接new一个匿名类,就不用单独创建Adapter类文件了。
        rvBank.setAdapter(new BaseQuickAdapter(R.layout.find_item, list) {
            @Override
            protected void convert(BaseViewHolder helper, DiseaseBankEntity item) {
            //调用赋值
                helper.setText(R.id.tv_item, item.getTitle());
            }
        });
    }

//模拟数据
    private void initData() {
        for (int i = 0; i < 10; i++) {
            DiseaseBankEntity bank = new DiseaseBankEntity();
            bank.setId(String.valueOf(i));
            bank.setTitle("title" + i);
            list.add(bank);
        }
    }

6.效果图
RecyclerView中GridLayoutManager网格的应用_第1张图片

你可能感兴趣的:(RecyclerView中GridLayoutManager网格的应用)