Android(ViewPager中的ViewPager)ViewPager FragmentPagerAdapter嵌套子ViewPager FragmentPagerAdapter页面不能正常显示问题
Android的ViewPager如果只使用一“层” FragmentPagerAdapter构建ViewPager,那么将不会导致本文题目中所说的问题,当ViewPager再次嵌套一个ViewPager时候,如果细节地方使用不当将导致页面不能正常显示问题。
比如,如图所示:
这是一个典型的ViewPager嵌套ViewPager的框架布局。底部0,1,2,3是一个SlidingTabLayout,SlidingTabLayout,SlidingTabLayout上面是一个ViewPager,其布局文件代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/slidingTabLayout" />
<zhangphil.viewpagerdebug.SlidingTabLayout
android:id="@+id/slidingTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Java代码:
package zhangphil.viewpagerdebug;
import java.util.ArrayList;
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.view.ViewPager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyViewPagerAdapter adapter = new MyViewPagerAdapter(this.getSupportFragmentManager());
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(adapter);
SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.slidingTabLayout);
mSlidingTabLayout.setViewPager(viewPager);
}
private class MyViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
public MyViewPagerAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<Fragment>();
for (int i = 0; i < 4; i++) {
PageFragment f=new PageFragment();
fragments.add(f);
}
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public Fragment getItem(int pos) {
return fragments.get(pos);
}
@Override
public CharSequence getPageTitle(int pos) {
return "" + pos;
}
}
}
将ViewPager与SlidingTabLayout联结起来。在此ViewPager中的MyViewPagerAdapter中喂给每一个ViewPager的子page又一个ViewPager——也是如果使用不当将引起问题的PageFragment:
package zhangphil.viewpagerdebug;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.pagefragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.setSmoothScrollingEnabled(true);
MyViewPagerAdapter pagerAdapter = new MyViewPagerAdapter(this.getChildFragmentManager());
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
private class MyViewPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
public MyViewPagerAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<Fragment>();
for (int i = 0; i < 5; i++) {
Fragment f=new ContentFragment();
fragments.add(f);
}
}
@Override
public String getPageTitle(int pos) {
return "卡" + pos;
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public Fragment getItem(int pos) {
return fragments.get(pos);
}
}
}
PageFragment的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
app:tabGravity="fill"
app:tabMode="fixed" >
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tabLayout"
android:layout_alignParentBottom="true" />
</RelativeLayout>
PageFragment就是图中顶部的卡0,卡1,卡2,卡3,卡4分别显示的每一个page。ContentFragment只是一个继承自ListFragment的、仅仅显示一行数据的演示Fragment。
PageFragment作为第二层、第二级ViewPager(ViewPager中的ViewPager)在PageFragment中创建此PageFragment的FragmentPagerAdapter时候,切记要传递给FragmentPagerAdapter的构造函数参数FragmentManager为:getChildFragmentManager() 而不是像父ViewPager中
那样直接随意的直接getFragmentManager()。如果PageFragment直接仍然用getFragmentManager()构造其FragmentPagerAdapter,将导致第二层、第二级的ViewPager不能正常显示。切记切记!