在片段中隐藏底部导航视图
Hide Bottom Navigation View in fragment
我想在某些片段中隐藏bottomNavigationView。
我试过下面的代码,但是有闪烁效果。 (bottomNavigationView 在 nextFragment 变得可见之前隐藏。
val navController = this.findNavController(R.id.nav_host_home)
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.searchArticlesFragment -> bnvMain.visibility = View.GONE
R.id.articleFragment -> bnvMain.visibility = View.GONE
else -> bnvMain.visibility = View.VISIBLE
}
}
我也试过另一个密码。但它会调整片段的大小。并在目标片段中给出 OutOfMemoryException。
supportFragmentManager.registerFragmentLifecycleCallbacks(object :
FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentViewCreated(
fm: FragmentManager,
f: Fragment,
v: View,
savedInstanceState: Bundle?
) {
when (f) {
is SearchArticlesFragment -> bnvMain.visibility = View.GONE
is ArticleDetailsFragment -> bnvMain.visibility = View.GONE
else -> bnvMain.visibility = View.VISIBLE
}
}
}, true)
请帮助我如何以正确和最好的方式隐藏 bottomNavigationView?这是我可以隐藏 bottomNavigationView 的唯一方法吗? youtube 和 Instagram 如何实现这种行为?
如果您的代码遵循单一 activity 设计模式,那么以下解决方案适合您。
- 在父 activity 到 hide/show bottomNavigationView.
创建一个方法
- 创建一个 BaseFragment class(通过扩展此 BaseFragment Class 创建您的片段)
- 在 BaseFragment 中创建一个变量来保存 bottomNavigationViewVisibility (hide/show)
- 在 BaseFragment 的 onActivityCreated 方法中,获取 activity 引用并通过调用我们在 STEP1 中创建的方法设置 bottomNavigationViewVisibility。
- 在您创建的每个片段中,只需设置 bottomNavigationViewVisibility 变量。
示例:
在 parentAcitivty 布局中,文件添加 bottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="labeled"
app:menu="@menu/main_nav" />
第 1 步:在父级 activity 中创建一个方法来更改可见性。
fun setBottomNavigationVisibility(visibility: Int) {
// get the reference of the bottomNavigationView and set the visibility.
activityMainBinding.mainBottomNavigationView.visibility = visibility
}
第 2、3、4 步:
abstract class BaseFragment : Fragment() {
protected open var bottomNavigationViewVisibility = View.VISIBLE
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// get the reference of the parent activity and call the setBottomNavigationVisibility method.
if (activity is MainActivity) {
var mainActivity = activity as MainActivity
mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
}
}
override fun onResume() {
super.onResume()
if (activity is MainActivity) {
mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
}
}
}
第 5 步:
class SampleFragment1 : BaseFragment() {
// set the visibility here, it takes care of setting the bottomNavigationView.
override var navigationVisibility = View.VISIBLE
// override var navigationVisibility = View.GONE
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sampleFragment1, container, false)
}
}
我想在某些片段中隐藏bottomNavigationView。
我试过下面的代码,但是有闪烁效果。 (bottomNavigationView 在 nextFragment 变得可见之前隐藏。
val navController = this.findNavController(R.id.nav_host_home)
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.searchArticlesFragment -> bnvMain.visibility = View.GONE
R.id.articleFragment -> bnvMain.visibility = View.GONE
else -> bnvMain.visibility = View.VISIBLE
}
}
我也试过另一个密码。但它会调整片段的大小。并在目标片段中给出 OutOfMemoryException。
supportFragmentManager.registerFragmentLifecycleCallbacks(object :
FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentViewCreated(
fm: FragmentManager,
f: Fragment,
v: View,
savedInstanceState: Bundle?
) {
when (f) {
is SearchArticlesFragment -> bnvMain.visibility = View.GONE
is ArticleDetailsFragment -> bnvMain.visibility = View.GONE
else -> bnvMain.visibility = View.VISIBLE
}
}
}, true)
请帮助我如何以正确和最好的方式隐藏 bottomNavigationView?这是我可以隐藏 bottomNavigationView 的唯一方法吗? youtube 和 Instagram 如何实现这种行为?
如果您的代码遵循单一 activity 设计模式,那么以下解决方案适合您。
- 在父 activity 到 hide/show bottomNavigationView. 创建一个方法
- 创建一个 BaseFragment class(通过扩展此 BaseFragment Class 创建您的片段)
- 在 BaseFragment 中创建一个变量来保存 bottomNavigationViewVisibility (hide/show)
- 在 BaseFragment 的 onActivityCreated 方法中,获取 activity 引用并通过调用我们在 STEP1 中创建的方法设置 bottomNavigationViewVisibility。
- 在您创建的每个片段中,只需设置 bottomNavigationViewVisibility 变量。
示例: 在 parentAcitivty 布局中,文件添加 bottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="labeled"
app:menu="@menu/main_nav" />
第 1 步:在父级 activity 中创建一个方法来更改可见性。
fun setBottomNavigationVisibility(visibility: Int) {
// get the reference of the bottomNavigationView and set the visibility.
activityMainBinding.mainBottomNavigationView.visibility = visibility
}
第 2、3、4 步:
abstract class BaseFragment : Fragment() {
protected open var bottomNavigationViewVisibility = View.VISIBLE
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// get the reference of the parent activity and call the setBottomNavigationVisibility method.
if (activity is MainActivity) {
var mainActivity = activity as MainActivity
mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
}
}
override fun onResume() {
super.onResume()
if (activity is MainActivity) {
mainActivity.setBottomNavigationVisibility(bottomNavigationViewVisibility)
}
}
}
第 5 步:
class SampleFragment1 : BaseFragment() {
// set the visibility here, it takes care of setting the bottomNavigationView.
override var navigationVisibility = View.VISIBLE
// override var navigationVisibility = View.GONE
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sampleFragment1, container, false)
}
}