android - 我想在每次选择底部导航项时初始化片段
问题描述
下面的代码是单击底部导航时运行的函数。
fun replaceFragment(fragment: Fragment)// 프래그먼트 교체 함수
{
Log.d("MainAcitvity", "fragment replace + $fragment")
//Fragment Start Remove
supportFragmentManager.beginTransaction().apply {
remove(fragment)
commit()
}
//Fragment Start Replace
supportFragmentManager.beginTransaction().apply {
replace(R.id.fragmentContainer, fragment)
commit()
Log.d("MainAcitvity", "fragment replace start + $fragment")
}
}//end of replaceFragment
bottomNavigationView.setOnNavigationItemSelectedListener {
when (it.itemId) {
R.id.fragment1 -> {
adjustGravity(bottomNavigationView)
}
R.id.fragment2 -> {
replaceFragment(Fragment2)
}
R.id.fragment3 -> {
replaceFragment(Fragment3)
}
R.id.fragment4 -> {
replaceFragment(Fragment4)
}
}
true
}//end of setOnNavigationItemSelectedListener
但这不起作用。
我正在尝试这样做,因为我正在使用 CoordinatorLayout 向上滚动 AppLayout 函数。
当我向下滚动并转到另一个片段时,它没有初始化,如上图所示。
在 NestedScrollView 中,每个 Fragment 的 On Pause() 都会显示在我尝试过的 NestedScrollView.scrollTo(0,0) 中,但它不起作用。
我也尝试过初始化 OnCreateView(),但它也不起作用。
我认为片段在 MainActivity 中工作,生命周期是一个问题。
下面是其中一个片段的示例。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/CoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/fragment3ToolbarFrame"
android:layout_width="match_parent"
android:layout_height="290dp"
android:background="@android:color/transparent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="50dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/fragment3ImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/talk_toolbar_color" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/fragment3_title_margin_start"
android:layout_marginTop="@dimen/fragment3_title_margin_top"
android:fontFamily="@font/notosanscjkkr_medium"
android:includeFontPadding="false"
android:text="@string/fragment3_tag_title"
android:textColor="@color/white"
android:textSize="@dimen/fragment3_title_size" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/fragment3_subtitle_margin_start"
android:layout_marginTop="@dimen/fragment3_subtitle1_margin_top"
android:fontFamily="@font/notosanscjkkr_regular"
android:includeFontPadding="false"
android:text="@string/fragment3_tag_subtitle1"
android:textColor="@color/white"
android:textSize="@dimen/fragment3_subtitle_size" />
<TextView
android:id="@+id/fragment3_tagText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/fragment3_subtitle_margin_start"
android:layout_marginTop="175dp"
android:fontFamily="@font/notosanscjkkr_regular"
android:includeFontPadding="false"
android:text="@string/fragment3_tag_subtitle2"
android:textColor="@color/white"
android:textSize="12dp" />
<TextView
android:id="@+id/fragment3AppbarTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="225dp"
android:fontFamily="@font/notosanscjkkr_medium"
android:includeFontPadding="false"
android:text="@string/fragment3_tag_title"
android:textColor="@color/white"
android:textSize="50sp"
android:visibility="gone" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/fragment3NestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/fragment3RecyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addFloatingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:backgroundTint="#447BE3"
android:src="@drawable/ic_baseline_create_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:tint="@null" />
</androidx.constraintlayout.widget.ConstraintLayout>
我想以任何方式初始化片段。有什么办法吗?
解决方案
如果您想在第二次单击 BottomNavigationView 时再次初始化 Fragment,请使用setOnNavigationItemReSelectedListener并用您需要的新片段替换您的容器。
推荐阅读
- java - 如何从 Spinner 获得吐司
- reactjs - 在 onChange 函数中将一页链接到另一页
- jquery - React 仅在键入时保存输入值状态,但在由 API 数据填充时不保存
- keyboard - 如何更改键盘快捷键以在 Chrome OS 上切换输入语言?
- google-chrome-devtools - 将 sourceURL 和 sourceMappingURL 与相对路径一起使用
- html - 将鼠标悬停在上面的图像上时,如何更改滑块的不透明度?
- xml - 如何修复节点上附加的空白换行符,在更改 XML 节点上的数据时我没有触及?
- xml - USQL - 如何使用 xml 提取器从 xml 文件中提取属性值
- python - tox+pytest 仅在 travis 上给出代码 -11 的调用错误
- sql-server - 在 ColdFusion 服务器上存储列主密钥证书的位置以用于 SQL Server Always Encrypted 数据库列