当我在一个全屏使用 #77000000
背景的 fragment 上显示另一个 fragment(我们称其为主要 fragment)时,我的主要 fragment 仍然会对点击做出反应(即使我们看不到它,也可以点击按钮)。
问题:如何防止第一个(主要的)fragment 上的点击事件?
编辑:
不幸的是,我不能只隐藏主要 fragment,因为我在第二个 fragment 上使用了透明背景(因此,用户可以看到其后面的内容)。
当我在一个全屏使用 #77000000
背景的 fragment 上显示另一个 fragment(我们称其为主要 fragment)时,我的主要 fragment 仍然会对点击做出反应(即使我们看不到它,也可以点击按钮)。
问题:如何防止第一个(主要的)fragment 上的点击事件?
编辑:
不幸的是,我不能只隐藏主要 fragment,因为我在第二个 fragment 上使用了透明背景(因此,用户可以看到其后面的内容)。
clickable
属性设置为true。该视图会捕获事件,以便不会传递给主片段。因此,如果第二个片段的视图是布局,则应使用以下代码:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
RelativeLayout
,并且我使用clickable
属性设置了整个视图。@Dmitry的解决方案解决了我的问题。 - 4gus71n解决方案非常简单。在我们的第二个片段(与主要片段重叠)中,我们只需要捕获 onTouch
事件:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstance){
View root = somehowCreateView();
/*here is an implementation*/
root.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
return root;
}
只需在父布局中添加 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>
focuseable
并不是必需的。 - Dmitry Zaytsevfocusable="true"
只是为了避免在Android Studio中出现警告。 - Artem M如果两个Fragment放置在同一个容器视图中,当显示第二个Fragment时,应该隐藏第一个片段。
如果您想了解有关如何解决有关Fragment的问题的更多问题,请查看我的库:https://github.com/JustKiddingBaby/FragmentRigger
FirstFragment firstfragment;
SecondFragment secondFragment;
FragmentManager fm;
FragmentTransaction ft=fm.beginTransaction();
ft.hide(firstfragment);
ft.show(secondFragment);
ft.commit();
您需要在 android:clickable="true"
后添加 android:focusable="true"
Clickable
表示它可以被指针设备点击或触摸设备轻敲。
Focusable
表示它可以从输入设备(如键盘)获得焦点。输入设备(如键盘)无法根据输入本身决定将其输入事件发送到哪个视图,因此它们将其发送到具有焦点的视图。
方法1:
您可以将其添加到所有片段布局中。
android:clickable="true"
android:focusable="true"
android:background="@color/windowBackground"
FragmentBase
扩展所有的片段等。然后将以下代码添加到FragmentBase
中。@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getView().setBackgroundColor(getResources().getColor(R.color.windowBackground));
getView().setClickable(true);
getView().setFocusable(true);
}
有一些解决方案是我们在这个讨论中共同贡献的,但我还想提到另一种解决方案。如果您不喜欢在XML中为每个布局的根ViewGroup设置clickable和focusable等于true,您也可以像下面这样将它放在您的基类中。
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的人有所帮助。
在Kotlin中:
fragmentManager.findFragmentByTag(BottomFragment.TAG).view.visibility = GONE
FragmentTransaction
的 hide()
和 show()
方法更可取,尤其是在你使用动画时。你只需要在 Transition.TransitionListener
的 onTransitionStart()
和 onTransitionEnd()
中调用它即可。我有多个具有相同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(View view)
,并不写任何内容。这样就可以实现了。
main
Fragment
的Visibility
设置为GONE
。 - adnealonClick
方法不返回任何内容。但是你提供了一个想法,谢谢(我很快会发布答案)。 - Dmitry Zaytsev