gpt4 book ai didi

android - BottomNavigationView 后退按钮行为应该像 Youtube 应用程序一样工作但崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:59 25 4
gpt4 key购买 nike

重现步骤:

  1. 开始一个新的 Android 项目,选择“BottomNavigationView”: enter image description here
  2. 将 MainActivity 替换为:

    class MainActivity : AppCompatActivity() {

    private var fragmentIds = ArrayList<Int>()

    val fragmentA: FragmentA = FragmentA()
    private val fragmentB = FragmentB()
    private val fragmentC = FragmentC()

    private fun getFragment(fragmentId: Int): Fragment {
    when (fragmentId) {
    R.id.navigation_home -> {
    return fragmentA
    }
    R.id.navigation_dashboard -> {
    return fragmentB
    }
    R.id.navigation_notifications -> {
    return fragmentC
    }
    }
    return fragmentA
    }

    private fun updateView(fragmentId: Int) {
    var exists = false
    fragmentIds
    .filter { it == fragmentId }
    .forEach { exists = true }

    if (exists) {
    fragmentIds.remove(fragmentId)
    showTabWithoutAddingToBackStack(getFragment(fragmentId))
    } else {
    fragmentIds.add(fragmentId)
    showTab(getFragment(fragmentId))
    }
    }

    private val onNavigationItemClicked = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
    R.id.navigation_home -> {
    updateView(R.id.navigation_home)
    return@OnNavigationItemSelectedListener true
    }
    R.id.navigation_dashboard -> {
    updateView(R.id.navigation_dashboard)
    return@OnNavigationItemSelectedListener true
    }
    R.id.navigation_notifications -> {
    updateView(R.id.navigation_notifications)
    return@OnNavigationItemSelectedListener true
    }
    }
    false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    showTabWithoutAddingToBackStack(fragmentA)

    navigation.setOnNavigationItemSelectedListener(onNavigationItemClicked)

    }

    private fun showTab(fragment: Fragment) {
    supportFragmentManager
    .beginTransaction()
    .replace(R.id.main_container, fragment, fragment::class.java.simpleName)
    .addToBackStack(fragment::class.java.simpleName)
    .commit()
    }

    fun showTabWithoutAddingToBackStack(fragment: Fragment) {
    supportFragmentManager
    .beginTransaction()
    .replace(R.id.main_container, fragment, fragment::class.java.simpleName)
    .commit()
    }

    fun setBottomTab(id: Int) {
    navigation.setOnNavigationItemSelectedListener(null)
    navigation.selectedItemId = id
    // currentTab = id
    navigation.setOnNavigationItemSelectedListener(onNavigationItemClicked)
    }
    }
  3. 创建 3 个新类,FragmentA、FragmentB 和 FragmentC:

    class FragmentA : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    setHasOptionsMenu(true)
    return inflater.inflate(R.layout.fragment_a, container, false)
    }

    override fun onResume() {
    super.onResume()
    val act = activity as MainActivity
    act.setBottomTab(R.id.navigation_home)
    }
    }

使用这个 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment A" />
</LinearLayout>
  1. 启动应用
  2. 按“仪表板”- 显示 fragment B
  3. 按“通知”- 显示 fragment C
  4. 按“仪表板”- 显示 fragment B
  5. 按“主页”- 显示 fragment A
  6. 按“后退”按钮 - 显示 fragment B
  7. 按返回按钮 - 应显示 fragment C - 应用程序崩溃
  8. 按“后退”按钮 - 应显示 fragment A - 应用程序崩溃
  9. 按返回按钮 - 应用程序关闭。 - 应用程序崩溃

Here is a video that demonstrates above steps

堆栈跟踪:

12-06 12:58:35.899 25903-25903/com.example.jimclermonts.bottomnavigationview E/InputEventSender: Exception dispatching finished signal.
12-06 12:58:35.900 25903-25903/com.example.jimclermonts.bottomnavigationview E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
12-06 12:58:35.912 25903-25903/com.example.jimclermonts.bottomnavigationview E/MessageQueue-JNI: java.lang.**IllegalStateException: Fragment already added**: FragmentB{3aac1d9 #1 id=0x7f080059 FragmentB}
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1882)
at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:825)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2577)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:851)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:794)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:174)

最佳答案

我已经使用 Bottombar 库实现了这个概念。我已经上传到GitHub。如果有任何问题,请在此处检查和评论。

https://github.com/itvignes09/youtube-like-bttom-menu

示例输出

enter image description here

关于android - BottomNavigationView 后退按钮行为应该像 Youtube 应用程序一样工作但崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47435967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com