Android RecyclerView使用及出现问题汇总(附代码)

系列文章目录

基本用法

遇到问题汇总

附上完整代码

文章目录

  • 系列文章目录
  • 前言
  • 基本使用
    • 1.先在使用该控件xml文件中引入RecyclerView:
    • 2.再编写Adapter适配器代码
    • 3.再创建一个Item的xml文件
    • 4.在界面中调用这个适配器
  • 二、遇到问题汇总
    • 1.checkbox多选问题
    • 2.多选不能及时刷新
    • 3.重影问题
    • 4.处理数据问题
  • 三、附上完整代码
    • 1.fragment文件
    • 2.Adapter 适配器文件
    • 3.fragmnet的xml文件
    • 4.item的xml文件
  • 总结

前言

RecyclerVie是安卓控件里相对复杂难用的,同时也是用的比较多的。今天就介绍一下基本用法,及我在使用过程中出现问题,后边附上的有代码。

一、基本用法

1.先在使用该控件xml文件中引入RecyclerView:

代码如下(示例):

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

2.再编写Adapter适配器代码

代码如下(示例):

这三个函数是强制必须重写的

  • onCreateViewHolder 用于得到我们自定义的ViewHolder,在ListView中,我们也会定义ViewHolder来承载视图中的元素.
  • onBindViewHolder 指定位置的数据和视图绑定起来
  • getItemCount 列表总共有多少条
public class FragmentRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
 
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(mContext).inflate(R.layout.fragment_adapter_item_layout, parent, false);
    return new NormalHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    NormalHolder normalHolder = (NormalHolder) holder;
    normalHolder.textView.setText(mDatas.get(position));
    normalHolder.checkBox.setTag(position);
    normalHolder.checkBox.setChecked(mCheckStates.get(position, false));
}

@Override
public int getItemCount() {
    return mDatas.size();
}

}

ListView的数据都是从外部传进来的,我们需要给Adapter添加上一个构造函数,将数据从外部传进来:

public FragmentRecyclerAdapter(Context context, ArrayList<String> datas) {
    mContext = context;
    mDatas = datas;
}

3.再创建一个Item的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/item_layout"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/check_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/checkbox_shape"
        android:button="@null" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="发动机数据"></TextView>

</LinearLayout>

4.在界面中调用这个适配器

@Nullable
public View onCreateView(LayoutInflater inflater,
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值