okay here is how to add button in fragment A to switch fragment c : 1- add button in Fragment a in xml file which id foo like this :
<Button
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to fragemnt c" />
go to fragment a class overide method called onCreateView and set onclickListner create by creating interface called it buttonClick and create var from it in side fragment a then override method onAttch initialize interface var inside onAttch method:
public class FragmentA extends Fragment {
buttonClick click;
Button foo;
interface buttonClick {
void buttonClicked(View v);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
click = (buttonClick) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container,false);
foo = (Button) view.findViewById(R.id.foo);
foo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
click.buttonClicked(v);
}
});
return view;
}
}
then back to your MainActivity implements buttonClick interface and override methods in the method called buttonClicked(View v); setCurrentItem for view pager like this :
calss MainActivity implements FragmentA.buttonClick {
// your code here ...
public void buttonClicked(View v){
//get your viewPager var
viewPager.setCurrentItem(3);
}
}
i hope this helps