【问题标题】:Android: Load activity or intent from fragment pageAndroid:从片段页面加载活动或意图
【发布时间】:2013-03-27 05:42:08
【问题描述】:

我正在尝试从片段中加载活动。我只是使用了基本的 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;
        }


    }

【问题讨论】:

    标签: android android-activity fragment


    【解决方案1】:

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

    【讨论】:

    • 谢谢,将新的 Intent 调用放在 onPageChangeListener 中修复了它。
    猜你喜欢
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多