在Android中的Fragment中添加ViewPager

时间:2017-01-03 04:39:05

标签: android android-viewpager fragment

我有两个片段:FirstFragmentSecondFragmentFirstFragment包含ViewPager,其中包含三个页面(三个片段)。在onCreate的activty中,我将FirstFragment添加到主布局,并且正常显示三个页面。

问题是:当我将FirstFragment替换为SecondFragment,然后回归FirstFragment三个页面消失。然后我向前和向后滚动ViewPager,页面显示但只显示第一页和第三页,第二页消失。

此处FirstFragment

package com.example.bkmsx.afragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FirstFragment extends Fragment {
    ViewPager mViewPager;
    static MainActivity mainActivity;
    public static FirstFragment newInstance(MainActivity activity){
        mainActivity = activity;
        return new FirstFragment();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first_fragment, null);
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
        ViewPagerAdapter adapter = new ViewPagerAdapter(mainActivity.getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        return view;
    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            String text = "";
            switch (position) {
                case 0: text = "First Page";
                    break;
                case 1: text = "Second Page";
                    break;
                case 2: text = "Third Page";
                    break;
            }
            return TextFragment.newInstance(mainActivity, text);
        }

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

SecondFragment

package com.example.bkmsx.afragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SecondFragment extends Fragment {
    static MainActivity mainActivity;

    public static SecondFragment newInstance(MainActivity activity) {
        mainActivity = activity;
        return new SecondFragment();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        TextView textView = new TextView(mainActivity);
        textView.setText("New Fragment");
        return textView;
    }
}

有什么建议吗?提前谢谢。

3 个答案:

答案 0 :(得分:2)

FirstFragment中,使用此代码并尝试

public View view; // declare view as global variable

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if(view == null){
              view = inflater.inflate(R.layout.first_fragment, null);
              mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
              ViewPagerAdapter adapter = new ViewPagerAdapter(mainActivity.getSupportFragmentManager());
              mViewPager.setAdapter(adapter);
        }
        return view;
    }

由于Divyesh评论有很好的建议,还需要添加

mViewPager.setOffscreenPageLimit(3);

答案 1 :(得分:0)

使用ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager() );代替mainActivity.getSupportFragmentManager()

答案 2 :(得分:0)

试试这段代码希望有所帮助

        final ViewPager viewPager = (ViewPager) findViewById(R.id.container);
    final PagerAdapter adapter = new PaginationAdapter(getSupportFragmentManager(),tabLayout.getTabCount());

    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    viewPager.setOffscreenPageLimit(3);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });