单击 Fragment 在 Activity 内完成
Clicking on Fragment goes through in Activity
我的 MainActivity
中有一个按钮可以打开 FragmentA
。 FragmentA
覆盖了整个屏幕,但我仍然看到 MainActivity
的按钮,我仍然可以点击它。
我试过在我的片段布局中使用 clickable
,但它不起作用
MainActivty
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val fragmentManager = this@MainActivity.supportFragmentManager
fragmentManager.beginTransaction()
.add(R.id.fragment_container, AFragment())
.addToBackStack(null)
.commit()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/fragment_container">
<Button
android:text="Button Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
fragment_a.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:clickable="true"
android:focusable="true">
android:background="@color/colorPrimary"
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A"/>
</LinearLayout>
发生这种情况是因为您将 Button
放在了 ConstraintLayout
中,您将其用作片段的容器。
当您 add
像您正在做的那样将一个片段放入容器中时,它只是以与 View
相同的方式添加它。
因此,如果您将一个 Fragment 添加到 ConstraintLayout
中,而该 ConstraintLayout
已经拥有 Button
作为 child,则该 Fragment 将显示在 Button
旁边,因为ConstraintLayout
允许重叠视图。
这也是为什么,如果您的容器是 LinearLayout
,那么添加片段会将片段放在 Button
下面。
因此,考虑到这一点,解决方案是将它们当作视图来处理。
如果您将一个视图添加到布局中,并且有另一个视图重叠,您将如何摆脱它?
最常见的解决方案是在添加 Fragment 时将 Button 的可见性设置为 INVISIBLE 或 GONE。
另一种解决方案可能是提高片段的高度,因此它现在比您的 Button
高。
当然,您也可以将按钮从Container 中移除,并将其也放置在Fragment 中。
这样,您可以在 FragmentManager
中使用 replace()
方法将包含您的 Button
的片段替换为您要显示的 Fragment
。
我的 MainActivity
中有一个按钮可以打开 FragmentA
。 FragmentA
覆盖了整个屏幕,但我仍然看到 MainActivity
的按钮,我仍然可以点击它。
我试过在我的片段布局中使用 clickable
,但它不起作用
MainActivty
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val fragmentManager = this@MainActivity.supportFragmentManager
fragmentManager.beginTransaction()
.add(R.id.fragment_container, AFragment())
.addToBackStack(null)
.commit()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/fragment_container">
<Button
android:text="Button Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
fragment_a.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:clickable="true"
android:focusable="true">
android:background="@color/colorPrimary"
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A"/>
</LinearLayout>
发生这种情况是因为您将 Button
放在了 ConstraintLayout
中,您将其用作片段的容器。
当您 add
像您正在做的那样将一个片段放入容器中时,它只是以与 View
相同的方式添加它。
因此,如果您将一个 Fragment 添加到 ConstraintLayout
中,而该 ConstraintLayout
已经拥有 Button
作为 child,则该 Fragment 将显示在 Button
旁边,因为ConstraintLayout
允许重叠视图。
这也是为什么,如果您的容器是 LinearLayout
,那么添加片段会将片段放在 Button
下面。
因此,考虑到这一点,解决方案是将它们当作视图来处理。
如果您将一个视图添加到布局中,并且有另一个视图重叠,您将如何摆脱它?
最常见的解决方案是在添加 Fragment 时将 Button 的可见性设置为 INVISIBLE 或 GONE。
另一种解决方案可能是提高片段的高度,因此它现在比您的 Button
高。
当然,您也可以将按钮从Container 中移除,并将其也放置在Fragment 中。
这样,您可以在 FragmentManager
中使用 replace()
方法将包含您的 Button
的片段替换为您要显示的 Fragment
。