RecyclerView万能适配器

原文地址:https://github.com/CymChad/BaseRecyclerViewAdapterHelper

使用步骤

1.依赖注入

 dependencies {
            compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.40'
    }

2.代码实现

继承BaseQuickAdapter(layoutId),第一个参数为数据对象,第二个参数为ViewHolder可自定义可用官方的BaseViewHolder

      val adapter=object :BaseQuickAdapter(R.layout.item_main1){
            override fun convert(helper: BaseViewHolder, item: String) {
                //绑定ID
                val view=helper.getView(R.id.time_marker)
                //获取数据集合
                mData
                //获取当前上下文
                mContext
                //获取position
                helper.adapterPosition
                //获取itemView
                helper.itemView
                //获取ViewType
                helper.itemViewType
                //设置值
                helper.setText(R.id.text,"之撒旦");
            }
        }
        //赋值
        val list=ArrayList()
        list.add("A")
        list.add("A")
        adapter.addData(list)

Item点击事件

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.d(TAG, "onItemClick: ");
                Toast.makeText(ItemClickActivity.this, "onItemClick" + position, Toast.LENGTH_SHORT).show();
            }
        });

Item长按事件

        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                Log.d(TAG, "onItemLongClick: ");
                Toast.makeText(ItemClickActivity.this, "onItemLongClick" + position, Toast.LENGTH_SHORT).show();
                return false;
            }
        });

添加动画

adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//自定义动画
adapter.openLoadAnimation(new BaseAnimation() {
                            @Override
                            public Animator[] getAnimators(View view) {
                                return new Animator[]{
                                        ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1),
                                        ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1)
                                };
                            }
                        });

添加删除头部尾部

adapter.addHeaderView(getView());
adapter.addFooterView(getView());
adapter.removeHeaderView(getView);
adapter.removeFooterView(getView);

除这些还有下拉上拉加载,空布局设置等等
中文文档地址:https://www.jianshu.com/p/b343fcff51b0

你可能感兴趣的:(RecyclerView万能适配器)