导入依赖
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
implementation ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22’//万能适配器
BaseRecyclerViewAdapterHelper实现简单布局
1.在布局文件中引入RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
2.编写条目布局文件
3.编写数据实体类型
package com.example.day08;
public class Bean {
String url;
String context;
public Bean(String url, String context) {
this.url = url;
this.context = context;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
}
4.编写适配器
package com.example.day08;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
public class Myadpter extends BaseQuickAdapter<Bean, BaseViewHolder> {
public Myadpter(int layoutResId, @Nullable List<Bean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, Bean item) {
helper.setText(R.id.texts,item.context)
.addOnClickListener(R.id.images)
.addOnClickListener(R.id.texts);
View view = helper.getView(R.id.images);
Glide.with(mContext).load(item.url).into((ImageView) view);
}
}
四.BaseRecyclerViewAdapterHelper设置事件监听
package com.example.day08;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
Myadpter myadpter;
List<Bean> beans=new ArrayList<>();
Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.rv);
//添加数据
beans.add(new Bean("http://img1.6tj.com/remen/resou/20181019/acd96a0de5edee7b4791aa068de0b21a.gif","hhhhhhhhh"));
beans.add(new Bean("http://img1.6tj.com/remen/resou/20181019/acd96a0de5edee7b4791aa068de0b21a.gif","hhhhhhhhh"));
beans.add(new Bean("http://img1.6tj.com/remen/resou/20181019/acd96a0de5edee7b4791aa068de0b21a.gif","hhhhhhhhh"));
beans.add(new Bean("http://img1.6tj.com/remen/resou/20181019/acd96a0de5edee7b4791aa068de0b21a.gif","hhhhhhhhh"));
myadpter= new Myadpter(R.layout.item, beans);
//设置适配器
rv.setAdapter(myadpter);
rv.setLayoutManager(new LinearLayoutManager(this));
//判断点击了哪一个子类控件
myadpter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
if (view.getId()==R.id.images){
Toast.makeText(MainActivity.this, "你点击了图片", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "你点击了文字", Toast.LENGTH_SHORT).show();
}
}
});
//点击条目
myadpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "你点击了"+position, Toast.LENGTH_SHORT).show();
}
});
//长按条目
myadpter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "你长按了"+position, Toast.LENGTH_SHORT).show();
return true;
}
});
//设置头尾布局
View inflate = LayoutInflater.from(this).inflate(R.layout.head, null);
View viewById = inflate.findViewById(R.id.headimage);
Glide.with(this).load("http://img1.6tj.com/remen/resou/20181019/acd96a0de5edee7b4791aa068de0b21a.gif").into((ImageView) viewById);
View footerinflate = LayoutInflater.from(this).inflate(R.layout.footer, null);
View footer = footerinflate.findViewById(R.id.footerimage);
Glide.with(this).load("http://img1.6tj.com/remen/resou/20181019/acd96a0de5edee7b4791aa068de0b21a.gif").into((ImageView) footer);
myadpter.setFooterView(footerinflate);
myadpter.setHeaderView(inflate);
//加载更多
myadpter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
beans.add(new Bean("http://img1.6tj.com/remen/resou/20181019/acd96a0de5edee7b4791aa068de0b21a.gif","AAAAAAAAAAAAAAAAAAAAAA"));
myadpter.notifyDataSetChanged();
myadpter.loadMoreComplete();
}
},2000);
}
});
}
}
多布局适配器
package com.example.app2;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import java.util.List;
public class Myadpter extends BaseQuickAdapter<Bean, BaseViewHolder> {
public Myadpter(@Nullable List<Bean> data) {
super(data);
setMultiTypeDelegate(new MultiTypeDelegate<Bean>() {
@Override
protected int getItemType(Bean bean) {
if (bean.type==0){
return 0;
}else{
return 1;
}
}
});
getMultiTypeDelegate().registerItemType(0,R.layout.items)
.registerItemType(1,R.layout.itemss);
}
@Override
protected void convert(BaseViewHolder helper, Bean item) {
if (helper.getItemViewType()==0){
helper.setText(R.id.texts,item.context);
View view =helper.getView(R.id.images);
Glide.with(mContext).load(item.url).into((ImageView) view);
}else{
helper.setText(R.id.textss,item.context);
View view =helper.getView(R.id.itemimagess);
Glide.with(mContext).load(item.url).into((ImageView) view);
}
}
}