集成步骤:
1.先在项目 build.gradle(Project:XXXX) 的 repositories 添加:
maven { url "https://jitpack.io" }
2.然后在 build.gradle(Module:app) 的 dependencies 添加:
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.1'
3.xml布局
4.Activity
public class MainActivity extends AppCompatActivity { private RecyclerView recycleView; private MainAdapter mainAdapter; private ListhomeList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getHomeList(); initViw(); } public void initViw() { recycleView = findViewById(R.id.recycleView); mainAdapter = new MainAdapter(R.layout.item_user, homeList); recycleView.setAdapter(mainAdapter); } public void getHomeList(){ homeList = new ArrayList<>(); HomeItem homeItem = new HomeItem(); homeItem.name = "张三丰"; homeItem.desc = "一代武术大师"; homeItem.age = 103; homeList.add(homeItem); homeItem = new HomeItem(); homeItem.name = "柯南"; homeItem.desc = "名侦探,走到哪里哪里就有命案发生"; homeItem.age = 20; homeList.add(homeItem); homeItem = new HomeItem(); homeItem.name = "鸣人"; homeItem.desc = "火影的主角,主角光环,一步一步提升"; homeItem.age = 103; homeList.add(homeItem); } }
5.Adapter
public class MainAdapter extends BaseQuickAdapter{ public MainAdapter(int layoutResId, List data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder baseViewHolder, HomeItem homeItem) { baseViewHolder.setText(R.id.tvName, homeItem.name); //输入文字 TextView tvContent = baseViewHolder.getView(R.id.tvContent); tvContent.setText("获取ID后赋值"); baseViewHolder.setImageResource(R.id.imgIcon ,R.mipmap.ic_launcher); //设置加载图片 // baseViewHolder.setBackgroundColor() //设置背景颜色 // baseViewHolder.setEnabled() //设置是否可以点击 // baseViewHolder.setGone() //是否隐藏 // baseViewHolder.setVisible() //是否显示 } }
6.设置Item点击事件
public void initViw() { recycleView = findViewById(R.id.recycleView); mainAdapter = new MainAdapter(R.layout.item_user, homeList); recycleView.setAdapter(mainAdapter); mainAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) { Log.e("rvAdapterHelper" ,"单击事件"); } }); mainAdapter.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) { Log.e("rvAdapterHelper" ,"长按事件"); return false; } }); /** * 绑定child的ViewID,用于点击事件 * 注意:设置子控件的事件,如果不在adapter中绑定,点击事件无法生效,因为无法找到你需要设置的控件。 * */ mainAdapter.addChildClickViewIds(R.id.imgIcon); mainAdapter.setOnItemChildClickListener(new OnItemChildClickListener() { @Override public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) { Log.e("rvAdapterHelper" ,"onItemChildClick"); } }); }
7.添加列表加载动画
开启动画(默认为渐显效果)adapter.openLoadAnimation();
默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左)
切换动画
quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
自定义动画
quickAdapter.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)
};
}
});
动画默认只执行一次,如果想重复执行可设置
mQuickAdapter.isFirstOnly(false);
8.添加头部、尾部
添加
mQuickAdapter.addHeaderView(getView());
mQuickAdapter.addFooterView(getView());
删除指定view
mQuickAdapter.removeHeaderView(getView);
mQuickAdapter.removeFooterView(getView);
删除所有
mQuickAdapter.removeAllHeaderView();
mQuickAdapter.removeAllFooterView();
默认出现了头部就不会显示Empty,和尾部,配置以下方法也支持同时显示:
setHeaderAndEmpty
setHeaderFooterEmpty
默认头部尾部都是占满一行,如果需要不占满可以配置:
setHeaderViewAsFlow
setFooterViewAsFlow
还有更多的属性,可以点击链接:https://blog.csdn.net/chehec2010/article/details/83902263 查看
最后是交流公众号,大家可以关注一下