我在一个 Activity 中有两个片段,在纵向模式下,当用户在 ListView 中的某个位置单击时显示第一个(ListFragment),而在横向模式下显示两个片段。但是我想在纵向模式下使用滑动功能,并且应该显示下一个片段,例如这个例子,
这是我的主要 laucher 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);
}
}
}
这是我的布局/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>
我不知道为什么 View Pager 没有滑入下一个片段......我该怎么做?我是第一次使用 Fragment API .. 所以请给一些正确的方向指点。
注意对于其他文件,请参阅此问题