BaseRecyclerViewAdapterHelper 教程
简介
RecyclerView是一个强大的组件,用来代替ListView和GridView,可以实现数据纵向滚动以及横向滚动、瀑布流布局,提供更高效的回收和复用机制,同时也能实现管理,目前被广泛使用。
BaseRecyclerViewAdapterHelper是一个RecyclerView.Adapter的框架,能节省大量的开发时间,因为在其中集成了大部分常用需求的解决方案。
教程
导入框架
在
build.gradle(Project)
中添加JitPack
库build.gradle(Project) 1
2
3
4
5
6allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}在
build.gradle(Module)
中添加RecyclerView
依赖build.gradle(Module) 1
2
3
4
5dependencies {
...
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.0-beta11'
}
简单例子
在布局文件中添加
RecyclerView
组件activity_main.xml 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>编写条目布局文件
recyclerview_main.xml 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>编写数据实体类
MainAdapterData.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18class MainAdapterData {
private String title;
private String info;
String getTitle() {
return title;
}
void setTitle(String title) {
this.title = title;
}
String getInfo() {
return info;
}
void setInfo(String info) {
this.info = info;
}
}编写适配器
MainAdapter.java 1
2
3
4
5
6
7
8
9
10public class MainAdapter extends BaseQuickAdapter<MainAdapterData, BaseViewHolder> implements LoadMoreModule, DraggableModule {
MainAdapter(int layoutResId, List<MainAdapterData> list) {
super(layoutResId, list);
}
protected void convert(BaseViewHolder helper, MainAdapterData item) {
helper.setText(R.id.textView, item.getTitle())
.setText(R.id.button, item.getInfo());
}
}在
Activity
中使用适配器MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建数据
List<MainAdapterData> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
list.add(mainAdapterData);
}
//创建适配器
MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, list);
//设置RecyclerView.布局管理
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置RecyclerView.适配器
recyclerView.setAdapter(adapter);
}
}演示
空布局
编写空布局文件
recyclerview_empty.xml 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:gravity="center"
android:text="空"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>在
Activity
中使用MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建适配器
MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, null);
//设置RecyclerView.布局管理
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置RecyclerView.适配器
recyclerView.setAdapter(adapter);
//创建适配器.空布局
//在setAdapter后使用 且 new MainAdapter第二个参数为null
adapter.setEmptyView(R.layout.recyclerview_empty);
}
}演示
头部与尾部
编写头部布局文件
recyclerview_header.xml 1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Header"
android:textSize="24sp" />
</LinearLayout>编写尾部布局文件
recyclerview_footer.xml 1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Footer"
android:textSize="24sp" />
</LinearLayout>在
Activity
中使用MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建数据
List<MainAdapterData> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
list.add(mainAdapterData);
}
//创建适配器
MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, list);
//创建适配器.头部
//adapter.setHeaderWithEmptyEnable(true);//同时显示头部与空布局
//adapter.setFooterWithEmptyEnable(true);//同时显示尾部与空布局
//adapter.removeHeaderView();//删除指定头部
//adapter.removeFooterView();//删除指定尾部
//adapter.removeAllHeaderView();//删除所有头部
//adapter.removeAllFooterView();//删除指定尾部
//adapter.setHeaderViewAsFlow(true);//设置头部不占满一行
//adapter.setFooterViewAsFlow(true);//设置尾部不占满一行
adapter.setHeaderView(View.inflate(this, R.layout.recyclerview_header, null));//设置头部,可添加多个头部
adapter.setFooterView(View.inflate(this, R.layout.recyclerview_footer, null));//设置尾部,可添加多个尾部
//设置RecyclerView.布局管理
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置RecyclerView.适配器
recyclerView.setAdapter(adapter);
}
}演示
动画
开启并设置动画
1
2
3
4adapter.setAnimationEnable(true);//开启动画-默认渐显效果类型
adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.AlphaIn);//设置默认动画类型,如AlphaIn、SlideInLeft、SlideInRight、SlideInBottom、ScaleIn等
adapter.setAnimationFirstOnly(false);//设置始终显示动画
adapter.setAdapterAnimation(new BaseAnimation());//设置自定义动画例子
MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建数据
List<MainAdapterData> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
list.add(mainAdapterData);
}
//创建适配器
MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, list);
adapter.setHeaderView(View.inflate(this, R.layout.recyclerview_header, null));
adapter.setFooterView(View.inflate(this, R.layout.recyclerview_footer, null));
//创建适配器.动画
adapter.setAnimationEnable(true);//打开动画
adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInLeft);//设置动画类型
adapter.setAnimationFirstOnly(false);//设置始终显示动画
//设置RecyclerView.布局管理
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//设置RecyclerView.适配器
recyclerView.setAdapter(adapter);
}
}演示
事件
条目事件
点击事件
1 | adapter.setOnItemClickListener(new OnItemClickListener() {//监听条目的点击事件 |
长按事件
1 | adapter.setOnItemLongClickListener(new OnItemLongClickListener() {//监听条目的长按事件 |
条目子组件事件
点击事件
1 | adapter.addChildClickViewIds(R.id.button);//注册条目子组件的点击事件 |
长按事件
1 | adapter.addChildLongClickViewIds(R.id.button);//注册条目子组件的长按事件 |
例子
代码
MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
final RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建数据
List<MainAdapterData> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
list.add(mainAdapterData);
}
//创建适配器
final MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, list);
//创建适配器.组件事件
adapter.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "onItemClick" + position, Toast.LENGTH_SHORT).show();
}
});
adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "onItemLongClick" + position, Toast.LENGTH_SHORT).show();
return true;
}
});
//创建适配器.子组件事件
adapter.addChildClickViewIds(R.id.button);//注册条目子组件的点击事件
adapter.setOnItemChildClickListener(new OnItemChildClickListener() {//监听条目子组件的点击事件
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
if (view.getId() == R.id.button) {
Toast.makeText(MainActivity.this, "onItemChildClick" + position, Toast.LENGTH_SHORT).show();
//获取其他子控件,如有设置头部则position应加头部布局数量
TextView textView = (TextView) adapter.getViewByPosition(position, R.id.textView);
Objects.requireNonNull(textView).setText("Clicked Me");
}
}
});
adapter.addChildLongClickViewIds(R.id.button);//注册条目子组件的长按事件
adapter.setOnItemChildLongClickListener(new OnItemChildLongClickListener() {//监听条目子组件的长按事件
public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
if (view.getId() == R.id.button) {
Toast.makeText(MainActivity.this, "onItemChildLongClick" + position, Toast.LENGTH_SHORT).show();
}
return true;
}
});
//设置RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}演示
上拉加载
在
Adapter
中导入LoadMoreModule
1
public class MainAdapter extends BaseQuickAdapter<MainAdapterData, BaseViewHolder> implements LoadMoreModule
编写代码
MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
final RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建数据
List<MainAdapterData> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
list.add(mainAdapterData);
}
//创建适配器
final MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, list);
//创建适配器.上拉加载
BaseLoadMoreModule loadMore = adapter.getLoadMoreModule();
assert loadMore != null;
loadMore.setEnableLoadMore(true);//打开上拉加载
loadMore.setAutoLoadMore(true);//自动加载
loadMore.setPreLoadNumber(1);//设置滑动到倒数第几个条目时自动加载,默认为1
loadMore.setEnableLoadMoreIfNotFullPage(true);//当数据不满一页时继续自动加载
//loadMore.setLoadMoreView(new BaseLoadMoreView)//设置自定义加载布局
loadMore.setOnLoadMoreListener(new OnLoadMoreListener() {//设置加载更多监听事件
public void onLoadMore() {
recyclerView.postDelayed(new Runnable() {//延迟以提升用户体验
public void run() {
List<MainAdapterData> lists = new ArrayList<>();
for (int i = 0; i < 5; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
lists.add(mainAdapterData);
}
adapter.addData(lists);
adapter.getLoadMoreModule().loadMoreComplete();
//adapter.loadMoreEnd()//加载完毕
//adapter.loadMoreFail()//加载失败
}
}, 500);
}
});
//设置RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}演示
滑动删除
在
Adapter
中导入DraggableModule
1
public class MainAdapter extends BaseQuickAdapter<MainAdapterData, BaseViewHolder> implements DraggableModule
编写代码
MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
final RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建数据
List<MainAdapterData> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
list.add(mainAdapterData);
}
//创建适配器
final MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, list);
//创建适配器.侧滑删除
BaseDraggableModule draggableModule = adapter.getDraggableModule();
assert draggableModule != null;
draggableModule.setSwipeEnabled(true);//启动侧滑删除
draggableModule.getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START);//侧滑删除方向
draggableModule.setOnItemSwipeListener(new OnItemSwipeListener() {//侧滑删除监听
public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
}
public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
}
public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
Toast.makeText(MainActivity.this, "onItemSwipe" + viewHolder.getLayoutPosition(), Toast.LENGTH_SHORT).show();
}
public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {//滑动时的背景
canvas.drawColor(Color.parseColor("#00DDB6"));
}
});
//设置RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}演示
拖拽
在
Adapter
中导入DraggableModule
1
public class MainAdapter extends BaseQuickAdapter<MainAdapterData, BaseViewHolder> implements DraggableModule
编写代码
MainActivity.java 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定义RecyclerView
final RecyclerView recyclerView = findViewById(R.id.recyclerView);
//创建数据
List<MainAdapterData> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
MainAdapterData mainAdapterData = new MainAdapterData();
mainAdapterData.setTitle(String.valueOf(i));
mainAdapterData.setInfo(i + "s");
list.add(mainAdapterData);
}
//创建适配器
final MainAdapter adapter = new MainAdapter(R.layout.recyclerview_main, list);
//创建适配器.拖拽
BaseDraggableModule draggableModule = adapter.getDraggableModule();
assert draggableModule != null;
draggableModule.setDragEnabled(true);//启动拖拽
draggableModule.getItemTouchHelperCallback().setDragMoveFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN);//拖拽方向
draggableModule.setOnItemDragListener(new OnItemDragListener() {//拖拽监听
public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos) {
Toast.makeText(MainActivity.this, "onItemDragBefore" + viewHolder.getLayoutPosition(), Toast.LENGTH_SHORT).show();
}
public void onItemDragMoving(RecyclerView.ViewHolder source, int from, RecyclerView.ViewHolder target, int to) {
}
public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos) {
Toast.makeText(MainActivity.this, "onItemDragAfter" + viewHolder.getLayoutPosition(), Toast.LENGTH_SHORT).show();
}
});
//设置RecyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}演示
常见问题
- Q:数据不显示?
A:给RecyclerView
设置LayoutManager
。 - Q:单条目占满屏幕?
A:将条目的布局最外层Layout
的layout_height
属性设置为wrap_parent
。
参考资料
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Coolkk!
评论