ChatGPT解决这个技术问题 Extra ChatGPT

Fragment over another fragment issue

When I'm showing one fragment (which is full screen with #77000000 background) over another fragment (let's call it main), my main fragment still reacts to clicks (we can click a button even if we don't see it).

Question: how to prevent clicks on first (main) fragment?

EDIT

Unfortunately, I can't just hide main fragment, because I'm using transparent background on second fragment (so, user can see what located behind).

Based on what you gave us to work with, you should try setting the Visibility of your main Fragment to GONE when you're not using it.
Without seeing how you implement your onClicked method, I'm guessing you're returning "false" when clicked.
@DeeV, onClick method doesn't returns anything. But you give an idea, thanks (I'll post answer soon).
D'oh. You're right. onTouch returns it. I just wish I understood why a touch event fell through a fragment. It shouldn't do that if you're not issuing touch events.
@DeeV, looks like if your view (that, for example on top of other) doesn't catches onTouch event, then system continue searching for other views with same coordinates.

J
Jure Vizjak

Set clickable property on the second fragment's view to true. The view will catch the event so that it will not be passed to the main fragment. So if the second fragment's view is a layout, this would be the code:

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

This worked for me. It seems easier than the solution @Dmitry Zaitsev gave above. Is there any reason why this would be a bad idea? I can't seem to think of any, but I just want to be sure.
This didn't work for me. I have a RelativeLayout inside the fragment, and I set the whole view with the clickeable property. The solution of @Dmitry solve my problem.
This worked for me. "clickable" in Android apparently is somewhat like iOS' "userInteractionEnabled"
Why does Android subject us to harsh coding conditions?!
I am having same issue with ViewPager. When I scroll on first page, it passes to second page also, and this solution didn't work for me. Any ideas?
D
Dmitry Zaytsev

Solution is pretty simple. In our second fragment (that overlaps our main fragment) we just need to catch onTouch event:

@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;
}

your solution worked +1 for that but can you tell me why we need to do this explicitly ?
@Hunt you don't have to. That is just another way to do it (see accepted answer)
android:clickable="true" on your second fragment's xml main layout.
There is a problem with this solution, when you get in accessibility talkback mode ON, it won't read the individual elements instead gets the focus to root view.
Kotlin version: root.setOnTouchListener { _, _ -> true}
M
Miravzal

Just add clickable="true" and focusable="true" to parent layout

 <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>

If you are using AndroidX, try this

 <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>

Is this somehow different from the accepted answer? focuseable is not really necessary.
I think focusable="true" here is just to avoid a warning in Android Studio.
J
JingYeoh

You should hide the first fragment when you are showing the second Fragment if two fragments is placed in same container view.

If you want to know more questions about how to solve problems about Fragment, you can see my library: https://github.com/JustKiddingBaby/FragmentRigger

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

This should be the right answer! Thank you man. I have solved this question on another project. But I have forgotten How did I solve it and Finally, I got your answer. Thank you.
I don't think this is the right solution. Fragments/Activities work in a view stack. You'll have to call .show again after top fragment is popped off the stack, which means the bottom fragment has to be informed the top one is gone. It's just added extra logic to maintain.
H
Hossam Hassan

You need to add android:focusable="true" with android:clickable="true"

Clickable means that it can be clicked by a pointer device or be tapped by a touch device.

Focusable means that it can gain the focus from an input device like a keyboard. Input devices like keyboards cannot decide which view to send its input events to based on the inputs itself, so they send them to the view that has focus.


and focusable="true" is required to avoid a warning in new Android Studio
M
MarsPeople

Metod 1:

You can add to all fragments layout

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

Metod 2: (Programmatically)

Extend all fragment from FragmentBase etc. Then add this code to 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);
}

Y
Yekta Sarıoğlu

There is more than one solution that some of us contributed to this thread but also I would like to mention one other solution. If you don't fancy putting clickable and focusable equals true to every layout's root ViewGroup in XML like me. You can also put it to your base if you have one just like below;

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
    }

You can also use inline variable but I did not prefer it for personal reasons.

I hope it helps for the ones who hate layout XMLs.


A
Allan Veloso

The acceptable answer will "work", but will also cause performance cost (overdraw, re-measuring on orientation change) as the fragment on the bottom is still being drawn. Maybe you should simply find the fragment by tag or ID and set visibility to GONE or VISIBLE when you need to show again.

In Kotlin:

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

This solution is preferable to the alternative hide() and show() methods of FragmentTransaction when you use animations. You just call it from the onTransitionStart() and onTransitionEnd() of Transition.TransitionListener.


M
Maximilian Ast

What u can do is u can give a Blank click to the previous fragment's layout by using onClick property to parent layout of that main fragment and in activity you can create a function doNothing(View view) and do not write anything in it. This will do it for you.


J
Juan Mendez

This sounds like a case for DialogFragment. Otherwise with Fragment Manager commit one to hide and the other one to show. That has worked for me.


Z
Zhebzhik Babich

The adding of android:clickable="true" didn't work for me. This solution not works on CoordinatorLayout when it's a parent layout. That's why I've made RelativeLayout as parent layout, added android:clickable="true" to it and placed CoordinatorLayout on this RelativeLayout.


A
AskQ

I had multiple fragments with same xml.
After spending hours, I removed setPageTransformer and it started working

   //  viewpager.setPageTransformer(false, new BackgPageTransformer())

I had scalling logic.

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;
    }
}