I am trying to switch between fragments on my activity. I was reading this tutorial, but my case is a bit different since I don't want/can't use the 'FragmentPagerAdapter', instead, I want that a button that is pressed on activity will switch between 2 fragments.
My activity layout consist of a Button and ViewPager. in Addition I have got Fragment1 and Fragment2. How can I switch between this fragments using OnClick method?
My Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Switch it"
android:onClick="switchFragment"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
and my activity:
public class MainHeaderFragment2 extends ActionBarActivity {
...
public void switchFragment(View view){
Fragment fragment
if(checkSomething())
fragment = new Fragment1();
else
fragment = new Fragment2();
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
// now need to put the selected fragment in ViewPager somehow.
// How? that is my question
}
}
0 comments:
Post a Comment