【发布时间】:2017-11-22 13:28:44
【问题描述】:
重现步骤:
- 启动一个新的Android项目,选择“
BottomNavigationView”: -
将 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 个新类,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>
- 启动应用程序
- 按“仪表板”- 显示片段 B
- 按“通知”- 显示片段 C
- 按“仪表板”- 显示片段 B
- 按“主页”- 显示片段 A
- 按返回按钮 - 显示片段 B
- 按返回按钮 - 应显示片段 C - 应用程序崩溃
- 按返回按钮 - 应显示片段 A - 应用程序崩溃
- 按返回按钮 - 应用程序关闭。 - 应用程序崩溃
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)
【问题讨论】:
-
“我想要 XXX 行为”好的,您当前的实现有什么问题?
-
@azizbekian 我希望它与 Youtube 应用程序相同。但是现在后退按钮的行为不一样了。
-
@JimClermonts “不一样”到底是什么意思?
-
你的意思是你想喜欢这个:github.com/pedrovgs/DraggablePanel
-
@saif 我说的是底部导航,标签。它必须与 youtube 应用中的相同
标签: android bottomnavigationview