Skip to content

Commit 7980544

Browse files
author
catch-pig
committed
解决RecycleAdapter添加footer数据下标越界的崩溃问题
1 parent c827e51 commit 7980544

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

CHANGE_lOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# CHANGE LOG
2+
### v0.3.4(2021.12.5)
3+
4+
+ 解决RecycleAdapter添加footer数据下标越界的崩溃问题
5+
26
### v0.3.3(2021.12.4)
37

4-
注解ServiceApi增减debugInterceptors参数,只有NetManager.setDebug(true)的情况下,拦截器才会添加到OkHttp里面
8+
+ 注解ServiceApi增减debugInterceptors参数,只有NetManager.setDebug(true)的情况下,拦截器才会添加到OkHttp里面
59

610
### v0.3.2(2021.11.24)
711

app/src/main/java/com/catchpig/kmvvm/config/MvvmGlobalConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MvvmGlobalConfig : IGlobalConfig {
4747
}
4848

4949
override fun getPageSize(): Int {
50-
return 18
50+
return 16
5151
}
5252

5353
override fun getStartPageIndex(): Int {

app/src/main/java/com/catchpig/kmvvm/recycle/RecycleActivity.kt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import androidx.recyclerview.widget.LinearLayoutManager
55
import com.bumptech.glide.Glide
66
import com.catchpig.annotation.StatusBar
77
import com.catchpig.kmvvm.R
8+
import com.catchpig.kmvvm.config.MvvmGlobalConfig
89
import com.catchpig.kmvvm.databinding.ActivityRecycleBinding
10+
import com.catchpig.kmvvm.databinding.LayoutFooterBinding
911
import com.catchpig.kmvvm.databinding.LayoutHeaderBinding
1012
import com.catchpig.kmvvm.entity.User
13+
import com.catchpig.mvvm.apt.KotlinMvvmCompiler
1114
import com.catchpig.mvvm.base.activity.BaseActivity
1215
import com.gyf.immersionbar.ktx.immersionBar
1316
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
@@ -43,20 +46,25 @@ class RecycleActivity : BaseActivity<ActivityRecycleBinding>() {
4346
userAdapter.headerView<LayoutHeaderBinding> {
4447
headerName.text = "我是头部"
4548
}
49+
userAdapter.footerView<LayoutFooterBinding> {
50+
footerName.text = "我是底部"
51+
}
4652

4753
bodyBinding.refresh.setOnRefreshLoadMoreListener { nextPageIndex ->
48-
Flowable.timer(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe {
49-
var data: MutableList<User> = ArrayList()
50-
var count = if (nextPageIndex == 3) {
51-
15
52-
} else {
53-
16
54-
}
55-
for (i in 1..count) {
56-
data.add(User("姓名$i"))
54+
Flowable.timer(3, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread())
55+
.subscribe {
56+
var data: MutableList<User> = ArrayList()
57+
val pageSize = KotlinMvvmCompiler.globalConfig().getPageSize()
58+
var count = if (nextPageIndex == 3) {
59+
pageSize - 1
60+
} else {
61+
pageSize
62+
}
63+
for (i in 1..count) {
64+
data.add(User("姓名$i"))
65+
}
66+
userAdapter.autoUpdateList(data)
5767
}
58-
userAdapter.autoUpdateList(data)
59-
}
6068
}
6169
bodyBinding.refresh.autoRefresh()
6270
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/header"
4+
android:layout_width="match_parent"
5+
android:layout_height="200dp"
6+
android:gravity="center"
7+
android:orientation="vertical">
8+
9+
<TextView
10+
android:id="@+id/footer_name"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:text="底部" />
14+
</LinearLayout>

mvvm/src/main/java/com/catchpig/mvvm/base/adapter/RecyclerAdapter.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,19 @@ abstract class RecyclerAdapter<M, VB : ViewBinding>(private val iPageControl: IP
299299
* @return Int
300300
*/
301301
override fun getItemViewType(position: Int): Int {
302-
return if (position == 0 && showEmpty) {
302+
return if (position == 0 && headerView == null && showEmpty) {
303+
//当前数据空位,展示空页面
304+
TYPE_EMPTY.value
305+
} else if (position == 1 && headerView != null && showEmpty) {
303306
//当前数据空位,展示空页面
304307
TYPE_EMPTY.value
305308
} else if (position == 0 && headerView != null) {
306309
//当前view是头部信息
307310
TYPE_HEADER.value
308-
} else if (position == itemCount && footerView != null) {
311+
} else if (position == (itemCount - 1) && headerView != null && footerView != null) {
312+
//当前view是底部信息
313+
TYPE_FOOTER.value
314+
} else if (position == itemCount && headerView == null && footerView != null) {
309315
//当前view是底部信息
310316
TYPE_FOOTER.value
311317
} else getCenterViewType(position)

0 commit comments

Comments
 (0)