1、注入依赖
allprojects {
repositories {
google()
//jcenter()
maven { url ‘https://maven.aliyun.com/repository/google’ }
maven { url ‘https://maven.aliyun.com/repository/jcenter’ }
maven { url ‘http://maven.aliyun.com/nexus/content/groups/public/’ }
maven { url "https://jitpack.io" }//第三方
}
}
app:
//adpter適配器
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22'
//可实现瀑布流
implementation 'com.android.support:recyclerview-v7:28.0.0'
2、Activity中使用
package com.example.administrator.baserecyclerviewadapterhelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List datas;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化RecyclerView
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//模拟的数据(实际开发中一般是从网络获取的)
datas = new ArrayList<>();
Model model;
for (int i = 0; i < 15; i++) {
model = new Model();
model.setTitle("我是第" + i + "条标题");
model.setContent("第" + i + "条内容");
datas.add(model);
}
//创建布局管理
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//创建适配器
adapter = new MyAdapter(R.layout.item_mian, datas);
//给RecyclerView设置适配器
recyclerView.setAdapter(adapter);
//条目子控件点击事件
adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//判断id
if (view.getId() == R.id.iv_img) {
Log.i("tag", "点击了第" + position + "条条目的 图片");
} else if (view.getId() == R.id.tv_title) {
Log.i("tag", "点击了第" + position + "条条目的 标题");
}
}
});
//条目长按事件(和条目子控件点击事件类似)
adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "长按了第" + (position + 1) + "条条目", Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
3、adpter
package com.example.administrator.baserecyclerviewadapterhelper;
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;
public class MyAdapter extends BaseQuickAdapter {
public MyAdapter(@LayoutRes int layoutResId, @Nullable List data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, Model item) {
//可链式调用赋值
helper.setText(R.id.tv_title, item.getTitle())
.setText(R.id.tv_content, item.getContent())
.addOnClickListener(R.id.iv_img) //给图标添加 点击事件
.addOnClickListener(R.id.tv_title) //给标题也添加 点击事件
//.addOnLongClickListener(R.id.tv_title)
.setImageResource(R.id.iv_img, R.mipmap.ic_launcher);
//获取当前条目position
//int position = helper.getLayoutPosition();
}
}
4、添加特效
//开启动画(默认为渐显效果)
adapter.openLoadAnimation();
详情网站
https://www.jianshu.com/p/1e20f301272e