BaseQuickAdapter——简化RecyclerView的使用


不积跬步,无以至千里;不积小流,无以成江海。要沉下心来,诗和远方的路费真的很贵!

BaseQuickAdapter——简化RecyclerView的使用

例子:用RecyclerView实现招聘信息列表

1.引入依赖

  • 在build.gradle的allprojects中加入镜像
//jcenter镜像
        maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter' }
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url "https://jitpack.io" }
  • 引入依赖
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'//BaseQuickAdapter

2.根据item布局建立item数据类

  • 布局
    在这里插入图片描述
  • item布局
    • item_looked_list
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
    android:background="@color/white">

    <TextView
        android:id="@+id/tv_work_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:layout_marginTop="12dp"
        tools:text="测试实习生"
        android:textColor="@color/black"
        android:textSize="18dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvSalary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="6dp"
        android:layout_marginRight="6dp"
        tools:text="230-240元/天"
        android:textColor="@color/salary_color"
        android:textSize="15dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv_work_name"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/tv_company_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        tools:text="杭州快定网络科技公司"
        android:textColor="@color/tv_gray"
        android:textSize="15dp"
        app:layout_constraintStart_toStartOf="@+id/tv_work_name"
        app:layout_constraintTop_toBottomOf="@+id/tv_work_name" />

    <TextView
        android:id="@+id/tv_company_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="已上市"
        android:layout_marginLeft="5dp"
        android:textColor="@color/tv_gray"
        android:textSize="15dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv_company_name"
        app:layout_constraintStart_toEndOf="@+id/tv_company_name"
        app:layout_constraintTop_toTopOf="@+id/tv_company_name" />

    <TextView
        android:id="@+id/tvLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tv_background"
        tools:text="杭州 余杭区 未来科技城"
        android:textSize="13dp"
        android:textColor="@color/tv_gray"
        android:gravity="center"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="@+id/tv_company_name"
        app:layout_constraintTop_toBottomOf="@+id/tv_company_name" />

    <TextView
        android:id="@+id/tv_work_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tv_background"
        android:gravity="center"
        tools:text="5天/周"
        android:textColor="@color/tv_gray"
        android:textSize="13dp"
        android:layout_marginLeft="4dp"
        app:layout_constraintBottom_toBottomOf="@+id/tvLocation"
        app:layout_constraintStart_toEndOf="@+id/tvLocation"
        app:layout_constraintTop_toTopOf="@+id/tvLocation" />

    <TextView
        android:id="@+id/tv_work_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tv_background"
        android:gravity="center"
        tools:text="2个月"
        android:textColor="@color/tv_gray"
        android:textSize="13dp"
        android:layout_marginLeft="4dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv_work_day"
        app:layout_constraintStart_toEndOf="@+id/tv_work_day"
        app:layout_constraintTop_toTopOf="@+id/tv_work_day" />

    <TextView
        android:id="@+id/tvEducation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tv_background"
        android:gravity="center"
        tools:text="本科"
        android:textColor="@color/tv_gray"
        android:textSize="13dp"
        android:layout_marginLeft="4dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv_work_time"
        app:layout_constraintStart_toEndOf="@+id/tv_work_time"
        app:layout_constraintTop_toTopOf="@+id/tv_work_time" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/ivAvatar"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginTop="10dp"
        tools:background="@mipmap/home_selected"
        app:layout_constraintStart_toStartOf="@+id/tvLocation"
        app:layout_constraintTop_toBottomOf="@+id/tvLocation" />

    <TextView
        android:id="@+id/tv_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        tools:text="张琪"
        android:textColor="@color/tv_gray"
        android:textSize="12dp"
        android:layout_marginLeft="4dp"
        app:layout_constraintBottom_toBottomOf="@+id/ivAvatar"
        app:layout_constraintStart_toEndOf="@+id/ivAvatar"
        app:layout_constraintTop_toTopOf="@+id/ivAvatar" />

    <TextView
        android:id="@+id/tv_job"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:gravity="center"
        android:textColor="@color/tv_gray"
        android:textSize="12dp"
        app:layout_constraintBottom_toBottomOf="@+id/tv_username"
        app:layout_constraintStart_toEndOf="@+id/tv_username"
        app:layout_constraintTop_toTopOf="@+id/tv_username"
        app:layout_constraintVertical_bias="1.0"
        tools:text="开发" />

    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginTop="4dp"
        android:background="@color/bg_gray"
        app:layout_constraintTop_toBottomOf="@+id/ivAvatar" />


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:text="查看了您"
        android:textSize="13dp"
        app:layout_constraintStart_toStartOf="@+id/ivAvatar"
        app:layout_constraintTop_toBottomOf="@+id/view2" />

    <View
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:layout_marginTop="4dp"
        android:background="@color/bg_gray"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • 根据布局建立数据类

    • Works
    package com.example.app_smallsystem.bean;
    
    /**
     * @author created by lvjunkai
     * @date 2021/7/29
     * @description: 招聘信息类
     */
    public class Works {
        String workName;
        String workSalary;
        String companyName;
        String companyState;
        String workLocation;
        String workDay;
        String workTime;
        String education;
        String avatar;
        String username;
        String job;
    
        public Works() {
        }
    
        public String getWorkName() {
            return workName;
        }
    
        public void setWorkName(String workName) {
            this.workName = workName;
        }
    
        public String getWorkSalary() {
            return workSalary;
        }
    
        public void setWorkSalary(String workSalary) {
            this.workSalary = workSalary;
        }
    
        public String getCompanyName() {
            return companyName;
        }
    
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
    
        public String getCompanyState() {
            return companyState;
        }
    
        public void setCompanyState(String companyState) {
            this.companyState = companyState;
        }
    
        public String getWorkLocation() {
            return workLocation;
        }
    
        public void setWorkLocation(String workLocation) {
            this.workLocation = workLocation;
        }
    
        public String getWorkDay() {
            return workDay;
        }
    
        public void setWorkDay(String workDay) {
            this.workDay = workDay;
        }
    
        public String getWorkTime() {
            return workTime;
        }
    
        public void setWorkTime(String workTime) {
            this.workTime = workTime;
        }
    
        public String getEducation() {
            return education;
        }
    
        public void setEducation(String education) {
            this.education = education;
        }
    
        public String getAvatar() {
            return avatar;
        }
    
        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getJob() {
            return job;
        }
    
        public void setJob(String job) {
            this.job = job;
        }
    }
    

3. 建立适配器

  • LookedAdapter
package com.example.app_smallsystem.adapter;

import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.app_smallsystem.R;
import com.example.app_smallsystem.bean.Works;
import com.example.app_smallsystem.fragment.LookedVpFragment;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author created by lvjunkai
 * @date 2021/7/29
 * @description: 谁看过我的适配器
 */

//继承BaseQuickAdapter,里面有数据类和ViewHolder
public class LookedAdapter extends BaseQuickAdapter<Works, BaseViewHolder> {

    //构造函数,用于适配器初始化
    public LookedAdapter(int layoutResId, @Nullable List<Works> data) {
        super(layoutResId, data);
    }

    @Override
    //重写convert方法
    //进行控件操作
    protected void convert(@NotNull BaseViewHolder holder, Works item) {
        Glide.with(getContext())
                .load(item.getAvatar())
                .into((ImageView) holder.getView(R.id.ivAvatar));
        holder.setText(R.id.tv_work_name, item.getWorkName())
                .setText(R.id.tvSalary, item.getWorkSalary())
                .setText(R.id.tv_company_name, item.getCompanyName())
                .setText(R.id.tv_company_state, item.getCompanyState())
                .setText(R.id.tvLocation, item.getWorkLocation())
                .setText(R.id.tv_work_day, item.getWorkDay())
                .setText(R.id.tv_work_time, item.getWorkTime())
                .setText(R.id.tvEducation, item.getEducation())
                .setText(R.id.tv_username, item.getUsername())
                .setText(R.id.tv_job, item.getJob());
    }
}

4.使用适配器

  • LookedVpFragment
package com.example.app_smallsystem.fragment;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.app_smallsystem.R;
import com.example.app_smallsystem.adapter.LookedAdapter;
import com.example.app_smallsystem.bean.Works;
import java.util.ArrayList;
import java.util.List;

public class LookedVpFragment extends Fragment {
    RecyclerView rcvLookedList;
    List<Works> lookedList = new ArrayList<>();
    LookedAdapter lookedAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //主布局
        View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_looked_vp,container,false);
        //初始化
        rcvLookedList = root.findViewById(R.id.rcvLookedList);
        //设置适配器
        //new的时候,把它的子布局和数据传入
        lookedAdapter = new LookedAdapter(R.layout.item_looked_list,lookedList);
        rcvLookedList.setAdapter(lookedAdapter);
        //设置recyclerView的布局
        LinearLayoutManager lm = new LinearLayoutManager(getActivity());
        rcvLookedList.setLayoutManager(lm);

        //构造测试数据
        for(int i=0;i<20;i++){
            Works works = new Works();
            works.setAvatar("https://img-blog.csdnimg.cn/2020061412003655.JPG");
            works.setWorkName("工作名" + i);
            works.setCompanyName("公司名" + i);
            works.setWorkSalary("薪水" + i);
            works.setCompanyState("公司状态" + i);
            works.setEducation("学历" + i);
            works.setJob("职位" + i);
            works.setUsername("账号使用者" + i);
            works.setWorkTime("工作时长" + i);
            works.setWorkDay("工作日" + i);
            works.setWorkLocation("工作地点" + i);
            lookedList.add(works);
        }

        return root;
    }
}

总结

使用BaseQuickAdapter后,不需要我们自己再写ViewHolder类去缓存控件了,也不需要重写Adapter的三个方法了。大大便利了我们对RecyclerView的使用。

使用步骤

  1. 建立bean类,实现item的数据结构对象。
  2. 实现适配器类,通过构造方法初始化时确定子布局数据
  3. 重写convert方法,所有对控件的操作,都在这个方法中实现。
  4. 在Activity或者Fragment中使用Adapter类。
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过继承`BaseQuickAdapter`创建RecyclerView的适配器,并为item添加点击事件,你可以按照以下步骤进行操作: 1. 创建一个继承自`BaseQuickAdapter`的自定义适配器类,并指定数据类型和ViewHolder类型。例如: ```java public class CustomAdapter extends BaseQuickAdapter<String, CustomAdapter.CustomViewHolder> { public CustomAdapter() { super(R.layout.item_custom); } @Override protected void convert(CustomViewHolder holder, String item) { // 在这里实现数据绑定 holder.textViewTitle.setText(item); } // 自定义ViewHolder类 public static class CustomViewHolder extends BaseViewHolder { TextView textViewTitle; public CustomViewHolder(View itemView) { super(itemView); textViewTitle = itemView.findViewById(R.id.textViewTitle); } } } ``` 在上面的示例中,`CustomAdapter`继承自`BaseQuickAdapter`,并指定数据类型为`String`,ViewHolder类型为`CustomViewHolder`。在`convert()`方法中实现数据绑定逻辑,通过`holder`对象获取item布局中的视图元素,并设置数据。 2. 创建自定义的item布局文件。例如,创建一个名为`item_custom.xml`的布局文件,定义自定义item的外观和样式。示例代码如下: ```xml <!-- item_custom.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <TextView android:id="@+id/textViewTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:textAppearanceMedium"/> <!-- 添加其他需要的视图 --> </LinearLayout> ``` 在这个示例中,我们使用了一个`TextView`来显示标题。 3. 在你的Activity或Fragment中,创建一个`CustomAdapter`对象,并为RecyclerView设置适配器。例如: ```java CustomAdapter adapter = new CustomAdapter(); recyclerView.setAdapter(adapter); ``` 4. 为RecyclerView的item添加点击事件。在`convert()`方法中,为`holder.itemView`设置点击事件监听器。例如: ```java @Override protected void convert(CustomViewHolder holder, String item) { holder.textViewTitle.setText(item); // 为item添加点击事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 处理点击事件 // 在这里你可以执行特定的操作,比如打开新的Activity或显示Toast信息等 } }); } ``` 在上面的示例中,我们为`holder.itemView`设置了一个匿名内部类的点击事件监听器。 通过继承`BaseQuickAdapter`,你可以更方便地创建RecyclerView的适配器,并实现自定义的item布局和点击事件。 希望这个示例能帮助到你!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值