Skip to content

Commit

Permalink
Added utility method to lock scrolling. Released 2.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gotev committed Feb 19, 2019
1 parent d4e400e commit 2d43b52
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ In this way every item of the recycler view has its own set of files, resulting
* [Programmatically select items](#programmatically-select-items)
* [Replacing selection groups items and Master/Slave selection groups](#replacing-selection-groups-items-and-masterslave-selection-groups)
* [Leave Behind pattern](#leaveBehind)
* [Lock scrolling while inserting](#lockScroll)
* [Contributors](#contributors)

## <a name="setup"></a>Setup
In your gradle dependencies add:
```groovy
implementation 'net.gotev:recycleradapter:2.2.1'
implementation 'net.gotev:recycleradapter:2.2.2'
```

## <a name="basicTutorial"></a>Basic usage tutorial
Expand Down Expand Up @@ -507,6 +508,14 @@ This can be achieved combining `setSelectionGroupListener`, `replaceSelectionGro
## <a name="leaveBehind"></a>Leave Behind pattern example implementation
In the demo app provided with the library, you can also see how to implement the [leave behind material design pattern](https://material.io/guidelines/components/lists-controls.html#lists-controls-types-of-list-controls). All the changes involved into the implementation can be seen in [this commit](https://github.com/gotev/recycler-adapter/commit/fa240519025f98ba609395034f42e89d5bb777fd). This implementation has not been included into the base library deliberately, to avoid depending on external libraries just for a single kind of item implementation. You can easily import the needed code in your project from the demo app sources if you want to have leave behind implementation.

## <a name="lockScroll"></a>Lock scrolling while inserting
When dynamically loading many data at once in the RecyclerView, specially when we are inserting new items at the first position, the default behavior of the RecyclerView, which scrolls down automatically may not be what we want. To lock the scrolling while inserting new items, simply call:

```kotlin
recyclerAdapter.lockScrollingWhileInserting(layoutManager)
```
To get a better comprehension of this behavior, try commenting `lockScrollingWhileInserting` in [SyncActivity](https://github.com/gotev/recycler-adapter/blob/master/app/demo/src/main/java/net/gotev/recycleradapterdemo/SyncActivity.kt) and run the demo app again pressing the `shuffle` button to see the difference.

## <a name="contributors"></a>Contributors
Thanks to:
* [Kristiyan Petrov](https://github.com/kristiyanP) for the beta testing and code review
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,10 @@ class SyncActivity : AppCompatActivity() {

val linearLayoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)

recyclerAdapter = RecyclerAdapter()
recyclerAdapter.setEmptyItem(LabelItem(getString(R.string.empty_list)))

// prevent recyclerview from scrolling when adding many items
// https://github.com/airbnb/epoxy/issues/224#issuecomment-305991898
recyclerAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
linearLayoutManager.scrollToPosition(0)
}
}
})
recyclerAdapter = RecyclerAdapter().apply {
setEmptyItem(LabelItem(getString(R.string.empty_list)))
lockScrollingWhileInserting(linearLayoutManager)
}

recycler_view.apply {
layoutManager = linearLayoutManager
Expand Down
2 changes: 1 addition & 1 deletion manifest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ext {
library_licenses = ["Apache-2.0"]
library_licenses_url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
library_project_group = 'net.gotev'
library_version = '2.2.1'
library_version = '2.2.2'
version_code = 5
min_sdk = 18
target_sdk = 28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,4 +730,20 @@ class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapterViewHolder>(), Recyc

return this
}

/**
* Prevent RecyclerView from scrolling when adding many items
* Taken from: https://github.com/airbnb/epoxy/issues/224#issuecomment-305991898
*
* @param layoutManager RecyclerView's Layout Manager
*/
fun lockScrollingWhileInserting(layoutManager: RecyclerView.LayoutManager) {
registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
if (positionStart == 0) {
layoutManager.scrollToPosition(0)
}
}
})
}
}

0 comments on commit 2d43b52

Please sign in to comment.