效果图
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列表
然后通过我们的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
然后创建适配器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
*
* _ _ _ _ ____ _ _ _
* / \ _ __ __| |_ __ ___ (_) __| | / ___|| |_ _ _ __| (_) ___
* / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
* / ___ \| | | | (_| | | | (_) | | (_| | ___) | |_| |_| | (_| | | (_) |
* /_/ \_\_| |_|\__,_|_| \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
* 万能适配器
*/
public class MessageReminderAdapter extends BaseQuickAdapter {
/***
*
* @param layoutResId 行布局的资源ID
* @param data 数据源
*/
public MessageReminderAdapter(int layoutResId, @Nullable List 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();
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());
}
});
}
}
源码地址