Merge pull request #2594 from Bnyro/master

Scroll to top on reselecting navigation bar items
This commit is contained in:
Bnyro 2023-01-05 17:12:22 +01:00 committed by GitHub
commit a44d8ad25f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,17 +11,22 @@ import android.os.Looper
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.view.WindowManager
import android.widget.ScrollView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.SearchView
import androidx.core.os.bundleOf
import androidx.core.view.children
import androidx.core.widget.NestedScrollView
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
@ -112,6 +117,13 @@ class MainActivity : BaseActivity() {
binding.bottomNav.setOnItemReselectedListener {
if (it.itemId != navController.currentDestination?.id) {
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 +185,31 @@ class MainActivity : BaseActivity() {
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
*/