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