一、框架引入
1,在项目的 build.gradle的 repositories 添加:
allprojects {
repositories {
maven { url "https://jitpack.io" }
......
}
}
2,在Module的 build.gradle的 dependencies 添加依赖:
dependencies {
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
......
}
二、Adapter的简单使用方法 (demo演示)
1,在布局文件activity_main.xml中引入RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
注意:引入RecyclerView:时要在Module的 build.gradle的 dependencies 添加依赖:
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
......
}
2,写item布局文件item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_shadow"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:layout_marginLeft="@dimen/dp_10"
>
<ImageView
android:id="@+id/iv_img"
android:background="@mipmap/d"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_vertical"
android:layout_marginTop="105dp"
android:text="Thanks"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
android:text="thanks" />
</RelativeLayout>
3,创建数据实体类型文件Model.java:
package com.example.administrator.listview.utils;
public class Model {
private String etname;
private String etitem;
private int iv_image;
public int getImage() {
return iv_image;
}
public void setImage(int image) {
this.iv_image = image;
}
public String getEtname() {
return etname;
}
public String getEtitem() {
return etitem;
}
public void setEtname(String etname) {
this.etname = etname;
}
public void setEtitem(String etitem) {
this.etitem = etitem;
}
}
4,写适配器MyAdapter.java文件:
public class MyAdapter extends BaseQuickAdapter<Model, BaseViewHolder> {
public MyAdapter(@LayoutRes int layoutResId, @Nullable List<Model> data) {
super(layoutResId, data); }
@Override
protected void convert(BaseViewHolder helper, Model item) {
//可链式调用赋值,绑定子控件id
helper.setText(R.id.etname, item.getEtname())
.setText(R.id.etitem, item.getEtitem())
.setImageResource(R.id.ivimage,R.mipmap.ic_launcher)
.addOnClickListener(R.id.etname) //给item子控件添加点击事件
.addOnClickListener(R.id.ivimage); //给item子控件添加点击事件
}
注意:
需要继承BaseQuickAdapter,然后BaseQuickAdapter<Status, BaseViewHolder>第一个泛型是数据实体类型,第二个BaseViewHolder是ViewHolder其目的是为了支持扩展ViewHolder。
5,在Activity文件(MainActivity)中使用MyAdapter适配器:(加入item和item子控件的点击事件,切换动画效果
public class MainActivity extends AppCompatActivity{
private RecyclerView recyclerView;
private List<Model> data;
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);
data = new ArrayList<>();
Model model;
for (int i = 0; i < 15; i++) {
model = new Model();
model.setEtname("Thanks"+i);
model.setEtitem("Thank you"+i);
data.add(model);
}
//创建布局管理(线性布局)
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//创建适配器
adapter = new MyAdapter(R.layout.item, data);
//给RecyclerView设置适配器
recyclerView.setAdapter(adapter);
//item点击事件
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "onItemClick" + position, Toast.LENGTH_SHORT).show(