Android进阶之路 - BaseQuickAdapter通用适配器

常规项目中我们用到Adapter的场景比比皆是,针对于如此频繁的需求,我们一般都会封装一个Base出来,那么BaseQuickAdapter就是一个比较成熟的BaseAdapter !

前言

在当前实际开发的项目中已经很少用到ListView这款老控件了,大多用的RecyclerView,一般的话我们都自己有一些简单的封装,但是相对而言不是很完善,扩展性并不是很强,所有找到了
BaseRecyclerViewAdapterHelper框架,它是一款扩展性较强的BaseAdapter,同时在Github的Start较多,也使用便捷,所以在此情况下,我们来学习一下它的使用方法 ~

Effect
Android进阶之路 - BaseQuickAdapter通用适配器_第1张图片

提前引入

build.gradle (项目级)

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

build.gradle (app级):

 implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
 implementation 'com.android.support:recyclerview-v7:27.1.1'

常规使用

TextBean
Demo列表为常规展示,为了说明使用方式,这里我们建立一个简单的Model

package com.bakheet.effect.adaptertext;

/**
 * Created by YongLiu on 2017/11/10.
 */

public class TextBean {
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

TextAdapter
创建Adapter继承BaseQuickAdapter,只实现俩个方法即可,但是需要你绑定对应的model ~

package com.bakheet.effect.adaptertext;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Created by YongLiu on 2017/11/10.
 * 可在对应的Adapter头部声明实体类的泛型,BaseViewHolder为通用型
 */
public class TextAdapter extends BaseQuickAdapter<TextBean, BaseViewHolder> {

    public TextAdapter(@LayoutRes int layoutResId, @Nullable List data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, TextBean item) {
        helper.setText(R.id.tv_item,item.getName());

		/*这里设置子条目点击事件需要注意一下!
		如果 添加了固定的某控件id点击事件(如下文的 .addOnLongClickListener(R.id.tv_btn)),
		同时你又把子条目的id控件设置成了match的属性!那么OnItemOnClick你会发现调用不到!!!
		这时候你需要用到的子条目监听就是setOnItemChildClickListener,然后去绑定对应的id!
		如果只是需要子条目的点击事件,,不需要add任何Listener!!!
		**/
        helper.addOnClickListener(R.id.tv_btn).addOnLongClickListener(R.id.tv_btn);
    }
}

MainActivity
使用常规的RecyclerView控件即可,因为我们主要作用于Adapter方面

package com.bakheet.effect.adaptertext;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRv;
    private TextAdapter adapter;
    private ArrayList<TextBean> lists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lists = new ArrayList<TextBean>();
        initData();

        mRv = (RecyclerView) findViewById(R.id.rv);
        mRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new TextAdapter(R.layout.item_layout, lists);
        mRv.setAdapter(adapter);

        adapter.openLoadAnimation();

//        默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左)
//        adapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "触发子条目提示" + position, Toast.LENGTH_SHORT).show();
            }
        });

        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {

                Toast.makeText(MainActivity.this, "子条目长按提示" + position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_btn:
                        Toast.makeText(MainActivity.this, "内部 - 子条目点击提示" + position, Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        });

        adapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "内部 - 子条目长按提示" + position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
	
	//数据模拟
    private void initData() {
        for (int i = 0; i < 55; i++) {
            TextBean textBean = new TextBean();
            textBean.setName("吃土一个月" + i);
            lists.add(textBean);
        }
    }
}

activity_main


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bakheet.effect.adaptertext.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv"
        />
LinearLayout>

使用扩展

动画引入

//        默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左)
//        adapter.openLoadAnimation(BaseQuickAdapter.FOOTER_VIEW);

事件监听
如只使用普通的Item点击和长按操作,可直接调用

        //点击监听
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "触发子条目提示" + position, Toast.LENGTH_SHORT).show();
            }
        });
        
        //长按监听
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "子条目长按提示" + position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

同时设置Item内部子视图的点击和长按监听
如下面第一种,可处理内部多个事件,需要绑定对应的Id,但是需要在Adapter内设置设个事件

adapter设置方式

    //一个点击一个长按
    helper.addOnClickListener(R.id.tv_btn).addOnLongClickListener(R.id.tv_btn);

使用方式

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_btn:
                        Toast.makeText(MainActivity.this, "内部 - 子条目点击提示" + position, Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        });

        adapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "内部 - 子条目长按提示" + position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });

扩展资源

  • 原作者Github地址
  • 中文文档地址
  • 此Demo下载地址

你可能感兴趣的:(#,三方框架)