BaseRecyclerViewAdapterHelper 简介:
减少重复 Adapter 代码
添加 Item 的点击事件,长按事件以及子控件的点击事件
添加头部、尾部,下拉刷新、上拉加载(上拉加载的5种加载更多动画任你选择,后期会添加更多的加载动画)、没有更多数据
可以自定义头部、尾部、加载更多布局
添加 Item滑动动画 (9种动画切换,轻松一行代码)
添加新增、删除 Item动画 (目前支持默认的动画方式)
网格,列表,流式随意切换
添加空布局(列表无数据时,显示更加人性化)
拖拽和侧滑删除
支持多类型布局
类似淘宝列表切换
字母导航
类似探探翻牌
框架引入:
allprojects { repositories { jcenter() maven { url "https://jitpack.io" } google() } } 导入依赖:
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.18'
自己创建adapter类
public class TestAdapter extends BaseQuickAdapter<CouponListBean.Data, BaseViewHolder> implements View.OnClickListener { private List<CouponListBean.Data> couponList; private Context context; public TestAdapter(int layoutResId) { super(layoutResId); } @Override protected void convert(BaseViewHolder helper, CouponListBean.Data item) { helper.setText(R.id.tv_coupon_money, "¥" + item.getCouponSaleMoney() + ""); helper.setText(R.id.tv_coupon_name, "¥" + item.getCouponName()); helper.setText(R.id.tv_site_title, "¥" + item.getSite_title()) .setOnClickListener(R.id.tv_site_title, this::onClick); helper.setText(R.id.tv_page_count, "¥" + item.getCouponCount() + "张"); Log.e("convert", item.getSite_title()); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.tv_site_title: break; } } }
有木有很简单,代码量是不是也少了很多。接下来看Activity的实现,还是先上代码:
public class TestActivity extends BaseActivity { private TabLayout tabLayout; private RecyclerView recyclerView; private TextView textView, tv, tvTranslationContent; private Button btnFanyi; private EditText editText; private int CurrentIndex = 1; private int PageSize = 10; private String Channel_one = "0"; private ChannelDataBean bean; private SwipeRefreshLayout refreshLayout; private TestAdapter couponListAdapter; private List<CouponListBean.Data> dataList = new ArrayList<>(); private boolean slideToBottom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo); initView(); getChannelList(); initListener(); } private void initListener() { tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { if (tab.getPosition() == 0) { CurrentIndex = 1; Channel_one = "0"; } else { CurrentIndex = 1; Channel_one = bean.getData().get(tab.getPosition()).getID(); } refreshLayout.setRefreshing(true); getCouponList(0); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshLayout.setRefreshing(true); CurrentIndex = 1; getCouponList(1); } }); couponListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() { @Override public void onLoadMoreRequested() { CurrentIndex += 1; getCouponList(2); } }); } private void initView() { tabLayout = (TabLayout) findViewById(R.id.tabLayout); recyclerView = (RecyclerView) findViewById(R.id.recycleView); textView = (TextView) findViewById(R.id.tv); btnFanyi = (Button) findViewById(R.id.fanyi); tv = (TextView) findViewById(R.id.request1); editText = (EditText) findViewById(R.id.editText); tvTranslationContent = (TextView) findViewById(R.id.tv_translation_content); refreshLayout = findViewById(R.id.refresh); couponListAdapter = new TestAdapter(R.layout.recycler_item_coupon); recyclerView.setAdapter(couponListAdapter); // recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(DemoActivity.this, WrapContentLinearLayoutManager.VERTICAL, false)); recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(TestActivity.this)); addHeadView(); } /** * 获取分类 */ private void getChannelList() { new HttpBuilder(Constant.MallHandler_url) // .path(getExternalFilesDir(null) + File.separator + "Photoshop_CS6.exe") // .params("key", "转换(map)") .params("op", "GetChannelList") .params("user_Group_ID", "337") .params("parentid", "0") .progress(p -> { }) .success(s -> { //返回path try { bean = new Gson().fromJson(s, ChannelDataBean.class); if (bean.getErrorCode().equals("200")) { ChannelDataBean.DataBean channelDataBean = new ChannelDataBean.DataBean(); channelDataBean.setID("0"); channelDataBean.setChannel_Name("全部"); bean.getData().set(0, channelDataBean); for (int i = 0; i < bean.getData().size(); i++) { tabLayout.addTab(tabLayout.newTab().setText(bean.getData().get(i).getChannel_Name())); } } Log.e("success===>s ", "" + bean.getData().size()); } catch (Exception e) { e.printStackTrace(); } }) .error(t -> { Log.e("error==>", t + ""); }) .post(); } /** * 0 第一次进入 * 1 刷新 * 2 加载更多 */ private void getCouponList(int state) { new HttpBuilder(Constant.MallOrderHandler_url) .params("OP", "GetCouponList") .params("user_Group_ID", "337") .params("Account_ID", "8798") .params("Channel_one", "" + Channel_one) .params("Channel_two", "") .params("CurrentIndex", "" + CurrentIndex) .params("PageSize", "" + PageSize) .params("version", "2.32") .params("lang", "" + (getResources().getConfiguration().locale.getCountry().equals("CN") ? "zh" : "en")) .params("APPType", "android") .params("PlantType", "0") .success(s -> { refreshLayout.setRefreshing(false); CouponListBean couponList = new Gson().fromJson(s, CouponListBean.class); Log.e("onSuccess==>", s); if (couponList.getErrorCode().equals("200")) { if (state == 0) { dataList = couponList.getData(); couponListAdapter.setNewData(dataList); } else if (state == 1) { dataList = couponList.getData(); couponListAdapter.setNewData(dataList); } else if (state == 2) { if (CurrentIndex == 1) { dataList.clear(); } dataList.addAll(couponList.getData()); couponListAdapter.setNewData(dataList); couponListAdapter.loadMoreComplete(); couponListAdapter.notifyDataSetChanged(); Log.e("convert", couponList.getData().size() + " "); } } else if (couponList.getErrorCode().equals("404")) { if (CurrentIndex == 1) { recyclerView.removeAllViews(); dataList.clear(); couponListAdapter.notifyDataSetChanged(); // couponListAdapter.setEmptyView(R.layout.activity_jcvideoplayer, recyclerView);//添加空布局 } else { couponListAdapter.loadMoreEnd(); } } }) .error(e -> { refreshLayout.setRefreshing(false); Log.e("error==>", e + ""); }) .post(); } /** * 添加headView 或footerView */ private void addHeadView() { View headView = getLayoutInflater().inflate(R.layout.item_foot, null); headView.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); //headView添加监听 headView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(TestActivity.this, "click HeadView", Toast.LENGTH_LONG).show(); } }); // couponListAdapter.addHeaderView(headView); couponListAdapter.addFooterView(headView); } }
注释已经很清楚了,相信大家都能看的懂,在这就不多做解释了。如果遇到以下问题:
Android之RecyclerView的原生Bug-Inconsistency detected. Invalid view holder adapter positionViewHolder{a1bbfa3 position=2 id=-1, oldPos=-1, pLpos:-1 no parent},recyclerviewholder
以及:java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{ec75dcb position=18 id=-1, oldPos=-1, pLpos:-1 not recyclable(1)} android.support.v7.widget.RecyclerView{b3515af VFED..... ......ID 0,0-1080,1230 #7f0900ec app:id/recycleView}, adapter:com.xgh.app.demo2.CouponListAdapterdemo@68fbdbc, layout:com.xgh.app.demo2.WrapContentLinearLayoutManager@26b4145, context:com.xgh.app.demo2.DemoActivity@6973c1d请参考:https://blog.csdn.net/xiaoshuxgh/article/details/80225516