我的例子包括一个Mainactivity.java(FragmentActivity),一个Fragment Called Fragment_ViewPager(Fragment),一个适配器Adapter_ViewPager(FragmentPagerAdapter),三个片段是常见的片段Fragment extentienden,并且有一个方法onCreateView。
Mainactivity.java
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager FM = getSupportFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
Fragment_ViewPager f_pager = new Fragment_ViewPager();
FT.add(R.id.fragment_container, f_pager);
FT.commit();
}
}
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
Fragment_ViewPager.java
private static final int[] ICONS = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
private FragmentPagerAdapter mFragmentPagerAdapter;
private ViewPager mViewPager;
private TabPageIndicator mTabPagerIndicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.f_pager, container, false);
mFragmentPagerAdapter = new Adapter_ViewPager(getActivity().getSupportFragmentManager(), ICONS);
mViewPager = (ViewPager)v.findViewById(R.id.view_pager_inicio);
mViewPager.setAdapter(mFragmentPagerAdapter);
mTabPagerIndicator = (TabPageIndicator)v.findViewById(R.id.tab_indicator_inicio);
mTabPagerIndicator.setViewPager(mViewPager);
return v;
}
fragment_viewpager.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="vertical" >
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/tab_indicator_inicio"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager_inicio"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
Adapter_ViewPager.java
private static int[] ICONS = null;
private Fragment mFragment = null;
public Ad_inicio(FragmentManager fm, int[] icons) {
super(fm);
ICONS = icons;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
mFragment = new Fragmento_uno();
break;
case 1:
mFragment = new Fragmento_dos();
break;
case 2:
mFragment = new Fragmento_tres();
break;
default:
break;
}
return mFragment;
}
@Override
public int getIconResId(int index) {
return ICONS[index];
}
@Override
public int getCount() {
return ICONS.length;
}
Fragment_One.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmento_One, container, false);
return v;
}
fragment_one.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="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="uno" />
</LinearLayout>
适用于片段的滑动,但问题是当我将方向从纵向更改为horizintal我的设备时,滑动不起作用并且只显示fragemnto在垂直方向之前可见... 我想知道我做错了什么,因为viewpager必须在FragmentAcitivity中运行,但是对于这个例子我必须扩展Fragment以使用dentrod一个FrameLayout,看起来就像它的getActivity()。 GetSupportFragmentManager()我将他发送给Adpater第二次无法正常运行。
答案 0 :(得分:0)
不要使用Adapter_ViewPager
类的其他构造函数,而不是默认的constructor(FragmentManager)
!
我认为,当您的活动重新启动时,Android会调用默认构造函数,ICONS
&lt; ll将为null。
尝试在那里放置一个断点,看看旋转后ICONS
的值是什么。