我有一个 Activity 中有两个片段,以纵向模式为例,当用户在 ListView 中点击某处时,第一个片段(ListFragment)会显示,然后会显示第二个片段;而在横向模式下,两个片段都会显示。但是我想在纵向模式下使用滑动功能,从而显示下一个片段,例如 this example。
这是我的主要启动 Activity,它扩展了 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);
}
}
}
这是我的layout/main.xml文件
<?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>
而 /layout-port/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="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>
我不知道为什么视图翻页器无法滑动到下一个片段... 我该怎么做?这是我第一次使用片段API... 所以请指引我正确的方向。
注意 其他文件请参见 this question