How to replace a Fragment on button click of that fragment?

后端 未结 6 1050
南笙
南笙 2020-11-30 09:00

I have an activity containing multiple fragments. Activity initially have fragment and in it have two buttons. Upon clicking this button I have to replace the fragment by ne

6条回答
  •  臣服心动
    2020-11-30 09:49

    Well even I am learning android...

    I solved same problem recently, "How to Change Fragment On button's click event".

    buttonName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentTransaction fragmentTransaction = getActivity()
                .getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame1, new Homefragment());
            fragmentTransaction.commit();
        }
    });
    

    Here frame1 is id of FrameLayout which have define in my DrawerLayer's XML.

    So now whenever I want fragment transaction I use this code. Each time it will replace frame1 instated of your last fragment.

    FragmentTransaction fragmentTransaction = getActivity()
            .getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame1, new newfragment());
    fragmentTransaction.commit()
    

    Hope this will help..

提交回复
热议问题