分组布局问题 #170
This commit is contained in:
@@ -111,11 +111,6 @@ open class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
|
||||
binding.viewPagerMain.currentItem = 0
|
||||
return
|
||||
}
|
||||
|
||||
val fragment = fragmentMap[getFragmentId(0)] as? BookshelfFragment2
|
||||
if (fragment?.back() == true) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
onBackPressedDispatcher.addCallback(this, backCallback)
|
||||
@@ -434,8 +429,8 @@ open class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
|
||||
}
|
||||
|
||||
efab.setOnClickListener {
|
||||
startActivity<SearchActivity>()
|
||||
}
|
||||
startActivity<SearchActivity>()
|
||||
}
|
||||
|
||||
button.setOnClickListener {
|
||||
efab.let { it1 ->
|
||||
@@ -509,8 +504,7 @@ open class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
|
||||
}
|
||||
|
||||
private fun updateBackCallbackState() {
|
||||
val fragment = fragmentMap[getFragmentId(0)] as? BookshelfFragment2
|
||||
backCallback.isEnabled = (pagePosition != 0) || (fragment?.canHandleBack() == true)
|
||||
backCallback.isEnabled = (pagePosition != 0)
|
||||
}
|
||||
|
||||
private inner class TabFragmentPageAdapter(
|
||||
@@ -522,13 +516,7 @@ open class MainActivity : VMBaseActivity<ActivityMainBinding, MainViewModel>(),
|
||||
override fun createFragment(position: Int): Fragment {
|
||||
val fragment = when (getFragmentId(position)) {
|
||||
idBookshelf1 -> BookshelfFragment1(position)
|
||||
idBookshelf2 -> {
|
||||
val bookshelfFragment = BookshelfFragment2(position)
|
||||
bookshelfFragment.setGroupIdChangeListener {
|
||||
updateBackCallbackState()
|
||||
}
|
||||
bookshelfFragment
|
||||
}
|
||||
idBookshelf2 -> BookshelfFragment2(position)
|
||||
idBookshelf3 -> BookshelfFragment3(position)
|
||||
idExplore -> ExploreFragment(position)
|
||||
idRss -> RssFragment(position)
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.app.ActivityOptionsCompat
|
||||
import androidx.core.view.isGone
|
||||
@@ -78,10 +79,6 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
|
||||
|
||||
private var groupIdChangeListener: OnGroupIdChangeListener? = null
|
||||
|
||||
fun setGroupIdChangeListener(listener: OnGroupIdChangeListener) {
|
||||
this.groupIdChangeListener = listener
|
||||
}
|
||||
|
||||
private val booksAdapter: BaseBooksAdapter<*> by lazy {
|
||||
when (bookshelfLayoutMode) {
|
||||
0 -> {
|
||||
@@ -113,12 +110,27 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
|
||||
override var books: List<Book> = emptyList()
|
||||
private var enableRefresh = true
|
||||
|
||||
private lateinit var backCallback: OnBackPressedCallback
|
||||
|
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
|
||||
setSupportToolbar(binding.topBar)
|
||||
initRecyclerView()
|
||||
initBookGroupData()
|
||||
initAllBooksData()
|
||||
initBooksData()
|
||||
backCallback = object : OnBackPressedCallback(false) { // 初始禁用
|
||||
override fun handleOnBackPressed() {
|
||||
if (back()) {
|
||||
updateBackCallbackState()
|
||||
}
|
||||
}
|
||||
}
|
||||
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, backCallback)
|
||||
}
|
||||
|
||||
private fun updateBackCallbackState() {
|
||||
//每次进出书架时,判断是否启用回调
|
||||
backCallback.isEnabled = groupId != BookGroup.IdRoot
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@@ -272,9 +284,11 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
|
||||
groupId = BookGroup.Companion.IdRoot
|
||||
initBooksData()
|
||||
groupIdChangeListener?.onGroupIdChanged()
|
||||
return true
|
||||
updateBackCallbackState()
|
||||
return false
|
||||
}
|
||||
return false
|
||||
return true
|
||||
//需要提前设置,因为在返回时触发
|
||||
}
|
||||
|
||||
override fun onQueryTextSubmit(query: String?): Boolean {
|
||||
@@ -324,6 +338,7 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
|
||||
groupId = item.groupId
|
||||
initBooksData()
|
||||
groupIdChangeListener?.onGroupIdChanged()
|
||||
updateBackCallbackState()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -365,10 +380,6 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
|
||||
}
|
||||
}
|
||||
|
||||
fun canHandleBack(): Boolean {
|
||||
return groupId != BookGroup.Companion.IdRoot
|
||||
}
|
||||
|
||||
override fun getItems(): List<Any> {
|
||||
return if (groupId == BookGroup.Companion.IdRoot) {
|
||||
bookGroups + books
|
||||
|
||||
Reference in New Issue
Block a user