android开发技巧Android DeveloperAndroid面试知识

BaseQuickAdapter——RecyclerView通用

2017-09-19  本文已影响224人  So_ProbuING

BaseQuickAdapter——RecyclerView通用适配器

RecyclerView是Android L版本中新添加的一个用于取代ListView的SDK,具有灵活性和可替代性比ListView更好,RecyclerView同样也用到适配,作为开发者,我们希望有一款通用的适配。

BaseQuickAdapter 特点

BaseQuickAdapter 使用

导入依赖

    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }
    dependencies {
            compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.28'
    }
# gradle
//最新代码还没更新到 jcenter 仓库,推荐使用方法二依赖项目
compile 'com.github.baserecycleradapter:library:1.1.0'
# 下载源码依赖

使用

mRecyclerView.setAdapter(mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.rv_item, getItemDatas()) {
    @Override
    protected void convert(final BaseViewHolder helper, String item) {
        helper.setText(R.id.tv_item_text, item);

    }
});

//Item 点击事件
setOnItemClickListener(int viewId, AdapterView.OnItemClickListener listener)

//长按事件
setOnItemLongClickListener(int viewId, AdapterView.OnItemLongClickListener listener)
helper.setOnClickListener(R.id.tv_item_text, new View.OnClickListener() {
       @Override
       public void onClick(View v) {
          //事件处理
       }
   });

    //头部
    mAdapter.addHeaderView();
    View headerView=getLayoutInflater().inflate(R.layout.rv_header, null);

    headerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));

    mAdapter.addHeaderView(headerView);
    
    //尾部
    mAdapter.addFooterView();
 //一行代码开启动画 默认CUSTOM动画
  mAdapter.openLoadAnimation(BaseQuickAdapter.CUSTOMIN);
 # 更换动画
 //ALPHAIN, SCALEIN, SLIDEIN_BOTTOM, SLIDEIN_LEFT, SLIDEIN_RIGHT, //SLIDEIN_LEFT_RIGHT, SLIDEIN_BOTTOM_TOP, CUSTOMIN
mAdapter.openLoadAnimation(BaseQuickAdapter.CUSTOMIN);
//自定义
mAdapter.openLoadAnimation(new BaseAnimation[]{
        new BaseAnimation() {
            @Override
            public Animator[] getAnimators(View view) {
                return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", 0.5f, 1.0f),
                        ObjectAnimator.ofFloat(view, "scaleX", 0.5f, 1.0f)};
            }
        }
});

mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
        @Override
        public void onLoadMoreRequested() {
        }
    });

mShowType++;
    if (mShowType == 2) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //加载失败,点击重试
                mAdapter.loadMoreFail();
            }
        }, DELAY_MILLIS);
    } else if (mShowType >= 4) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                //加载完成
                mAdapter.loadMoreEnd();
            }
        });
    } else {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.addData(addDatas());
                //加载更多完成
                mAdapter.loadMoreComplete();
            }
        }, DELAY_MILLIS);
    }

View emptyView=getLayoutInflater().inflate(R.layout.rv_empty, null);
    emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    //添加空视图
    mAdapter.setEmptyView(emptyView);

    emptyView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "your click empty", Snackbar.LENGTH_SHORT).show();
        }
    });

ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(mAdapter);

ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(callback);

mAdapter.setItemTouchHelper(mItemTouchHelper);

mAdapter.setDragViewId(R.id.iv_drag);

mItemTouchHelper.attachToRecyclerView(mRecyclerView);

mRecyclerView.setAdapter(mAdapter = new BaseMultiItemAdapter<MultiItem>(this, getMultiItemDatas()) {
    @Override
    protected void convert(BaseViewHolder helper, MultiItem item) {
        switch (helper.getItemViewType()) {
            case MultiItem.SEND:
                helper.setText(R.id.chat_from_content, item.content);
                //helper.setImageBitmap(R.id.chat_from_icon,getRoundCornerBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.chat_head), 16));
                break;
            case MultiItem.FROM:
                helper.setText(R.id.chat_send_content, item.content);
                //helper.setImageBitmap(R.id.chat_send_icon,getRoundCornerBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.from_head), 16));
                break;
        }
    }
    @Override
    protected void addItemLayout() {
        addItemType(MultiItem.SEND, R.layout.chat_send_msg);
        addItemType(MultiItem.FROM, R.layout.chat_from_msg);
    }
});
mAdapter.openLoadAnimation(true);
btnSend.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        MultiItem multiItem = new MultiItem();
        multiItem.itemType = MultiItem.SEND;
        multiItem.content = etChat.getText().toString();
        mAdapter.add(multiItem);
        mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount());
        //mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
    }
});

mRecyclerView.setLayoutManager(new FlowLayoutManager());
 mRecyclerView.setLayoutManager(new OverLayCardLayoutManager());

Document

http://www.jianshu.com/p/b343fcff51b0
http://www.jianshu.com/p/a00b4a3a1838

上一篇下一篇

猜你喜欢

热点阅读