2014-09-30 64 views
-1

我正在使用片段来做一个简单的3页面应用程序,页面之间有幻灯片。安卓幻灯片片段不全加载

但现在,我有3个片段,但只去2和3

应用幻灯片它必须做1 - >2 - >3,但它做3 - >2 - >3。如果你知道我想说什么。

这是我的代码

public class main extends Activity { 



public static SectionsPagerAdapter mSectionsPagerAdapter; 
public static Boolean init = true; 


static ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager()); 

    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 
} 



public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    [...] 
} 


public static class PlaceholderFragment extends Fragment { 

    private static final String ARG_SECTION_NUMBER = "section_number"; 
    private static int ARG_NUMBER = 0; 



    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     ARG_NUMBER = sectionNumber; 
     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 = new View(this.getActivity()); 

    // THE PROBLEM SHOULD COME FROM HERE 

      switch (ARG_NUMBER) { 
       case 1: 
        rootView = inflater.inflate(R.layout.fragment_main, container, false); 
        break; 
       case 2: 
        rootView = inflater.inflate(R.layout.fragment_map, container, false); 
        break; 
       case 3: 
        rootView = inflater.inflate(R.layout.profile, container, false); 
        break; 
      } 


     return rootView; 
    } 
} 
} 

因此,如果任何人有一个想法...谢谢!

编辑:

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
     } 
     return null; 
    } 
} 
+1

“SectionsPagerAdapter”的邮政编码 – 2014-09-30 07:49:36

+0

我已将它添加到编辑中 – F4Ke 2014-09-30 07:54:12

回答

0

你应该片段instead ofuse arguments设置ARG_NUMBER = sectionNumber

你的代码应该是这个样子:

public static class PlaceholderFragment extends Fragment { 

    private static final String ARG_SECTION_NUMBER = "section_number"; 
    private int ARG_NUMBER = 0; 



    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 = new View(this.getActivity()); 

     ARG_NUMBER = getArguments().getInt(ARG_SECTION_NUMBER, 0); 

      switch (ARG_NUMBER) { 
       case 1: 
        rootView = inflater.inflate(R.layout.fragment_main, container, false); 
        break; 
       case 2: 
        rootView = inflater.inflate(R.layout.fragment_map, container, false); 
        break; 
       case 3: 
        rootView = inflater.inflate(R.layout.profile, container, false); 
        break; 
      } 


     return rootView; 
    } 
} 

注:使用的if-else梯若开关箱不允许非常量值。

+0

这是如何解决适配器中存在错误片段的问题的?此外,您使用0作为getInt的默认值,而switch语句中没有默认情况下处理该值。其次,getArguments().getInt不检查null,但Fragment可以非常好地用它的空构造函数实例化,所以为什么不使用谷歌在关于Fragments的官方文档中建议的接口方法,所以它会抛出一个有意义的异常的NPE? – 2Dee 2014-09-30 08:18:36

+0

它的工作原理,谢谢 – F4Ke 2014-09-30 08:37:53

+0

@ 2Dee:这里,fragment将会成为'ViewPager'的一部分,所以不可能有这样的场景。而且我只提出了一些参数,请参阅我的答案。它只是基于与OP提供的现有代码相比的更改,并且在处理独立片段时可能会遇到这种情况。无论如何,随时编辑或张贴您的答案。 – 2014-09-30 09:39:47