Brvah——一个强大的Adapter框架

前言

在用到RecycleView的时候,通常会写一个adapter,然后是一堆的垃圾代码,为了让adapter用起来更简便,代码更少,今天介绍一个强大的adapter库——BRVAH

本文参考以下链接:
BRVAH官网
BRVAH官方使用指南(持续更新)
BRVAH版本更新说明
在此对作者表示感谢

今天只是对此adapter对一般的点击事件及展示做一个介绍,更多使用方法,请参考上面链接

今天涉及知识:

  • BRVAH 框架配置
  • 对界面展示及基本点击事件做简单”封装”
  • 在activity中使用
一.BRVAH 框架配置

BRVAH的配置比一般的库配置要多出一步,就是配置maven。

1.1在project的build.gradle中配置maven

在project的build.gradle的allprojects下的repositories中配置,代码如下:

maven { url "https://jitpack.io" }

具体代码如下:

//在这里引入config.gradle 
apply from:"config.gradle"

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这里需要注意的有两点:

  1. 配置的是project的build.gradle,而不是app module的build.gradle
  2. maven写在allprojects–>repositories 下,而不是buildscript –>repositories 下,切记!
1.2在app module的build.gradle中引入库依赖:
//我使用时,最新版本为 2.9.34
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'

自此,BRVAH库配置完毕

二.写一个父类Adapter以供一般的adapter使用,adapter名称问为ComAdapter

注:此adapter只专注于一般的展示及点击事件,和简单header和footer的添加,若有比较复杂的用途,需要自己重新写一个adapter,此ComAdapter并不适用于复杂情况
ComAdapter代码如下:

package com.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.animation.BaseAnimation;

import java.util.List;

/**
 * Title:RecyclerView适配器基类
 * Description:
 * <p>
 * Created by pei
 * Date: 2017/12/29
 */
public abstract class ComAdapter<T> extends BaseQuickAdapter<T, BaseViewHolder> {

    protected Context mContext;
    protected List<T>mData;
    protected int mItemViewId;//item布局id
    protected View mHeaderView;
    protected View mFooterView;

    public ComAdapter(int itemViewId,List<T>data,Context context) {
        super(itemViewId, data);
        this.mItemViewId=itemViewId;
        this.mContext=context;
        this.mData=data;
    }

    @Override
    protected void convert(BaseViewHolder viewHolder, T t) {
        initView(viewHolder,t);
        initData(viewHolder,t);
        setListener(viewHolder,t);
    }

    protected abstract void initView(BaseViewHolder viewHolder, T t);
    protected abstract void initData(BaseViewHolder viewHolder, T t);
    protected abstract void setListener(BaseViewHolder viewHolder, T t);

    /**获取position,当添加有header或footer要注意改变**/
    public int getPosition(BaseViewHolder viewHolder){
        return viewHolder.getLayoutPosition();
    }

    /**获取headerView**/
    protected View getHeaderView(int headerViewId) {
        if(mContext!=null){
            mHeaderView=LayoutInflater.from(mContext).inflate(headerViewId, null);
        }
        return mHeaderView;
    }

    /**获取footerView**/
    protected View getFooterView(int footerViewId) {
        if (mContext != null&&mFooterView==null) {
            mFooterView = LayoutInflater.from(mContext).inflate(footerViewId, null);
        }
        return mFooterView;
    }

    /**添加headerView**/
    public void addHeaderView(int headerViewId){
        addHeaderView(getHeaderView(headerViewId));
    };

    /**添加footerView**/
    public void addFooterView(int footerViewId){
        addFooterView(getFooterView(footerViewId));
    }

    /**设置RecyclerView**/
    public void setRecyclerManager(RecyclerView recyclerView){
        LinearLayoutManager layoutManager=new LinearLayoutManager(mContext);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(this);
        openLoadAnimation();//默认adapter渐现效果
    }

    /**adapter渐现动画**/
    public void openAlphaAnimation(){
        openLoadAnimation(BaseQuickAdapter.ALPHAIN);
    }

    /**adapter缩放动画**/
    public void openScaleAnimation(){
        openLoadAnimation(BaseQuickAdapter.SCALEIN );
    }

    /**adapter从下到上动画**/
    public void openBottomAnimation(){
        openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM  );
    }

    /**adapter从左到右动画**/
    public void openLeftAnimation(){
        openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT   );
    }

    /**adapter从右到左动画**/
    public void openRightAnimation(){
        openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT    );
    }

    /**自定义动画**/
    public void openLoadAnimation(BaseAnimation animation){

    }

}

三.写自己的适配器NameAdapter,继承于ComAdapter
package com.adapter;

import android.content.Context;
import android.widget.TextView;

import com.brvahdemo.R;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Title:
 * Description:
 * <p>
 * Created by pei
 * Date: 2017/12/29
 */
public class NameAdapter<T>extends ComAdapter {

    private TextView mTvName;

    public NameAdapter(List<T> data, Context context){
        super(R.layout.item_layout,data,context);
    }

    @Override
    protected void initView(BaseViewHolder viewHolder, Object o) {
        mTvName=viewHolder.getView(R.id.tv_name);
    }

    @Override
    protected void initData(BaseViewHolder viewHolder, Object o) {
        mTvName.setText(o.toString());
    }

    @Override
    protected void setListener(BaseViewHolder viewHolder, Object o) {
        //mTvName点击事件
        viewHolder.addOnClickListener(mTvName.getId());
    }
}

涉及到的item_layout.xml布局代码很简单,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:background="#ff0000">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:background="#0000ff"/>

</LinearLayout>
四.在MainActivity中的简单使用
4.1先给出MainActivity的布局activity_main.xml,代码很简单,只做展示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.brvahdemo.MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

</LinearLayout>
4.2 MainActivity中使用代码

废话不多说,直接上代码是王道

package com.brvahdemo;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

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

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends AppActivity {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private List<String>mNameList;
    private NameAdapter mNameAdapter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        mNameList= Arrays.asList("小明","小红","小芳","小花","小海","小林","小叶","小虎","小柔");
        mNameAdapter=new NameAdapter(mNameList,mContext);//初始化NameAdapter
        mNameAdapter.setRecyclerManager(mRecyclerView);//设置RecyclerView特性
        mNameAdapter.addHeaderView(R.layout.item_header);//添加header
        mNameAdapter.addFooterView(R.layout.item_footer);//添加footer
        mNameAdapter.openLeftAnimation();//设置加载动画
    }

    @Override
    protected void setListener() {
        //item點擊事件
        mNameAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "onItemLongClick" + position, Toast.LENGTH_SHORT).show();
            }
        });

        //子空控件点击事件
        mNameAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "onItemLongClick" + mNameList.get(position), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

其中addHeaderView时的R.layout.item_header.xml和addFooterView时的R.layout.item_footer.xml是随便写的布局,仅供测试之用,这里就不展示出来了。

ok,今天对BRVAH库的简单介绍就到这里了,谢谢诶。

    原文作者:奔跑的佩恩
    原文地址: https://www.jianshu.com/p/cd9058ce74f6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞