I have 3 fragments in my activity, managed by a custom class which extends FragmentPagerAdapter.
My Main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="@+id/tabviewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
My activity :
public class RankingFragmentActivity extends FragmentActivity{
....
List fragments = new Vector();
fragments.add(Fragment.instantiate(this,Tab1Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Tab2Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Tab3Fragment.class.getName()));
//adapter
MyPagerAdapter mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.tabviewpager);
pager.setAdapter(this.mPagerAdapter);
}
I can perfectly switch from fragments from letf to right (and right to left).
I ‘d like to set buttons as listeners (in order to change displayed fragment) but I don’t know how to do.
ex:
click on button1 : Fragment 1 is displayed
click on button2 : Fragment 2 is displayed
click on button3 : Fragment 3 is displayed
Thank you !
The answer is right in the documentation for FragmentPagerAdapter