DataBinding在RecycleView中结合BaseRecyclerViewAdapterHelper的使用
- 引入BaseRecyclerViewAdapterHelper
在app的build.gradle中
implementation ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30’
在Project的build.gradle中
allprojects {
repositories {
maven { url “https://jitpack.io” }
jcenter()
}
}
- 创建BaseActivity
package tsou.cn.databinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.List;
public abstract class BaseActivity extends AppCompatActivity {
private static List activities = new LinkedList();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
synchronized (activities) {
activities.add(this);
}
}
protected void finishAllActivity() {
List tempList;
synchronized (activities) {
tempList = new LinkedList(activities);
}
for (BaseActivity a : tempList) {
a.finish();
}
}
@Override
protected void onDestroy() {
activities.remove(this);
try {
System.gc();
} catch (Exception e) {
}
super.onDestroy();
}
}
- 创建BaseRecyclerViewActivity处理布局绑定
package tsou.cn.databinding;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public abstract class BaseRecyclerViewActivity extends BaseActivity {
protected View notDataView;
protected View errorView;
protected M binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, getLayoutId());
setEmptyAndErrorView(getRecyclerView(), R.mipmap.icon_empty, getString(R.string.the_server_did_not_return_data));
initData(savedInstanceState);
initListener();
}
public abstract int getLayoutId();
public abstract RecyclerView getRecyclerView();
protected abstract void initData(Bundle savedInstanceState);
protected void initListener() {
}
protected void setEmptyAndErrorView(RecyclerView mRecyclerView, int emptyIcon, String emptyText) {
notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) mRecyclerView.getParent(), false);
ImageView empty_icon = (ImageView) notDataView.findViewById(R.id.iv_empty_icon);
TextView empty_text = (TextView) notDataView.findViewById(R.id.tv_empty_no_data);
empty_icon.setImageResource(emptyIcon);
empty_text.setText(emptyText);
notDataView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRefresh();
}
});
errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) mRecyclerView.getParent(), false);
errorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRefresh();
}
});
}
protected void onRefresh() {
}
}
- 数据LoadMore
package tsou.cn.databinding.bean;
/**
- Created by Administrator on 2018/5/31 0031.
*/
public class LoadMore {
private String name;
private String imaggUrl;
public LoadMore(String name, String imaggUrl) {
this.name = name;
this.imaggUrl = imaggUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImaggUrl() {
return imaggUrl;
}
public void setImaggUrl(String imaggUrl) {
this.imaggUrl = imaggUrl;
}
}
- item布局item_load_more
<layout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:bind=“http://schemas.android.com/apk/res-auto”>
<variable
name=“loadMore”
type=“tsou.cn.databinding.bean.LoadMore” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“200dp”
android:layout_marginBottom=“5dp”
android:orientation=“vertical”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:layout_margin=“10dp”
android:textSize=“16sp”
android:textColor=“@android:color/black”
android:text=“@{loadMore.name,default=标题}” />
<ImageView
android:layout_width=“match_parent”
android:layout_height=“match_parent”
bind:url=“@{loadMore.imaggUrl}”
android:src=“@mipmap/ic_launcher_round” />
- LoadMoreAdapter
package tsou.cn.databinding.adapter;
import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.List;
import tsou.cn.databinding.R;
import tsou.cn.databinding.bean.LoadMore;
import tsou.cn.databinding.databinding.ItemLoadMoreBinding;
import tsou.cn.databinding.viewholder.MyBaseViewHolder;
/**
- Created by Administrator on 2018/5/31 0031.
*/
public class LoadMoreAdapter extends BaseQuickAdapter<LoadMore, MyBaseViewHolder> {
public LoadMoreAdapter(@Nullable List data) {
super(data);
}
@Override
protected MyBaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
ItemLoadMoreBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext),
R.layout.item_load_more, parent, false);
return new MyBaseViewHolder(binding.getRoot());
}
@Override
protected void convert(MyBaseViewHolder holder, LoadMore item) {
ItemLoadMoreBinding binding = DataBindingUtil.getBinding(holder.itemView);
binding.setLoadMore(item);
binding.executePendingBindings();
}
}
- MyBaseViewHolder
package tsou.cn.databinding.viewholder;
import android.view.View;
import com.chad.library.adapter.base.BaseViewHolder;
public class MyBaseViewHolder extends BaseViewHolder {
public MyBaseViewHolder(View view) {
super(view);
}
}
- Activity调用
package tsou.cn.databinding;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import tsou.cn.databinding.adapter.LoadMoreAdapter;
import tsou.cn.databinding.bean.LoadMore;
import tsou.cn.databinding.databinding.ActivityLoadMoreRecyclerViewBinding;
import tsou.cn.databinding.view.CustomLoadMoreView;
public class LoadMoreRecyclerViewActivity extends BaseRecyclerViewActivity
implements SwipeRefreshLayout.OnRefreshListener,
BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {
private LoadMoreAdapter adapter;
private ArrayList loadMores = new ArrayList<>();
private int mLastIndex;
private MyHandler myHandler;
@Override
public int getLayoutId() {
return R.layout.activity_load_more_recycler_view;
}
@Override
public RecyclerView getRecyclerView() {
return binding.recyclerView;
}
protected void initData(Bundle savedInstanceState) {
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
binding.swipeRefresh.setColorSchemeColors(Color.rgb(63, 81,
181));
adapter = new LoadMoreAdapter(loadMores);
adapter.isFirstOnly(false);
//慢慢淡进
//adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//慢慢放大进入
//adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
//从下滑入
adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
//从左滑入
//adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
//从有滑入
//adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
adapter.setLoadMoreView(new CustomLoadMoreView());
binding.recyclerView.setAdapter(adapter);
adapter.setEnableLoadMore(false);
adapter.setEmptyView(R.layout.loading_view, (ViewGroup) binding.recyclerView.getParent());
binding.swipeRefresh.setEnabled(false);
myHandler = new MyHandler(this);
fetchData(true);
}
@Override
protected void initListener() {
super.initListener();
binding.swipeRefresh.setOnRefreshListener(this);
adapter.setOnLoadMoreListener(this, binding.recyclerView);
adapter.setOnItemClickListener(this);
}
@Override
public void onRefresh() {
if (adapter.getData().isEmpty())
adapter.setEmptyView(R.layout.loading_view, (ViewGroup) binding.recyclerView.getParent());
adapter.setEnableLoadMore(false);
fetchData(true);
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
文末
很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家
这里笔者分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。
【视频教程】
天道酬勤,只要你想,大厂offer并不是遥不可及!希望本篇文章能为你带来帮助,如果有问题,请在评论区留言。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
文末
很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家
这里笔者分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。
[外链图片转存中…(img-NWZhXw7Q-1711810237435)]
[外链图片转存中…(img-2J6PiuWM-1711810237435)]
【视频教程】
[外链图片转存中…(img-6o35JJIJ-1711810237436)]
天道酬勤,只要你想,大厂offer并不是遥不可及!希望本篇文章能为你带来帮助,如果有问题,请在评论区留言。