mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Scroll to top on reselecting navigation bar items
This commit is contained in:
parent
6832e97b4b
commit
6b46db0e96
@ -8,20 +8,26 @@ import android.os.Build
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import android.util.Log
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
import android.view.WindowInsets
|
import android.view.WindowInsets
|
||||||
import android.view.WindowInsetsController
|
import android.view.WindowInsetsController
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
|
import android.widget.ScrollView
|
||||||
import androidx.activity.OnBackPressedCallback
|
import androidx.activity.OnBackPressedCallback
|
||||||
import androidx.appcompat.widget.SearchView
|
import androidx.appcompat.widget.SearchView
|
||||||
import androidx.core.os.bundleOf
|
import androidx.core.os.bundleOf
|
||||||
import androidx.core.view.children
|
import androidx.core.view.children
|
||||||
|
import androidx.core.widget.NestedScrollView
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
import androidx.navigation.findNavController
|
import androidx.navigation.findNavController
|
||||||
|
import androidx.navigation.fragment.NavHostFragment
|
||||||
import androidx.navigation.ui.setupWithNavController
|
import androidx.navigation.ui.setupWithNavController
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.constants.IntentData
|
import com.github.libretube.constants.IntentData
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
@ -112,6 +118,13 @@ class MainActivity : BaseActivity() {
|
|||||||
binding.bottomNav.setOnItemReselectedListener {
|
binding.bottomNav.setOnItemReselectedListener {
|
||||||
if (it.itemId != navController.currentDestination?.id) {
|
if (it.itemId != navController.currentDestination?.id) {
|
||||||
navigateToBottomSelectedItem(it)
|
navigateToBottomSelectedItem(it)
|
||||||
|
} else {
|
||||||
|
// get the host fragment containing the current fragment
|
||||||
|
val navHostFragment =
|
||||||
|
supportFragmentManager.findFragmentById(R.id.fragment) as NavHostFragment?
|
||||||
|
// get the current fragment
|
||||||
|
val fragment = navHostFragment?.childFragmentManager?.fragments?.get(0)
|
||||||
|
tryScrollToTop(fragment?.requireView() as? ViewGroup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,6 +186,31 @@ class MainActivity : BaseActivity() {
|
|||||||
loadIntentData()
|
loadIntentData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to find a scroll or recycler view and scroll it back to the top
|
||||||
|
*/
|
||||||
|
private fun tryScrollToTop(viewGroup: ViewGroup?) {
|
||||||
|
(viewGroup as? ScrollView)?.scrollTo(0, 0)
|
||||||
|
|
||||||
|
if (viewGroup == null || viewGroup.childCount == 0) return
|
||||||
|
|
||||||
|
viewGroup.children.forEach {
|
||||||
|
(it as? ScrollView)?.let {
|
||||||
|
it.smoothScrollTo(0, 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
(it as? NestedScrollView)?.let {
|
||||||
|
it.smoothScrollTo(0, 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
(it as? RecyclerView)?.let {
|
||||||
|
it.smoothScrollToPosition(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tryScrollToTop(it as? ViewGroup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rotate according to the preference
|
* Rotate according to the preference
|
||||||
*/
|
*/
|
||||||
@ -304,8 +342,8 @@ class MainActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
// Handover back press to `BackPressedDispatcher`
|
// Handover back press to `BackPressedDispatcher`
|
||||||
else if (binding.bottomNav.menu.children.none {
|
else if (binding.bottomNav.menu.children.none {
|
||||||
it.itemId == navController.currentDestination?.id
|
it.itemId == navController.currentDestination?.id
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
this@MainActivity.onBackPressedDispatcher.onBackPressed()
|
this@MainActivity.onBackPressedDispatcher.onBackPressed()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user