另一个碎片问题的碎片

当我在另一个片段(让我们称它为主)上显示一个片段(它是全屏的#77000000背景)时,我的主片段仍然对点击做出反应(我们可以点击一个按钮,即使我们看不到它)。

问题:如何防止点击第一个(主)片段?

编辑

不幸的是,我不能只是隐藏主片段,因为我在第二个片段上使用透明的背景(所以,用户可以看到后面的位置)。

96853 次浏览

解决方法很简单。在第二个片段(与主片段重叠)中,我们只需要捕获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。视图将捕获事件,这样它就不会被传递给主片段。因此,如果第二个片段的视图是一个布局,这将是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />

你能做的是你可以通过使用onClick属性父主片段的布局,给前一个片段的布局一个空白点击,在活动中你可以创建一个函数doNothing(View view),不写任何东西。这个可以帮你。

这听起来像是DialogFragment的一个案例。否则,使用片段管理器提交一个隐藏,另一个显示。这对我来说很有效。

如果两个片段放在同一个容器视图中,则在显示第二个片段时应该隐藏第一个片段。

如果你想知道更多关于如何解决Fragment问题的问题,你可以看到我的库:https://github.com/JustKiddingBaby/FragmentRigger

FirstFragment firstfragment;
SecondFragment secondFragment;
FragmentManager fm;
FragmentTransaction ft=fm.beginTransaction();
ft.hide(firstfragment);
ft.show(secondFragment);
ft.commit();

只需将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>

添加android:clickable="true"对我不起作用。当CoordinatorLayout是父布局时,此解决方案不适用于该布局。这就是为什么我将RelativeLayout作为父布局,添加android:clickable="true"并将CoordinatorLayout放在这个RelativeLayout上。

可接受的答案将“工作”,但也会导致性能成本(透支,重新测量方向变化),因为底部的片段仍在绘制中。也许你应该简单地通过标签或ID找到片段,并将可见性设置为GONE或当你需要再次显示时可见。

在芬兰湾的科特林:

fragmentManager.findFragmentByTag(BottomFragment.TAG).view.visibility = GONE

当你使用动画时,这个解决方案比FragmentTransactionhide()show()方法更可取。你只需从Transition.TransitionListeneronTransitionStart()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;
}
}

你需要用android:clickable="true"添加android:focusable="true"

Clickable意味着它可以由指针设备点击或由触摸设备点击。

Focusable意味着它可以从键盘等输入设备获得焦点。键盘等输入设备不能根据输入本身决定将输入事件发送到哪个视图,所以它们将它们发送到有焦点的视图。

Metod 1:

你可以添加到所有的片段布局

android:clickable="true"
android:focusable="true"
android:background="@color/windowBackground"

方法2:(以程序方式)

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中。你也可以把它放在你的底座上,如果你有一个像下面这样的;

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的人。