Change Fragment from click

Asked

Viewed 1,841 times

3

I’m trying to get my app to display a new Fragment from one click. The message is printed on the console when I click on the button, but Fragment does not change.

@Override
public View onCreateView(final LayoutInflater inflater,
                         final ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_start, container, false);

    FAB = (ImageButton) view.findViewById(R.id.imageButton);
    FAB.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            View view = inflater.inflate(R.layout.fragment_info, container, false);
            System.out.println("Clique!");
        }

    });
}

I’m doing something wrong?

Thanks for your help!

==========

When I start my application it shows a listing (in Cardviews) of a series of records returned by my Webservice.

On this same screen is displayed a Floatactionbutton that is responsible for calling the form view. It was to create the action of this FAB that I opened this question. Currently, the onCreateView method of my Fragment that has FAB and Cardviews is as follows:

@Override
public View onCreateView(final LayoutInflater inflater,
                         final ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_classrooms, container, false);

    FAB = (ImageButton) view.findViewById(R.id.imageButton);
    FAB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NewClassroomFragment newClassroomFragment = new NewClassroomFragment();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.classrooms, newClassroomFragment);
            ft.commit();
            System.out.println("Clique!");
        }

    });
}

So far everything is working perfectly, thanks to the answers already received: when I click the button, my listing view is replaced by the form view.

In this form there is an Imageview for which I created a Clicklistener that has the responsibility to return to the listing page with Cardviews. This image is a back button. The method to do this is identical to the previous one:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_new_classroom, container, false);

    BACK = (ImageView) v.findViewById(R.id.back);
    BACK.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ClassroomsFragment classroomFragment = new ClassroomsFragment();
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.addToBackStack(null);
            ft.replace(R.id.classrooms, classroomFragment);
            ft.commit();
            System.out.println("Voltar!");
        }

    });

    return v;
}

This "back button" is working perfectly, except for the fact that behind the Cardviews it is possible to see the old Cardviews that were loaded before calling the form.

This is the flow:

Home Screen with Cardviews -> Form -> Home Screen with Cardviews.

The image below shows what is actually happening: CardViews com Problemas

Note that in the spaces that separate one Cardview and another you can see the cards loaded right in the exception of the app.

  • From a beginTransaction look at Fragments, with it Oce can work dynamically, later put to Oce as it does

1 answer

2

In your Activity create a method this way:

public void openFragment()
{
    Fragment fr = new Fragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_place, fr);
    fragmentTransaction.commit();
}

Your click method should look like this:

 public void onClick(View v) {
     openFragment();
     System.out.println("Clique!");
 }

For Fragment, we should not change the view, but change the Fragment.

To better understand a look at documentation clicking here.

  • Since the method onClick() is in the Fragment foul getActivity() before openFragment(), would be so: getActivity().openFragment();

  • If I put getActivity(). openFragment(); returns the error "Cannot resolve method 'openFragment()' ". What I’m doing wrong?

  • openFragment() is the method that Voce implemented ! where it is ? in the same class you call?

  • 1

    A cast on getActivity() is required for the Activity that Voce implemented

  • Yes Thiago. He’s in the same class. I tried like this: Tab2 tab2 = new Tab2(); Fragmentmanager fm = getFragmentManager(); Fragmenttransaction ft = fm.beginTransaction(); ft.replace(R.id.class_fragment, tab2); ft.commit(); The problem there is that Androidstudio gives me an error because it cannot find class_fragment. If I do R.layout.class_fragment it finds, but warns that it expects a Resource of type id.

  • Then the class_fragment is the container that will receive your Fragment in case a Framelayout. When Voce created the xml you declared a Framelayout?? In case it’s his id that goes in place of the class_fragment

  • I’m already getting a bit bewildered here. I’ve tried all these ways. I’m having trouble passing the parameters to do replace. After changing the code I can now find the Framelayout id that will be replaced. For example: ft.replace(R.id.classrooms, tab2); The problem is that it now gives me an error in the second parameter because it says import android.supportv4.app.Fragment is required. If I do this, all other previous instructions fail.

  • then use the getSupportFragmentManager() ** instead of ** getFragmentManager . Since we don’t know what you’re using for your project, this really happens! Take it easy, read the documentation and most importantly try to understand what you’re doing Anything we’re here to help

  • @Thiagoluizdomacoski I managed to carry out the transaction the way you instructed me. It happens that now I can display another Fragment, but the old Fragment keeps appearing under this new view. What should I put in the code so that old Fragment (or old view) simply ceases to exist?

  • Could you show your full code? I’ve never seen this happen!

  • @Thiagoluizdomacoski Just take a look at my Dit.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.