I have two fragments in an Activity, In portrait mode the first one (ListFragment) is shown when user clicks in somewhere in ListView the second one is shown, while in landscape mode both the fragments are shown. However I want to use the swipe feature while in the portrait mode and that should show the next fragment like this example,
This is my main laucher Activity which extends FragmentActivity,
package com.myexample.fragmentdemoexample;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ViewGroup;
public class MainFragmentActivity extends FragmentActivity {
private static int NUM_ITEMS = 4;
MyAdapter mAdapter;
ViewPager mPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPager = (ViewPager) findViewById(R.id.pager);
instantiateFragments();
Log.i("MainFragmentActivity", "MainFragmentActivity is Created");
}
public void instantiateFragments() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, MyListFragment.class.getName()));
fragments.add(Fragment.instantiate(this, DetailFragment.class.getName()));
mAdapter = new MyAdapter(getSupportFragmentManager(), fragments);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(0);
}
public class MyAdapter extends FragmentPagerAdapter {
private FragmentManager frmanager;
private List<Fragment> fragments;
public MyAdapter(FragmentManager fm, List<Fragment> fragList) {
super(fm);
frmanager = fm;
fragments = fragList;
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
}
}
This is my layout/main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<fragment
android:id="@+id/listFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
class="com.myexample.fragmentdemoexample.MyListFragment"
android:tag="listFragment" >
</fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myexample.fragmentdemoexample.DetailFragment"
android:tag="detailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
</LinearLayout>
and /layout-port/main.xml file contains
<?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="horizontal" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<fragment
android:id="@+id/listFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.myexample.fragmentdemoexample.MyListFragment" />
</LinearLayout>
I don't know why View Pager is not swiping in to next fragment... How can I do that ? I am using Fragment API for the first time.. so please give some pointers to right direction.
NOTE For other files see this question