当我将一个片段(全屏显示,背景#77000000)显示在另一个片段(我们称之为main)之上时,我的主片段仍然会对点击做出反应(即使我们看不到它,我们也可以单击按钮)。
问题:如何防止点击第一个(主)片段?
EDIT
不幸的是,我不能只是隐藏主片段,因为我在第二个片段上使用透明的背景(所以,用户可以看到后面的位置)。
当我将一个片段(全屏显示,背景#77000000)显示在另一个片段(我们称之为main)之上时,我的主片段仍然会对点击做出反应(即使我们看不到它,我们也可以单击按钮)。
问题:如何防止点击第一个(主)片段?
EDIT
不幸的是,我不能只是隐藏主片段,因为我在第二个片段上使用透明的背景(所以,用户可以看到后面的位置)。
当前回答
只需添加clickable="true"和focusable="true"到父布局
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<!--Your views-->
</android.support.constraint.ConstraintLayout>
如果你正在使用AndroidX,试试这个
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<!--Your views-->
</androidx.constraintlayout.widget.ConstraintLayout>
其他回答
如果两个片段放在同一个容器视图中,则在显示第二个片段时应该隐藏第一个片段。
如果你想了解更多关于如何解决Fragment问题的问题,你可以查看我的库:https://github.com/JustKiddingBaby/FragmentRigger
FirstFragment firstfragment;
SecondFragment secondFragment;
FragmentManager fm;
FragmentTransaction ft=fm.beginTransaction();
ft.hide(firstfragment);
ft.show(secondFragment);
ft.commit();
我有多个相同的xml片段。 在花了几个小时后,我删除了setPageTransformer,它开始工作了
// viewpager.setPageTransformer(false, new BackgPageTransformer())
我有召唤逻辑。
public class BackgPageTransformer extends BaseTransformer {
private static final float MIN_SCALE = 0.75f;
@Override
protected void onTransform(View view, float position) {
//view.setScaleX Y
}
@Override
protected boolean isPagingEnabled() {
return true;
}
}
你能做的是你可以给一个空白点击到以前的片段的布局使用onClick属性父布局的主片段和在活动中,你可以创建一个函数doNothing(视图视图),不写任何东西。这个可以帮你。
只需添加clickable="true"和focusable="true"到父布局
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<!--Your views-->
</android.support.constraint.ConstraintLayout>
如果你正在使用AndroidX,试试这个
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true">
<!--Your views-->
</androidx.constraintlayout.widget.ConstraintLayout>
我们中的一些人在这个帖子中提供了不止一个解决方案,但我也想提到另一个解决方案。如果你不喜欢把可点击和可聚焦等同于每个布局的根视图组在XML中。你也可以把它放在你的底座上,如果你有一个像下面这样的;
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) : View? {
super.onCreateView(inflater, container, savedInstanceState)
val rootView = inflater.inflate(layout, container, false).apply {
isClickable = true
isFocusable = true
}
return rootView
}
你也可以使用内联变量,但我不喜欢它的个人原因。
我希望它能帮助那些讨厌布局xml的人。