带有多个片段类的 Android ViewPager,每个片段类都带有单独的布局文件

Android ViewPager w/ multiple Fragment classes each w/ separate layout file(带有多个片段类的 Android ViewPager,每个片段类都带有单独的布局文件)
本文介绍了带有多个片段类的 Android ViewPager,每个片段类都带有单独的布局文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用使用 android.support.v4.view.ViewPager 类.对于 ViewPager 中的每一页,我希望有一个片段,每个片段都有自己的布局文件.每个片段都在其自己的文件中定义.我很难实现这一点.为了澄清,有:

My app uses the android.support.v4.view.ViewPager class. For each page in the ViewPager, I want there to be one Fragment, each with its own layout file. Each Fragment is defined in its own file. I am having a hard time implementing this. To clarify, there is:

1 ViewPager
4 个片段文件 - FragmentOne.class、FragmentTwo.class、FragmentThree.class、FragmentFour.class
4 个 XML 布局文件 - frag_one.xml、frag_two.xml、frag_three.xml、frag_four.xml

1 ViewPager
4 Fragment Files - FragmentOne.class, FragmentTwo.class, FragmentThree.class, FragmentFour.class
4 XML Layout Files - frag_one.xml, frag_two.xml, frag_three.xml, frag_four.xml

我看到的例子只处理一个布局文件.

The examples that I have seen only deal with one layout file.

这是我的 FragmentActivity 所在的 ViewPager:

Here is my FragmentActivity where the ViewPager is held:

public class Main extends FragmentActivity {
    MyAdapter mAdapter;
    ViewPager mPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Fragment getItem(int position) {
            return EditorFragment.newInstance(position);
        }
    }

}

我的主要问题是:如何让适配器 MyAdapter 识别四个单独的 Fragment 文件并使用 ViewPager 在它们之间顺利分页?我应该对 getItem() 进行哪些修改?

My main question is: How do I get the adapter, MyAdapter, to recognize the four separate Fragment files and smoothly page across them with ViewPager? What kind of modifications should I make to getItem()?

感谢任何帮助.

所以,我尝试了 lulumeya 的建议并想出了这个:

So, I tried what lulumeya suggested and came up with this:

编辑 2:是的,那些 break 语句修复了它.现在可以了.感谢所有帮助过的人!现在我有一个很好的隔离,谢谢.

EDIT 2: Yes, those break statements fixed it. It works now. Thanks to everyone who helped! Now I have a nice segregation, thanks.

    @Override
    public Fragment getItem(int position) {
        Fragment f = new Fragment();

        switch (position) {
        case 0:
            f = FragmentOne.newInstance(position);
                    break;
        case 1:
            f = FragmentTwo.newInstance(position);
                    break;
        case 2:
            f = FragmentThree.newInstance(position);
                    break;
        case 3:
            f = FragmentFour.newInstance(position);
                    break;
        }

        return f;
    }

应用程序启动,这始终是一个好兆头,但卡在布局四上.当我向右翻页时,它也是布局四,适用于所有页面.我肯定会为每个 Fragment 更改充气机中的布局.我仍将继续努力.@lulumeya - 这是你的意思吗?

The app lauches, which is always a good sign, but is stuck on layout four. When I page to the right it is also layout four, for all pages. I was sure to change the layout in the inflater for each Fragment. I am still going to work on this. @lulumeya - Is this what you meant?

推荐答案

只需在 getItem(int position) 中按位置返回正确的 Fragment 实例即可.用switch~case,很简单.

Just return proper Fragment instance in getItem(int position) by position. use switch~case, It's easy.

这篇关于带有多个片段类的 Android ViewPager,每个片段类都带有单独的布局文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)