Android万能适配器BaseRecyclerViewAdapterHelper的简单使用

继我们上一章的网络请求的封装中,我们在现有的项目中通过我们的万能适配器来写一个简单的Recyclerview列表

效果图

video1

首先还是同样的道理,我们需要进行一个简单的依赖添加

   //recyclerview列表和万能适配器
    implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha05'
//    implementation 'com.alibaba:fastjson:1.2.55'
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.50'

然后在我们的Activity的布局文件中写入recyclerview列表

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

然后通过我们的id来写找到控件并且创建好适配器

 binding.recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        messageReminderAdapter=new MessageReminderAdapter(R.layout.item_message_reminder, dataBeanList);
        binding.recyclerView.setAdapter(messageReminderAdapter);

然后需要我们创建一个适配器的布局文件item_message_reminder

<?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="wrap_content">

    <ImageView
        android:id="@+id/iv_recycler"
        android:layout_width="180dp"
        android:layout_height="180dp" />

    <TextView
        android:id="@+id/tv_recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp" />
</LinearLayout>

然后创建适配器MessageReminderAdapter来继承我们的万能适配器

package com.ghn.networkdemo.adapter;

import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ghn.networkdemo.R;
import com.ghn.networkdemo.bean.BannerBean;

import java.util.List;

import io.reactivex.annotations.Nullable;

/**
 * @author 浩楠
 * @date 2022/8/5
 * <p>
 * _              _           _     _   ____  _             _ _
 * / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * 万能适配器
 */
public class MessageReminderAdapter extends BaseQuickAdapter<BannerBean, BaseViewHolder> {
    /***
     *
     * @param layoutResId 行布局的资源ID
     * @param data 数据源
     */
    public MessageReminderAdapter(int layoutResId, @Nullable List<BannerBean> data) {
        super(layoutResId, data);
    }

    /***
     *
     * @param helper holder
     * @param item 每一行的数据
     */
    @Override
    protected void convert(BaseViewHolder helper, BannerBean item) {
        helper.setText(R.id.tv_recycler, item.getTitle());//添加标题
        Glide.with(mContext).load(item.getImagePath()).into((ImageView) helper.getView(R.id.iv_recycler));//加载图片
        //添加Item子控件的点击事件
        helper.addOnClickListener(R.id.tv_recycler);
    }

}

再回到我们的Activity中来看看我们的全部代码

package com.ghn.networkdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ghn.networkdemo.adapter.MessageReminderAdapter;
import com.ghn.networkdemo.databinding.ActivityMainBinding;
import com.ghn.networkdemo.network.Api.Api;
import com.ghn.networkdemo.network.utils.ApiException;
import com.ghn.networkdemo.network.utils.ErrorConsumer;
import com.ghn.networkdemo.network.ResponseTransformer;
import com.ghn.networkdemo.bean.BannerBean;
import com.ghn.networkdemo.utils.Log;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;
    @Inject
    Api api;
    private List dataBeanList;
    private MessageReminderAdapter messageReminderAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initData();
        dataBeanList = new ArrayList<BannerBean>();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        messageReminderAdapter=new MessageReminderAdapter(R.layout.item_message_reminder, dataBeanList);
        binding.recyclerView.setAdapter(messageReminderAdapter);
        /**
         * 渐显 ALPHAIN
         * 缩放 SCALEIN
         * 从下到上 SLIDEIN_BOTTOM
         * 从左到右 SLIDEIN_LEFT
         * 从右到左 SLIDEIN_RIGHT
         */
        messageReminderAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        //设置Item点击事件
        messageReminderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "onItemClick"+position, Toast.LENGTH_SHORT).show();
            }
        });
        //设置Item长按事件
        messageReminderAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "onItemLongClick"+position, Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        //设置Item中子控件点击事件
        messageReminderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                //判断子控件
                if (view.getId() == R.id.tv_recycler) {
                    Toast.makeText(MainActivity.this, "onItemChildClick"+position, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initData() {
        api.BannerRetrofit()
                .compose(ResponseTransformer.obtain())
                .subscribe(homeBanners -> {
                    //请求成功
                    if (homeBanners != null) {
                        dataBeanList.clear();
                        dataBeanList.addAll(homeBanners);
                        messageReminderAdapter.notifyDataSetChanged();
                    }
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        //请求失败
                        Log.e(e.getDisplayMessage() + e.getCode());
                    }
                });


    }

}

源码地址

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值