BRVAH_基本使用

BRVAH是GitHub上的一个RecyclerView 框架,非常强大 。
代码可以运行,GitHub仓库和依赖要加上去
其他的看代码注释

activity_main






item1

  
  

  


MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private List mDatas;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //模拟数据
    initData();
    //获取控件
    mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
    //方向
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    //item布局、列表数据源
    HomeAdapter mAdapter = new HomeAdapter(R.layout.item1, mDatas);
    //适配器
    mRecyclerView.setAdapter(mAdapter);
    //动画
    mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
    //单击事件
    mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            Toast.makeText(MainActivity.this, "单击" + mDatas.get(position), Toast.LENGTH_SHORT).show();
        }
    });
    //长按事件
    mAdapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
        @Override
        public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
            Toast.makeText(MainActivity.this, "长按" + mDatas.get(position), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

  }

protected void initData() {
    mDatas = new ArrayList<>();
    for (int i = 'A'; i < 'Z'; i++) {
        mDatas.add("" + (char) i);
        }
    }
}

HomeAdapter

import android.graphics.Color;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;

class HomeAdapter extends BaseQuickAdapter {

HomeAdapter(int layoutResId, List data) {
    super(layoutResId, data);
}

@Override
protected void convert(BaseViewHolder helper, String data) {
    helper.setText(R.id.id_num, data);
    //添加监听事件
    helper.addOnClickListener(R.id.id_num);
    //getLayoutPosition() 获取当前item的position
    if (helper.getAdapterPosition() % 2 == 0) {
        helper.setTextColor(R.id.id_num, Color.RED);
    } else {
        helper.setTextColor(R.id.id_num, Color.YELLOW);
    }
  }
}

maven 仓库(Project : test)

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

添加依赖(Module:test)

dependencies {
    ...
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30' 
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    ...
}

你可能感兴趣的:(BRVAH_基本使用)