22

I have developed an application that have navigation Drawer and many fragment inside drawer so I getting issue while I am open fragment inside fragment, in one fragment I have List view when user click on listview item they getting data related to list item so I facing problem its still click on list that is not visible but click

layout for Fragment

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:clickable="true"
        android:background="@drawable/backgroung"
        android:id="@+id/content_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"></FrameLayout>

    <LinearLayout
        android:id="@+id/linearDrawer"
        android:layout_width="@dimen/dp260"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/white"
        android:layout_gravity="start">

        <RelativeLayout
            android:id="@+id/userDrawer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_margin="@dimen/dp10">

Code for open Fragment

Fragment fragment = new FragmentContactDetails();
            FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.add(((ViewGroup) getView().getParent()).getId(), fragment);
            transaction.addToBackStack(null);
            transaction.commit();

            Bundle args = new Bundle();
            int index = adapter.arrayList.indexOf(adapter.propertyList.get(position));

            args.putInt(Constant.POSITION, index);
            Toast.makeText(getActivity(), "" + index + "----" + adapter.propertyList.get(position).getName(), Toast.LENGTH_LONG).show();
            fragment.setArguments(args);

3 Answers3

77

Take a look at this question of mine:

An invisible layout behind the fragment is getting clicked:

What helped me in this case and as the accepted answer states is to add the

android:clickable="true"

property to the top hierarchy ViewGroup of the layout you designed for the fragment on top ( the one that is being clicked through). That way the top fragment intercepts your clicks no matter where they occur (and even if no action is applied to it) and are not being passed to the lower level fragment/activity layout.

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • Where i have to implement this tag,, in main Frame layout or else..? –  May 28 '14 at 13:30
  • yes, the most top FrameLayout, RelativeLayout... of the top fragment. – Emil Adz May 28 '14 at 13:31
  • You can even try to apply it the the drawerLayout... but this should work.... And just to be clear you have a drawer in your fragment? – Emil Adz May 28 '14 at 13:41
  • but I'm using the replace api of the FragmentTransaction.. why does the previous fragment remain alive under the new one? – andrea.rinaldi Dec 04 '15 at 10:40
0

As of now, with compile SDK 33

android:clickable="true"

is NOT sufficient when your view is match_parent, it should be fill_parent insstead

SaadurRehman
  • 622
  • 8
  • 20
-1

Edit YOur code commit line from middle to end

    Fragment fragment = new FragmentContactDetails();
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.add(((ViewGroup) getView().getParent()).getId(), fragment);


                Bundle args = new Bundle();
                int index = adapter.arrayList.indexOf(adapter.propertyList.get(position));

                args.putInt(Constant.POSITION, index);
                Toast.makeText(getActivity(), "" + index + "----" + adapter.propertyList.get(position).getName(), Toast.LENGTH_LONG).show();
                fragment.setArguments(args);
transaction.addToBackStack(null);
                transaction.commit();
Rizwan Ahmed
  • 1,272
  • 2
  • 16
  • 27