如何根据在 Kotlin 中选择的片段显示/隐藏 FloatingActionButton?
Posted
技术标签:
【中文标题】如何根据在 Kotlin 中选择的片段显示/隐藏 FloatingActionButton?【英文标题】:How to show/hide FloatingActionButton based on the fragment selected in Kotlin? 【发布时间】:2021-12-22 18:23:31 【问题描述】:我有一个底部BottomNavigationView
、FloatingActionButton
和四个fragment
s 的活动。我只想在“fragment1”和“fragment2”上显示FloatingActionButton
,并在选择“fragment3”或“fragment4”时隐藏它。启动应用程序时,“fragment1”与FloatingActionButton
一起显示,当我导航到“fragment3”时,我设法隐藏了它。但问题是当我从“fragment3”或“fragment4”导航回“fragment1”时,FloatingActionButton
仍然隐藏。
以下是我的“同性恋”的987654328 @
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_
android:layout_>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_appbar"
android:layout_
android:layout_
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:menu="@menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_one"
android:layout_
android:layout_
android:src="@drawable/ic_hide_category"
app:layout_anchor="@id/bottom_appbar"/>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_
android:layout_
android:layout_marginBottom="50dp"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation"
app:layout_anchor="@id/bottom_appbar"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
以下是我在“HomeActivity”中的代码,我从fragment
s 的onCreateView
调用它来隐藏它。
fun hideFabOne()
binding.fabOne.hide()
然后我在“HomeActivity”中添加以下代码并从fragment
s 调用它以显示FAB,应用程序在启动时崩溃。
fun showFabOne()
binding.fabOne.show()
编辑:
java.lang.RuntimeException: Unable to start activity ComponentInfocom.abc.xyz/com.abc.xyz.ui.activities.HomeActivity: android.view.InflateException: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3835)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: android.view.InflateException: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #112 in com.abc.xyz:layout/activity_home: Error inflating class fragment
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized
at com.abc.xyz.ui.activities.HomeActivity.showFabOne(HomeActivity.kt:110)
at com.abc.xyz.ui.fragments.HomeFragment.onCreateView(HomeFragment.kt:102)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.ensureInflatedView(FragmentStateManager.java:392)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:281)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:140)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.abc.xyz.databinding.ActivityHomeBinding.inflate(ActivityHomeBinding.java:59)
at com.abc.xyz.databinding.ActivityHomeBinding.inflate(ActivityHomeBinding.java:53)
at com.abc.xyz.ui.activities.HomeActivity.onCreate(HomeActivity.kt:38)
2021-11-10 11:23:40.557 13300-13300/com.abc.xyz E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:8207)
at android.app.Activity.performCreate(Activity.java:8191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4011)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2325)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8633)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
【问题讨论】:
***.com/help/how-to-ask - “应用程序在启动时崩溃”没有堆栈跟踪或代码告诉我们什么,除了疯狂的猜测之外什么也得不到。发布一些代码和StackTrace。 span> @dominicoderLogcat
已添加,请立即查看。
引起:kotlin.UninitializedPropertyAccessException:lateinit属性绑定未初始化
当我从'fragment1'调用showFabOne()
时发生错误
好的。看来你有答案了。但是下次:从您发布的日志中,您可以看到问题发生在 HomeActivity
第 110 行 - 所以您想发布该代码。回顾一下:您的应用程序崩溃 -> 检查日志 cat -> 检查该行的代码 -> 调试您的应用程序 -> 如果您遇到问题,请使用该信息发布到 SO。
【参考方案1】:
由于您使用的是app:navGraph
,因此请使用addOnDestinationChangedListener
来控制FloatingActionButton
的可见性。
在HomeActivity
里面做这个
navController.addOnDestinationChangedListener _, destination, _ ->
if(destination.id == /*FRAGMENT ID*/)
// hide
else
// show
【讨论】:
【参考方案2】:我不知道你的代码,我用一个演示来尝试重现崩溃和你想要的功能。
我重用了代码并将您的 XML 添加到我的 XML 文件中。
是什么原因
我在不同的方法中添加了一些日志来检查调用顺序以查找崩溃原因。
我的解决方案:将代码移到Fragment中的onResume方法中
这是我的代码MainActivity
class MainActivity : AppCompatActivity()
private lateinit var binding: ActivityMainBinding
lateinit var fab: FloatingActionButton
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
Log.d("MainActivity", "TANCOLO===> onCreate 000 ")
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navView: BottomNavigationView = binding.navView
fab = findViewById(R.id.fab_one)
Log.d("MainActivity", "TANCOLO===> onCreate 111, fab = $fab ")
val navController = findNavController(R.id.nav_host_fragment_activity_main)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
HomeFragment
class HomeFragment : Fragment()
private lateinit var homeViewModel: HomeViewModel
private var _binding: FragmentHomeBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
Log.d("HomeFragment", "TANCOLO===> onCreateView() ")
homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
val textView: TextView = binding.textHome
homeViewModel.text.observe(viewLifecycleOwner, Observer
textView.text = it
)
return root
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
Log.d("HomeFragment", "TANCOLO===> onViewCreated ")
//get the FAB and show it.
// (requireActivity() as MainActivity).fab.apply
// Log.d("HomeFragment", "TANCOLO===> onViewCreated, fab = $this ")
// visibility = View.VISIBLE
//
override fun onResume()
super.onResume()
Log.d("HomeFragment", "TANCOLO===> onResume ")
//get the FAB and show it.
(requireActivity() as MainActivity).fab.apply
Log.d("HomeFragment", "TANCOLO===> onResume, fab = $this ")
visibility = View.VISIBLE // or View.GONE
override fun onDestroyView()
super.onDestroyView()
_binding = null
【讨论】:
以上是关于如何根据在 Kotlin 中选择的片段显示/隐藏 FloatingActionButton?的主要内容,如果未能解决你的问题,请参考以下文章