diff --git a/README.md b/README.md index 0964719..24e976b 100644 --- a/README.md +++ b/README.md @@ -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) ## Setup In your gradle dependencies add: ```groovy -implementation 'net.gotev:recycleradapter:2.2.1' +implementation 'net.gotev:recycleradapter:2.2.2' ``` ## Basic usage tutorial @@ -507,6 +508,14 @@ This can be achieved combining `setSelectionGroupListener`, `replaceSelectionGro ## 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. +## 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. + ## Contributors Thanks to: * [Kristiyan Petrov](https://github.com/kristiyanP) for the beta testing and code review diff --git a/app/demo/src/main/java/net/gotev/recycleradapterdemo/SyncActivity.kt b/app/demo/src/main/java/net/gotev/recycleradapterdemo/SyncActivity.kt index 782b7d5..3127f56 100644 --- a/app/demo/src/main/java/net/gotev/recycleradapterdemo/SyncActivity.kt +++ b/app/demo/src/main/java/net/gotev/recycleradapterdemo/SyncActivity.kt @@ -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 diff --git a/manifest.gradle b/manifest.gradle index 00c61f0..e2c1a8e 100644 --- a/manifest.gradle +++ b/manifest.gradle @@ -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 diff --git a/recycleradapter/src/main/java/net/gotev/recycleradapter/RecyclerAdapter.kt b/recycleradapter/src/main/java/net/gotev/recycleradapter/RecyclerAdapter.kt index ff33e4e..a0ce850 100644 --- a/recycleradapter/src/main/java/net/gotev/recycleradapter/RecyclerAdapter.kt +++ b/recycleradapter/src/main/java/net/gotev/recycleradapter/RecyclerAdapter.kt @@ -730,4 +730,20 @@ class RecyclerAdapter : RecyclerView.Adapter(), 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) + } + } + }) + } }