FragmentStatePagerAdapter 重置第一个片段的内容
FragmentStatePagerAdapter resets content of first fragment
当应用程序启动时它显示第一个片段并准备第二个片段,当我滑到第二个时它正在准备第三个...当我从第二个滑回第三个时一切都很好但是当幻灯片 1 -> 2 -> 3 - > 2 -> 1 第一个内容已重置...这里可能有什么问题?
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
FoldersFragment foldersFragment = new FoldersFragment();
NowPlayingFragment nowPlayingFragment = new NowPlayingFragment();
EqFragment eqFragment = new EqFragment();
public ViewPager getMViewPager() {
return mViewPager;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
foldersFragment.setNowPlayingFragment(nowPlayingFragment);
nowPlayingFragment.setFoldersFragment(foldersFragment);
eqFragment.setMediaPlayer(nowPlayingFragment.mp);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
System.out.println("0");
return foldersFragment;
case 1:
System.out.println("1");
return nowPlayingFragment;
case 2:
System.out.println("2");
return eqFragment;
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
}
默认的离屏限制为 1,因此每次您转到下一个片段时,它都会检查是否有片段已经离开屏幕,并在您返回时重新启动:
如果您当前在第 3 页
1 -> |offset| 2 -> (3) -> 4 -> |offset| 5
因此page 1 and page 5
如果在当前页+偏移量之前或之后将重新启动。
要增加偏移量,您可以调用:
mViewPager.setOffscreenPageLimit(4)
当应用程序启动时它显示第一个片段并准备第二个片段,当我滑到第二个时它正在准备第三个...当我从第二个滑回第三个时一切都很好但是当幻灯片 1 -> 2 -> 3 - > 2 -> 1 第一个内容已重置...这里可能有什么问题?
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
FoldersFragment foldersFragment = new FoldersFragment();
NowPlayingFragment nowPlayingFragment = new NowPlayingFragment();
EqFragment eqFragment = new EqFragment();
public ViewPager getMViewPager() {
return mViewPager;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
foldersFragment.setNowPlayingFragment(nowPlayingFragment);
nowPlayingFragment.setFoldersFragment(foldersFragment);
eqFragment.setMediaPlayer(nowPlayingFragment.mp);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
System.out.println("0");
return foldersFragment;
case 1:
System.out.println("1");
return nowPlayingFragment;
case 2:
System.out.println("2");
return eqFragment;
}
return null;
}
@Override
public int getCount() {
return 3;
}
}
}
默认的离屏限制为 1,因此每次您转到下一个片段时,它都会检查是否有片段已经离开屏幕,并在您返回时重新启动:
如果您当前在第 3 页
1 -> |offset| 2 -> (3) -> 4 -> |offset| 5
因此page 1 and page 5
如果在当前页+偏移量之前或之后将重新启动。
要增加偏移量,您可以调用:
mViewPager.setOffscreenPageLimit(4)