第一次嘗試用BaseRecyclerViewAdapterHelper實現上拉重新整理、載入更多、新增頭佈局、尾佈局等等功能
阿新 • • 發佈:2018-12-11
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:[email protected], layout:[email protected], context:[email protected]請參考:https://blog.csdn.net/xiaoshuxgh/article/details/80225516