ViewPager 内部的 Android ViewPager

标签 android android-fragments

我目前面临将 ViewPager 放入另一个 ViewPager 中的问题,两者都可滑动。我成功创建了我的第一个 ViewPager,它包含三个页面,中间一个是第二个(内部)ViewPager,其中包含 3 个页面。

| ACTIVITY-1 | VIEWPAGER | ACTIVITY-1 |
         | AC1 | AC2 | AC3 |

父 ViewPager 在滑动时工作正常,但内部 ViewPager 无法显示内部 Activity ,因此也不可滑动。

父ViewPager:

public class MainSlider extends ActionBarActivity {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

}

关联的 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 tools:context="com.example.slidingapp.MainSlider" >

    <android.support.v4.view xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

适配器 ViewPager :

    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;
    }
}

还有 fragment :

   public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    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;
        Bundle args = getArguments();

        switch (args.getInt(ARG_SECTION_NUMBER)) {
        case 1:
             rootView = inflater.inflate(R.layout.activity_first_page, container, false);
            break;
        case 2:
             rootView = inflater.inflate(R.layout.activity_second_swap, container, false);
            break;
        case 3:
             rootView = inflater.inflate(R.layout.activity_third_page, container, false);
            break;
        default:
             rootView = inflater.inflate(R.layout.fragment_main_slider, container, false);
            break;
        }

        return rootView;
    }
}    

子 fragment 的代码与父 fragment 的代码相同,请为子 ViewPager 提供任何解决方案,因为它无法在其中加载任何 Activity 。

最佳答案

在嵌套 viewPager 的情况下,如果您的子 viewpager 不可见或未正确滚动,则使用 getChildFragmentAdapter()

初始化您的子 viewpager(pagerAdaper 类)

关于ViewPager 内部的 Android ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25785837/

相关文章:

java - 使用SectionStatPageAdapter和bundle将数据从 Activity 发送到多个 fragment

android - 实现视频和图像的幻灯片

android - 为什么具有 NET_CAPABILITY_INTERNET 功能的 WifiNetworkSpecifier 总是返回 onUnavailable?

java - 使 NotificationListenerService 保持 Activity 状态的解决方法

android - 在自定义 AdapterView 中实现 onMeasure() 和 onLayout() 的正确方法

Android - 底部工具栏 - 如何在顶部显示边框线

android - Android 中的 EditText 验证

android - Textview 在第一次点击时为空,但在第二次点击时更新

android - 如何将数据从 Adapter 传递到 Fragment?

android - 由于 fragment 事务多次调用 Oncreate