【发布时间】:2018-02-21 18:55:11
【问题描述】:
我有 3 个不同的片段。这是一个奇怪的问题,但我想将交易限制在一些片段中。例如:如果我在第三个片段中单击返回,我会回到第一个。如果我在第一个中单击返回,它将返回到 MainActivity。但所有三个片段都是来自另一个名为 SearchActivity 的 Activity。这种方式有两个问题。 第一:我的状态栏看起来有点……看看吧: look at this status bar
我的第二个问题:当我返回片段时(从 MainActivity 开始 SearchActivity,底部导航栏和 currentItem(0),然后我按下返回按钮,我认为它会进入 SearchActivity,但我需要回到 MainActivity。好吧,要回到 MainActivity,我需要按两次,因为第一次按它会进入 SearchActivity)会发生这种情况: imgur gif
我的活动:
lateinit var toolbar: ActionBar
private fun openFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_1 -> {
toolbar.title = resources.getString(R.string.title_1)
val firstFragment = FirstView.newInstance()
openFragment(firstFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_2-> {
toolbar.title = resources.getString(R.string.title_2)
val secFragment = SecView.newInstance()
openFragment(secFragment)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_3 -> {
toolbar.title = resources.getString(R.string.title_3)
val thirdFragment = ThirdView.newInstance()
openFragment(thirdFragment)
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
toolbar = supportActionBar!!
val bottomNavigation: BottomNavigationView = findViewById(R.id.navigation)
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
bottomNavigation.selectedItemId = R.id.navigation_1
}
还有活动xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="s.com.s.SearchActivity">
<!--<FrameLayout-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"/>-->
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="@android:color/white"
app:itemBackground="@android:color/white"
app:itemIconTint="@drawable/tab_state"
app:itemTextColor="@drawable/tab_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
片段示例:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_first, container, false)
val rv = view.findViewById<RecyclerView>(R.id.firstView)
rv.layoutManager = LinearLayoutManager(context, LinearLayout.VERTICAL, false)
val firsts = ArrayList<Hotel>()
firsts.add(Hotel("Paul", "Mr", R.drawable.abc_list_longpressed_holo))
firsts.add(Hotel("Jane", "Miss", R.drawable.abc_ratingbar_material))
firsts.add(Hotel("John", "Dr", R.drawable.abc_btn_check_to_on_mtrl_000))
firsts.add(Hotel("Amy", "Mrs", R.drawable.abc_ic_menu_paste_mtrl_am_alpha))
var adapter = CustomAdapter(firsts)
rv.adapter = adapter
return view
}
companion object {
fun newInstance(): FirstView = FirstView()
}
还有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"
android:background="@color/colorAccent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="?attr/actionBarSize"
android:id="@+id/firstView"/>
很高兴得到帮助,我一个星期都解决不了。我希望我已经解释清楚了,因为我什至不知道如何解释。
【问题讨论】:
-
您需要跟踪当前片段并覆盖 onBackPressed。然后你会在 Activity 的 onBackPressed 中添加一些逻辑,根据你所在的 Fragment 来决定要做什么。
-
你有什么例子吗?我只是不明白如何处理片段(只听说过活动),所以......希望你能提供帮助......
标签: java android user-interface android-fragments kotlin