Base+RecyclerView+Adapter+Helper
一.简介
BRVAH是一个强大的RecyclerAdapter框架(什么是RecyclerView?),它能节约开发者大量的开发时间,集成了大部分列表常用需求解决方案。
二.使用
官方使用指南
在使用时,首先要项目的build.gradle导入
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
然后再在module的build.gradle导入
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'//万能适配器
然后就可以使用
适配器的创建及使用,需要继承BaseQuickAdapter
其中的泛型为实体类和BaseViewHolder
/***
* 重写方法 和构造
* 泛型:实体类 BaseViewHolder
*/
public class MyAdapter extends BaseQuickAdapter<DishEntity.DataBean, BaseViewHolder> {
/***
*
* @param layoutResId 布局id
* @param data 数据源
*/
public MyAdapter(int layoutResId, @Nullable List<DishEntity.DataBean> data) {
super(layoutResId, data);
}
/***
*
* @param helper item布局
* @param item 实体对象
*/
@Override
protected void convert(BaseViewHolder helper, DishEntity.DataBean item) {
helper.setText(R.id.te_show, item.getTitle());
Glide.with(mContext).load(item.getPic()).into((ImageView) helper.getView(R.id.img_show));
helper.addOnClickListener(R.id.te_show)
.addOnClickListener(R.id.img_show);
}
}
然后时Activity的代码
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
private List<DishEntity.DataBean> datas = new ArrayList<>();
private String url = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
private MyAdapter myAdapter;
private Banner banner;
private String banner_url = "http://musicapi.leanapp.cn/banner";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
}
private void initDatas() {
OkGo.<String>get(url)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
DishEntity entity = new Gson().fromJson(response.body(), DishEntity.class);
datas.addAll(entity.getData());
myAdapter.notifyDataSetChanged();
}
});
}
private void initViews() {
rv = (RecyclerView) findViewById(R.id.rv);
//创建适配器
myAdapter = new MyAdapter(R.layout.item, datas);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(myAdapter);
//点击事件
myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
}
});
myAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
switch (view.getId()) {
case R.id.te_show:
Toast.makeText(MainActivity.this, "文字" + position, Toast.LENGTH_SHORT).show();
break;
case R.id.img_show:
Toast.makeText(MainActivity.this, "图片" + position, Toast.LENGTH_SHORT).show();
break;
}
}
});
//动画效果
//xiaAdapter.openLoadAnimation();//默认是渐变
//渐显ALPHAIN 、缩放SCALEIN、从下到上SLIDEIN_BOTTOM,从左到右SLIDEIN_LEFT、从右到左SLIDEIN_RIGHT
myAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
//添加头布局,尾布局
View head = LayoutInflater.from(this).inflate(R.layout.head, null);
View foot = LayoutInflater.from(this).inflate(R.layout.foot, null);
banner = (Banner) head.findViewById(R.id.banner);
initBannerDatas();
myAdapter.addHeaderView(head);
myAdapter.addFooterView(foot);
}
private void initBannerDatas() {
OkGo.<String>get(banner_url)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
ImageEntity imageEntity = new Gson().fromJson(response.body(), ImageEntity.class);
List<ImageEntity.BannersBean> banners = imageEntity.getBanners();
//设置图片
banner.setImages(banners);
//设置图片加载器
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
ImageEntity.BannersBean bean = (ImageEntity.BannersBean) path;
//加载图片
Glide.with(context).load(bean.getPicUrl()).into(imageView);
}
});
//启动轮播图
banner.start();
}
});
}
}
主布局
<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"
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>
头布局
<?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="150dp"
android:orientation="vertical">
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="150dp">
</com.youth.banner.Banner>
</LinearLayout>
尾布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:textSize="40sp"
android:gravity="center"
android:text="联系123456"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
效果展示
三.小案例
1.短视频展示
利用BRVAH作类似于快手的短视频展示
1)主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:background="#11121A"
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.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="60dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="30sp"
android:textColor="#fff"
android:text="石家庄"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<View
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="1dp">
</View>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smart"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
2)item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:padding="2dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<ImageView
android:src="@drawable/ic_launcher_background"
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="300dp">
</ImageView>
<ImageView
android:src="@drawable/ic_launcher_background"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:id="@+id/iv_headImage"
android:layout_width="40dp"
android:layout_height="40dp">
</ImageView>
</RelativeLayout>
<TextView
android:textColor="#fff"
android:textSize="16sp"
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
3)头布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:gravity="center"
android:layout_marginLeft="20dp"
android:textColor="#fff"
android:text="自动定位:石家庄"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="40dp"></TextView>
<TextView
android:gravity="center"
android:layout_marginRight="20dp"
android:layout_alignParentRight="true"
android:textColor="#fff"
android:text="切换 >"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="40dp"></TextView>
</RelativeLayout>
4)适配器
public class MyAdapter extends BaseQuickAdapter<VideoEntity.ListBean, BaseViewHolder> {
public MyAdapter(int layoutResId, @Nullable List<VideoEntity.ListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, VideoEntity.ListBean item) {
helper.setText(R.id.tv, item.getText() + "");//设置文本
//封面图片
View view = helper.getView(R.id.iv);
Glide.with(mContext)
.load(item.getImage0())
//CenterCrop 居中裁剪 RoundedCorners 圆角
.transform(new CenterCrop(), new RoundedCorners(5))
.into((ImageView) view);
//头像
View headImage = helper.getView(R.id.iv_headImage);
Glide.with(mContext)
.load(item.getProfile_image())
.transform(new CircleCrop())//圆形
.into((ImageView) headImage);
}
}
5)MainActivity
public class MainActivity extends AppCompatActivity {
private SmartRefreshLayout smart;
private RecyclerView rv;
private int page = 1;
private String url = "http://api.budejie.com/api/api_open.php?a=list&c=data&type=41&page=";
private MyAdapter videoAdapter;
private ArrayList<VideoEntity.ListBean> datas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initViews();
initDatas();
}
private void initDatas() {
OkGo.<String>get(url + page)
.headers("User-Agent", "PostmanRuntime/7.24.0")//添加请求头
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String body = response.body();
List<VideoEntity.ListBean> list = new Gson().fromJson(body, VideoEntity.class).getList();
datas.addAll(list);
//刷新适配器
videoAdapter.notifyDataSetChanged();
//完成加载和刷新
smart.finishLoadMore();
smart.finishRefresh();
}
});
}
private void initViews() {
smart = (SmartRefreshLayout) findViewById(R.id.smart);
rv = (RecyclerView) findViewById(R.id.rv);
videoAdapter = new MyAdapter(R.layout.item, datas);
rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));//设置展示模式
rv.setAdapter(videoAdapter);//设置适配器
videoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Intent intent = new Intent(MainActivity.this, VideoActivity.class);
intent.putExtra("url",datas.get(position).getVideouri());
startActivity(intent);
}
});
View view = LayoutInflater.from(this).inflate(R.layout.head, null);
videoAdapter.addHeaderView(view);//添加头布局
smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
//加载更多
page++;
initDatas();
}
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//刷新
page = 1;
//清空数据
datas.clear();
//重新加载数据
initDatas();
}
});
}
}
6)播放视频的Activity
public class VideoActivity extends AppCompatActivity {
private VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_video);
initViews();
}
private void initViews() {
vv = (VideoView) findViewById(R.id.vv);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
vv.setVideoPath(url);
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
vv.start();
}
});
}
}
7)效果展示
2.购物车
利用BRVAH做类似购物车
1)主布局
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:background="#D82466"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:layout_marginLeft="20dp"
android:textSize="30sp"
android:textColor="#fff"
android:text="购物车"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:layout_weight="8"
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="0dp">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<CheckBox
android:id="@+id/cb_all"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"></CheckBox>
<LinearLayout
android:orientation="vertical"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="match_parent">
<LinearLayout
android:gravity="right"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:textSize="20sp"
android:text="合计"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:text="¥0.0"
android:id="@+id/tv_count"
android:textSize="20sp"
android:textColor="#E15547"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView
android:gravity="right"
android:textAlignment="center"
android:text="满10元免运费"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"></TextView>
</LinearLayout>
<Button
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"
android:background="@drawable/shape_bt"
android:textSize="20sp"
android:textColor="#fff"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="结算"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent">
</Button>
</LinearLayout>
</LinearLayout>
2)item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#F0E8F0"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="150dp">
<CheckBox
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:id="@+id/cb"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"></CheckBox>
<ImageView
android:background="@drawable/shape"
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:id="@+id/iv"
android:src="@drawable/ic_launcher_background"
android:layout_width="90dp"
android:layout_height="90dp">
</ImageView>
<LinearLayout
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_weight="5"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_title"
android:text="jsdhfdjkgh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="30dp">
<ImageView
android:background="@drawable/shape"
android:src="@drawable/jian"
android:id="@+id/iv_jian"
android:layout_width="wrap_content"
android:layout_height="match_parent"></ImageView>
<TextView
android:textAlignment="center"
android:background="@drawable/shape"
android:text="0"
android:id="@+id/tv_num"
android:layout_width="30dp"
android:layout_height="match_parent"></TextView>
<ImageView
android:background="@drawable/shape"
android:src="@drawable/add"
android:id="@+id/iv_add"
android:layout_width="wrap_content"
android:layout_height="match_parent"></ImageView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:text="¥0"
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<ImageView
android:src="@drawable/delete"
android:id="@+id/iv_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
</LinearLayout>
</LinearLayout>
3)适配器
package com.example.shoppingcar.adapter;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.shoppingcar.R;
import com.example.shoppingcar.entity.DishEntity;
import java.util.List;
public class MyAdapter extends BaseQuickAdapter<DishEntity.DataBean, BaseViewHolder> {
public MyAdapter(int layoutResId, @Nullable List<DishEntity.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final DishEntity.DataBean item) {
helper.setText(R.id.tv_title, item.getTitle()+"");//设置标题
helper.setText(R.id.tv_price, item.getNum() + "");//设置单价
helper.setText(R.id.tv_num, item.getCheckNum() + "");//设置选中的数量
helper.setChecked(R.id.cb,item.isChecked());//设置是否被选中
View view = helper.getView(R.id.iv);//图片
Glide.with(mContext).load(item.getPic())
.transform(new CenterCrop())
.into((ImageView) view);
//设置子控件点击
helper.addOnClickListener(R.id.iv_jian)//减号
.addOnClickListener(R.id.iv_add)//加号
.addOnClickListener(R.id.iv_delete)//删除
.addOnClickListener(R.id.cb);//复选框
}
}
4)MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView rv;
private CheckBox cbAll;
private TextView tvCount;
private String url = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
private List<DishEntity.DataBean> datas = new ArrayList<>();
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initViews();
initDatas();
}
private void initDatas() {
OkGo.<String>get(url)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
DishEntity dishEntity = new Gson().fromJson(response.body(), DishEntity.class);
//添加数据
datas.addAll(dishEntity.getData());
//刷新适配器
myAdapter.notifyDataSetChanged();
}
});
}
private void initViews() {
rv = (RecyclerView) findViewById(R.id.rv);
cbAll = (CheckBox) findViewById(R.id.cb_all);
tvCount = (TextView) findViewById(R.id.tv_count);
//创建适配器
myAdapter = new MyAdapter(R.layout.item, datas);
rv.setLayoutManager(new LinearLayoutManager(this));//设置展示模式
//设置适配器
rv.setAdapter(myAdapter);
setItemClick();
//全选和取消全选
cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int i = 0; i < datas.size(); i++) {
datas.get(i).setChecked(b);
}
//刷新适配器,设置item的是否选中
myAdapter.notifyDataSetChanged();
//重新计算总价
count();
}
});
}
public void count() {
float countPrice = 0;
for (int i = 0; i < datas.size(); i++) {
DishEntity.DataBean dataBean = datas.get(i);
if (dataBean.isChecked()) {
countPrice += dataBean.getCheckNum() * dataBean.getNum();
}
}
tvCount.setText("¥" + countPrice);
}
private void setItemClick() {
myAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
switch (view.getId()) {
case R.id.iv_add:
//点击加号时 将对应的 checkNum+1
int checkNum = datas.get(position).getCheckNum();
checkNum++;
datas.get(position).setCheckNum(checkNum);
myAdapter.notifyDataSetChanged();//刷新适配器
count();//重新计算总价
break;
case R.id.iv_jian:
int checkNum1 = datas.get(position).getCheckNum();
checkNum1--;
datas.get(position).setCheckNum(checkNum1);
myAdapter.notifyDataSetChanged();//刷新适配器
count();//重新计算总价
break;
case R.id.iv_delete:
//直接删除
datas.remove(position);
myAdapter.notifyDataSetChanged();
count();
break;
case R.id.cb:
CheckBox checkBox = (CheckBox) view;
//设置是否选中
datas.get(position).setChecked(checkBox.isChecked());
myAdapter.notifyDataSetChanged();
count();
break;
}
}
});
}
}
5)效果展示
要开心加油