1 先导入依赖:
dependencies {
implementation'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
}
2 在build project中添加:
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
3 适配器 BaseQuickAdapter<需要解析的bean类,BaseViewHolder>
helper.setText(需要赋值的id,解析值);
图片用Glide
public class MyAdapter extends BaseQuickAdapter<User.ResultBean.DataBean, BaseViewHolder> {
public MyAdapter(int layoutResId, @Nullable List<User.ResultBean.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, User.ResultBean.DataBean item) {
helper.setText(R.id.mTitle, item.getTitle())
.setText(R.id.mAuthor_name, item.getAuthor_name())
.setText(R.id.mCategory, item.getCategory())
.setText(R.id.mDate, item.getDate());
Glide.with(mContext).load(item.getThumbnail_pic_s()).into((ImageView) helper.getView(R.id.mImg));
}
}
4 适配器使用 带点击事件:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyc.setLayoutManager(layoutManager);
adapter = new MyAdapter(R.layout.layout_main2, data);
mRecyc.setAdapter(adapter);
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
String url = data.get(position).getUrl();
Intent intent = new Intent(getContext(), Main2Activity.class);
intent.putExtra("url", url);
startActivity(intent);
}
});
5 XML文件是普通的RecyclerView布局:
<android.support.v7.widget.RecyclerView
android:id="@+id/mRecyc"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
最后再附上官方网址:官方代码