android – Adding a fragment on top of another fragment onClickListener issue-ThrowExceptions

Exception or error:

I’m adding a fragment to an activity instead of replacing the current fragment (because this corresponds to the type of behavior I want to have).

My problem is that clicking in a spot on the top fragment (the one that’s currently visible), where a view in the non-visible fragment is located, causes an onClick event on the view in the second, non-visible fragment, to fire.
Why is this happening and how can I prevent this?

This is the code I use to first add the ListView fragment to the activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    if (savedInstanceState == null) {
        listFragment = new ListFragment ();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.frame_container, listFragment)
                .addToBackStack(listFragment .TAG)
                .commit();
    }

    ...

}

In this same activity I’m adding the second fragment, on top of the list fragment:

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    ...
            createItemFragment = new CreateItemFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.frame_container, createItemFragment)
                    .addToBackStack(createItemFragment.TAG)
                    .commit();
    ...
}
How to solve:

You can just add the following attribute to the XML root layout of the fragment that agoes on top-

android:clickable="true"

This will ensure that touch events will not propagate further than the top layer.

###

If you’re adding a fragment it’ll overlap all the fragments under it So if you want to display both views at the same time this is the way to do it. Naturally with both view present both will listen for touch events. If you want to preserve the fragment but not show it use:

FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.detach(fragment).commit();

That will remove the fragments view without destroying the fragment.

you can call

FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.attach(fragment).commit();

later to reattach it to the view.

alternatively you could just change your on click listener to

public void onClick (View v){
    if(!v.isShown()){
        return;
    }
    //Stuff the listener should do.
}

###

well,I think the best way is set onClickListener to the locateView which you click on the top-fragment .

if you use the way android:clickable=”true” answered by The Metal Beard, and also you set any ImageView’s selector states (see below), then you will find the bug : if you click the other space place ,the imageView will change the status.

<ImageView
        android:id="@+id/mCollect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/specialprice_collect_selector" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/specialprice_collect_p" android:state_pressed="true"/>
    <item android:drawable="@drawable/specialprice_collect_n"/>
</selector>

if the clicked View on top-fragment is mBootomLayout,just do it like this:

mBootomLayout.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        }
    });

Leave a Reply

Your email address will not be published. Required fields are marked *