2017-07-04 16 views
0

我已经实现了简单的ViewPager及其分页适配器.PagerAdapter需要在ViewPager中显示的视图列表。我还没有为ViewPager调用setOffscreenPageLimit来设置限制。 因此,默认情况下选择1个限制(1页应该保留在闲置状态下的当前页面的任一侧)。但实际上在两侧保留了2页。Android viewpager实际默认的屏幕外页面限制

0 | 1 | 2(current visible) | 3 | 4 

请问您是否确认为什么会发生?

+0

“有2页保留,两侧” - 你怎么知道这是真的吗?你在看什么变量或对象?谢谢! –

+0

在空闲状态,我检查了container.getChildCount()(在PagerAdapter的instantiateItem方法中使用),它给出了5.这意味着有两页保留在任何一方。 –

+0

我试图用包含ListView的简单片段来重现此行为,使用android.support.v4.app.FragmentPagerAdapter和android.support.v4.view.ViewPager,但container.getChildCount在我的测试应用程序中始终保持3或更低。你使用的是我使用的ViewPager类吗? –

回答

0

我试图重现此问题,但是用下面的代码我看到container.getChildCount()总是3或更低:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    SectionsPagerAdapter mSectionsPagerAdapter; 

    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
    } 

    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     private String TAG = SectionsPagerAdapter.class.getSimpleName(); 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return PlaceholderFragment.newInstance(position + 1); 
     } 

     @Override 
     public Object instantiateItem(ViewGroup container, int position) { 
      Log.v(TAG, "container.getChildCount:" + container.getChildCount()); 
      return super.instantiateItem(container, position); 
     } 

     @Override 
     public int getCount() { 
      return 10; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      return String.format(l, "Section %d", position); 
     } 
    } 

    public static class PlaceholderFragment extends Fragment { 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public PlaceholderFragment() {} 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      ListView listView = (ListView) rootView.findViewById(R.id.listView); 
      ArrayAdapter<String> listAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1); 
      listAdapter.add("Fragment Body " + getArguments().getInt(ARG_SECTION_NUMBER)); 
      listAdapter.add("one"); 
      listAdapter.add("two"); 
      listAdapter.add("three"); 
      listView.setAdapter(listAdapter); 
      return rootView; 
     } 
    } 

} 

activity_main.xml中

<android.support.v4.view.ViewPager 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/pager" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"/> 

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/white" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      tools:context=".MainActivity$PlaceholderFragment"> 

<ListView 
    android:id="@+id/listView" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" />