Skip to content

Commit 6a34571

Browse files
committed
Update refresh
1 parent 9ae9137 commit 6a34571

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

app/src/main/java/com/hoc/pagination_mvi/ui/main/MainAdapter.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hoc.pagination_mvi.ui.main
22

33
import android.graphics.Rect
4+
import android.os.Parcelable
45
import android.util.Log
56
import android.view.LayoutInflater
67
import android.view.View
@@ -51,6 +52,8 @@ private object DiffUtilItemCallback : DiffUtil.ItemCallback<Item>() {
5152

5253
class MainAdapter(private val compositeDisposable: CompositeDisposable) :
5354
ListAdapter<Item, MainAdapter.VH>(DiffUtilItemCallback) {
55+
private val scrollToFirst = PublishSubject.create<Unit>()
56+
private var layoutManagerSavedState: Parcelable? = null
5457

5558
private val retryS = PublishSubject.create<Unit>()
5659
val retryObservable get() = retryS.asObservable()
@@ -167,14 +170,18 @@ class MainAdapter(private val compositeDisposable: CompositeDisposable) :
167170
private val progressBar = itemView.progress_bar_horizontal!!
168171
private val textError = itemView.text_error_horizontal!!
169172
private val buttonRetry = itemView.button_retry_horizontal!!
173+
170174
private val adapter = HorizontalAdapter(compositeDisposable)
171175
private val visibleThreshold get() = 2
176+
internal val linearLayoutManager =
177+
LinearLayoutManager(itemView.context, RecyclerView.HORIZONTAL, false)
172178

173179
init {
174180
recycler.run {
175181
setHasFixedSize(true)
176182
adapter = this@HorizontalListVH.adapter
177-
layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
183+
layoutManager = this@HorizontalListVH.linearLayoutManager
184+
178185
addItemDecoration(object : RecyclerView.ItemDecoration() {
179186
override fun getItemOffsets(
180187
outRect: Rect,
@@ -216,6 +223,10 @@ class MainAdapter(private val compositeDisposable: CompositeDisposable) :
216223
}
217224
.subscribeBy { loadNextPageHorizontalS.onNext(Unit) }
218225
.addTo(compositeDisposable)
226+
227+
scrollToFirst
228+
.subscribeBy { recycler.scrollToPosition(0) }
229+
.addTo(compositeDisposable)
219230
}
220231

221232
override fun bind(item: Item) {
@@ -231,6 +242,20 @@ class MainAdapter(private val compositeDisposable: CompositeDisposable) :
231242
textError.text = item.error?.message
232243

233244
adapter.submitList(item.items)
245+
246+
layoutManagerSavedState?.let {
247+
linearLayoutManager.onRestoreInstanceState(it)
248+
layoutManagerSavedState = null
249+
}
234250
}
235251
}
252+
253+
override fun onViewRecycled(holder: VH) {
254+
super.onViewRecycled(holder)
255+
if (holder is HorizontalListVH) {
256+
layoutManagerSavedState = holder.linearLayoutManager.onSaveInstanceState()
257+
}
258+
}
259+
260+
fun scrollHorizontalListToFirst() = scrollToFirst.onNext(Unit)
236261
}

app/src/main/java/com/hoc/pagination_mvi/ui/main/MainFragment.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class MainFragment : Fragment() {
172172
return when (event) {
173173
MainContract.SingleEvent.RefreshSuccess -> {
174174
toast("Refresh success")
175+
adapter.scrollHorizontalListToFirst()
175176
}
176177
is MainContract.SingleEvent.RefreshFailure -> {
177178
toast(

0 commit comments

Comments
 (0)