2013-03-27 56 views
1

我试图从片段内加载活动。我简单地使用了基本的android示例代码并添加了一些不同的布局。下面的代码部分工作,但是,意图/活动在滑动到第3页时开始,而不是在第4页时开始。我认为从第3页滑动到第2页时它也开始了一次。我认为这与android想要的有关预先加载页面上的所有内容以使其滑动无缝。Android:从片段页面加载活动或意图

当从一个片段页面滑动到另一个片段页面时,是否有加载活动的方法?或者,也许有一些迂回的方式才能在我实际上滑入相应页面之后才开展活动?除了不良行为,我没有得到任何错误。

public static class DummySectionFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public DummySectionFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 


     if (getArguments().getInt(ARG_SECTION_NUMBER)==2){ 
      LayoutInflater inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View mDialogView = inflater2.inflate(R.layout.activity_main2, null); 
      return mDialogView; 
     } else if (getArguments().getInt(ARG_SECTION_NUMBER)==3){ 
      LayoutInflater inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View mDialogView = inflater2.inflate(R.layout.activity_main3, null); 
      return mDialogView; 
     } else if (getArguments().getInt(ARG_SECTION_NUMBER)==4){ 
      LayoutInflater inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View mIntentView = inflater2.inflate(R.layout.page_four_layout, null); 
      Intent intent = new Intent(getActivity().getApplication(), PageFourActivity.class); 
      startActivity(intent); 
      return mIntentView; 
     } else { 

      // Create a new TextView and set its text to the fragment's section 
      // number argument value. 
      TextView textView = new TextView(getActivity()); 
      textView.setGravity(Gravity.CENTER); 
      textView.setText(Integer.toString(getArguments().getInt(
       ARG_SECTION_NUMBER))); 
      return textView; 
     } 


    } 

回答

1

如果您使用的是ViewPagerFragment S,我觉得应该ViewPager.SimpleOnPageChangeListener帮助你。看看onPageSelected的功能。就像你猜测的那样,当使用ViewPager时,android通常会在任一侧加载页面。该号码可以使用setOffscreenPageLimit()设置。您可以尝试将其设置为0或其他值,但未经过测试。

+0

谢谢,把新的Intent调用放在onPageChangeListener中修复了它。 – jdods 2013-03-27 16:49:18