1

我正在尝试从片段中加载活动。我只是使用了基本的 android 示例代码并添加了一些不同的布局。下面的代码部分工作,但是,当滑动到第 3 页而不仅仅是第 4 页时,意图/活动开始。我认为从第 3 页滑动到第 2 页时它也开始了一次。我认为这与 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;
        }


    }
4

1 回答 1

1

如果您使用的是ViewPagerwith Fragments,我认为ViewPager.SimpleOnPageChangeListener应该可以帮助您。查看onPageSelected函数。就像你猜到的,当使用 ViewPager 时,android 通常会在两侧加载页面。可以使用setOffscreenPageLimit()设置此数字。您可以尝试将其设置为 0 或其他值,未经测试。

于 2013-03-27T06:03:20.333 回答