RecyclerView 使用基础

概念

RecyclerView 类关系

A flexible view for providing a limited window into a large data set.

一个为了大型数据集提供提供有限窗口的灵活视图。

在实现 RecyclerView 过程中,几个主要的类说明如下:

  • LayoutManager:控制 item 的排列方式;
  • RecyclerView.Adapter:创建适配器;
  • RecyclerView.ItemDecoration:item 间的间隔;
  • RecyclerView.ItemAnimator:控制 item 的增删动画;

示例说明

1、在使用该控件之前,我们需要手动将引用包添加到 gradle 中:

implementation 'com.android.support:recyclerview-v7:26.1.0'

注意:在 Android Studio 3.0 之后,grade 文件发送了一些变化,在引用其它包的时候 compile 变成了 implementation。还有一些其他变化,可自行查找。

2、在 XML 文件中使用 RecyclerView 控件进行布局

<android.support.v7.widget.RecyclerView
        android:id="@+id/my_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
<!--item 布局 -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

3、在 Activity 中进行配置

public class RecyclerViewActivity extends AppCompatActivity {
   

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);

        mRecyclerView = findViewById(R.id.my_rv);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
//        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));
        mRecyclerView.setAdapter(new MyRvAdapter(this));
    }
}

适配器代码

public class MyRvAdapter extends RecyclerView.Adapter<MyRvAdapter.MyHolder> {
   

    private LayoutInflater mLayoutInflater;
    private String[] mTitles;

    public MyRvAdap
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值