forked from upstream-mirrors/quik
Create extension function to auto scroll to start of recyclerview
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of QKSMS.
|
||||||
|
*
|
||||||
|
* QKSMS is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* QKSMS is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package common.util.extensions
|
||||||
|
|
||||||
|
import android.support.v7.widget.LinearLayoutManager
|
||||||
|
import android.support.v7.widget.RecyclerView
|
||||||
|
|
||||||
|
fun RecyclerView.Adapter<*>.autoScrollToStart(recyclerView: RecyclerView) {
|
||||||
|
registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
||||||
|
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
||||||
|
val layoutManager = recyclerView.layoutManager as? LinearLayoutManager ?: return
|
||||||
|
|
||||||
|
if (layoutManager.stackFromEnd) {
|
||||||
|
if (positionStart > 0) {
|
||||||
|
notifyItemChanged(positionStart - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
val lastPosition = layoutManager.findLastCompletelyVisibleItemPosition()
|
||||||
|
if (positionStart >= getItemCount() - 1 && lastPosition == positionStart - 1) {
|
||||||
|
recyclerView.scrollToPosition(positionStart)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
val firstVisiblePosition = layoutManager.findFirstCompletelyVisibleItemPosition()
|
||||||
|
if (firstVisiblePosition == 0) {
|
||||||
|
recyclerView.scrollToPosition(positionStart)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -47,6 +47,7 @@ import kotlinx.android.synthetic.main.compose_activity.*
|
|||||||
import model.Contact
|
import model.Contact
|
||||||
import model.Message
|
import model.Message
|
||||||
import common.base.QkThemedActivity
|
import common.base.QkThemedActivity
|
||||||
|
import common.util.extensions.autoScrollToStart
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
||||||
@@ -93,20 +94,7 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
|||||||
|
|
||||||
val layoutManager = LinearLayoutManager(this).apply { stackFromEnd = true }
|
val layoutManager = LinearLayoutManager(this).apply { stackFromEnd = true }
|
||||||
|
|
||||||
messageAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
messageAdapter.autoScrollToStart(messageList)
|
||||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
|
||||||
if (positionStart > 0) {
|
|
||||||
messageAdapter.notifyItemChanged(positionStart - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're at the bottom, scroll down to show new messages
|
|
||||||
val lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition()
|
|
||||||
if (positionStart >= messageAdapter.itemCount - 1 && lastVisiblePosition == positionStart - 1) {
|
|
||||||
messageList.scrollToPosition(positionStart)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
messageAdapter.longClicks.subscribe { message ->
|
messageAdapter.longClicks.subscribe { message ->
|
||||||
AlertDialog.Builder(this)
|
AlertDialog.Builder(this)
|
||||||
.setItems(R.array.message_options, { _, row ->
|
.setItems(R.array.message_options, { _, row ->
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import com.uber.autodispose.kotlin.autoDisposable
|
|||||||
import common.MenuItemAdapter
|
import common.MenuItemAdapter
|
||||||
import common.Navigator
|
import common.Navigator
|
||||||
import common.base.QkThemedActivity
|
import common.base.QkThemedActivity
|
||||||
|
import common.util.extensions.autoScrollToStart
|
||||||
import common.util.extensions.dpToPx
|
import common.util.extensions.dpToPx
|
||||||
import common.util.extensions.setBackgroundTint
|
import common.util.extensions.setBackgroundTint
|
||||||
import common.util.extensions.setTint
|
import common.util.extensions.setTint
|
||||||
@@ -169,20 +170,8 @@ class MainActivity : QkThemedActivity<MainViewModel>(), MainView {
|
|||||||
.autoDisposable(scope())
|
.autoDisposable(scope())
|
||||||
.subscribe()
|
.subscribe()
|
||||||
|
|
||||||
|
conversationsAdapter.autoScrollToStart(recyclerView)
|
||||||
conversationsAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
conversationsAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
||||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
|
||||||
// If we're at the top, scroll up to show new conversations
|
|
||||||
val layoutManager = recyclerView.layoutManager as LinearLayoutManager
|
|
||||||
val firstVisiblePosition = layoutManager.findFirstCompletelyVisibleItemPosition()
|
|
||||||
if (firstVisiblePosition == 0) {
|
|
||||||
recyclerView.scrollToPosition(positionStart)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
|
|
||||||
onItemRangeInserted(positionStart, itemCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onChanged() {
|
override fun onChanged() {
|
||||||
if (conversationsAdapter.isAttachedToRecyclerView()) {
|
if (conversationsAdapter.isAttachedToRecyclerView()) {
|
||||||
empty.setVisible(conversationsAdapter.itemCount == 0, View.INVISIBLE)
|
empty.setVisible(conversationsAdapter.itemCount == 0, View.INVISIBLE)
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ package feature.qkreply
|
|||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.v7.widget.LinearLayoutManager
|
import android.support.v7.widget.LinearLayoutManager
|
||||||
import android.support.v7.widget.RecyclerView
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.view.Window
|
import android.view.Window
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
@@ -29,6 +28,7 @@ import com.moez.QKSMS.R
|
|||||||
import com.uber.autodispose.android.lifecycle.scope
|
import com.uber.autodispose.android.lifecycle.scope
|
||||||
import com.uber.autodispose.kotlin.autoDisposable
|
import com.uber.autodispose.kotlin.autoDisposable
|
||||||
import common.base.QkThemedActivity
|
import common.base.QkThemedActivity
|
||||||
|
import common.util.extensions.autoScrollToStart
|
||||||
import common.util.extensions.setBackgroundTint
|
import common.util.extensions.setBackgroundTint
|
||||||
import feature.compose.MessagesAdapter
|
import feature.compose.MessagesAdapter
|
||||||
import injection.appComponent
|
import injection.appComponent
|
||||||
@@ -88,23 +88,9 @@ class QkReplyActivity : QkThemedActivity<QkReplyViewModel>(), QkReplyView {
|
|||||||
|
|
||||||
toolbar.clipToOutline = true
|
toolbar.clipToOutline = true
|
||||||
|
|
||||||
val layoutManager = LinearLayoutManager(this).apply { stackFromEnd = true }
|
adapter.autoScrollToStart(messages)
|
||||||
|
|
||||||
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
|
messages.layoutManager = LinearLayoutManager(this).apply { stackFromEnd = true }
|
||||||
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
|
|
||||||
if (positionStart > 0) {
|
|
||||||
adapter.notifyItemChanged(positionStart - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're at the bottom, scroll down to show new messages
|
|
||||||
val lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition()
|
|
||||||
if (positionStart >= adapter.itemCount - 1 && lastVisiblePosition == positionStart - 1) {
|
|
||||||
messages.scrollToPosition(positionStart)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
messages.layoutManager = layoutManager
|
|
||||||
messages.adapter = adapter
|
messages.adapter = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user