Search code examples
androidtouch-event

Pass touch events to underlying view when click on transparent area, but allow clicks on buttons


I have a problem because I don't know how to make the transparent background of Fragment B pass on touch events to Fragment A:

diagram

Fragment C has Fragment B layered on top of Fragment A: Fragment C XML:

...
    <FrameLayout
        android:id="@+id/fragment_A_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/fragment_B_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
 ...

And here is where Fragment C attaches the fragments to those FrameLayouts: Fragment C class:

...
fragmentTransaction.add(R.id.fragment_A_container, FragmentA, TAG_FRAGMENT_A);
fragmentTransaction.add(R.id.fragment_B_container, FragmentB, TAG_FRAGMENT_B);
...

But now, I don't know how to only let the toolbar and floating action button get clicked, but the rest of the background pass on the click event to Fragment A? Here is a basic idea of Fragment B's layout: FragmentB XML:

<LinearLayout
    android:layout_gravity="bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <android.support.design.widget.FloatingActionButton/>
        <android.support.v7.widget.Toolbar
            android:layout_gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

Thank you!


Solution

  • It actually did work, but I noticed that in my code, it was really doing this (FragmentD is an opaque background meant to go underneath FragmentA and FragmentB):

    fragmentTransaction.add(R.id.fragment_A_container, FragmentD, TAG_FRAGMENT_D);
    fragmentTransaction.add(R.id.fragment_A_container, FragmentA, TAG_FRAGMENT_A);
    fragmentTransaction.add(R.id.fragment_B_container, FragmentB, TAG_FRAGMENT_B);
    

    I would think that by adding FragmentD and then adding FragmentA to fragment_A_container, FragmentA should be on top of FragmentD, but I guess it wasn't the case. So I guaranteed that the order would be correct by separating them into their own FrameLayouts, and it worked fine:

    Updated XML:

    ...
        <FrameLayout
            android:id="@+id/fragment_D_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    
        <FrameLayout
            android:id="@+id/fragment_A_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    
        <FrameLayout
            android:id="@+id/fragment_B_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
     ...