Adding a fragment on top of another fragment onClickListener issue

后端 未结 3 1063
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 02:53

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 hav

3条回答
  •  长情又很酷
    2021-02-07 03:44

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

提交回复
热议问题