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:
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
– Wellington Avelino