I'm trying to get my app to display a new Fragment from a click. The message is printed on the console when I click the button, but the 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!");
}
});
}
Am I doing something wrong?
Thanks in advance 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 owns the FAB and the 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 list view is replaced by the form view.
In this form there is an ImageView for which I created a ClickListener which has the responsibility of returning to the listing page with the CardViews. This image is a back button. The method for doing 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 that behind the CardViews you can see the old CardViews that were loaded before calling the form.
The flow is this:
Initial Screen with CardViews - > Form - > Initial Screen with CardViews.
The image below shows what is actually happening:
Note that in the spaces separating one CardView and another it is possible to see the cards loaded as soon as the app is executed.