标签页内的ViewPager

时间:2018-08-17 11:38:45

标签: android android-fragments android-viewpager

我有3个标签,每个标签共享一个片段。但是此片段包含一个viewPager。因此,所有三个选项卡将具有一个带有viewPager的UI,而viewPager具有三个不同的片段。遵循可用的示例之后,我设法用单个片段实现了Tab布局。但是,当我尝试在此片段内实现viewPager时,我遇到了问题。选项卡滚动不流畅,并且没有移动到第三个选项卡。我也看不到viewPager片段。

具有tablayout实现的主要片段:

public class DashboardFragment extends android.support.v4.app.Fragment {
    Activity mActivity;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    private static final String TAG = "DashboardFragment";

    public DashboardFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dashboard_fragment, container, false);
        tabLayout = (TabLayout)view.findViewById(R.id.tabs);
        viewPager = (ViewPager)view.findViewById(R.id.viewpager);


        tabLayout.addTab(tabLayout.newTab().setText("Yesterday"));
        tabLayout.addTab(tabLayout.newTab().setText("Last Week"));
        tabLayout.addTab(tabLayout.newTab().setText("This Month"));
        setupViewPager(viewPager);


        return view;
    }

    private void setupViewPager(ViewPager viewPager) {
       DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(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) {

            }
        });
    }
}

DashboardPagerAdapter:

public class  DashboardTabPagerAdapter extends FragmentStatePagerAdapter {

    private int mNoOfTabs;



    public DashboardTabPagerAdapter(FragmentManager fm, int tabCount) {
        super(fm);
        mNoOfTabs=tabCount;

    }
    @Override
    public Fragment getItem(int position) {
        DashboardYesterdayFragment fragment = new DashboardYesterdayFragment();
        Bundle args = new Bundle();

        switch (position) {
            case 0:
                args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_YESTERDAY);
                break;
            case 1:
                args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_LAST_WEEK);
                break;
            case 2:
                args.putSerializable(AppConstants.TAB_DASHBOARD_TYPE, AppConstants.TAB_DASHBOARD_THIS_MONTH);
                break;

            default:
                return null;
        }
        fragment.setArguments(args);
        return fragment;
    }

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

}

TabFragment包含childViewPager:

 public class DashboardYesterdayFragment  extends android.support.v4.app.Fragment {
        private String title;
        private Bundle bundle;
        ViewPager mViewPager;
        public DashboardYesterdayFragment(){

        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {
                bundle = getArguments();

            }

        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.dashboard_yesterday_layout, container, false);
             mViewPager = view.findViewById(R.id.vpPager);
            setupViewPager(mViewPager);

            return view;
        }

        private void setupViewPager(ViewPager viewPager) {
            DashboardViewPagerAdapter adapter = new DashboardViewPagerAdapter( getChildFragmentManager(),bundle);

            mViewPager.setAdapter(adapter);
            mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            mViewPager.setPageMargin(24);

    }

ChildViewPagerAdapter:

public class DashboardViewPagerAdapter extends FragmentStatePagerAdapter {

    private static int NUM_ITEMS = 3;
    private Bundle bundle;

    public DashboardViewPagerAdapter(FragmentManager fm, Bundle bundle) {
        super(fm);
        this.bundle=bundle;
    }


    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return IncidentsFragment.newInstance(bundle);
            case 1:
                return VehicleUtilizationFragment.newInstance(bundle);
            case 2:
                return FleetPerformanceFragment.newInstance(bundle);
            default:
                return null;
        }
    }

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

ChildViewPager第一个片段:

public class IncidentsFragment extends Fragment {

    private TextView mOverSpeeding,mGeoFence,mIdlingCase,mFuelFilling,mMosetIncidences, mLeastIncidences;

    public static IncidentsFragment newInstance(Bundle bundle) {
        IncidentsFragment fragmentFirst = new IncidentsFragment();
        Bundle args = new Bundle();

        args.putBundle("bundle", bundle);


        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.incident_layout, container, false);
       initViews(view);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initViews(View view) {
        mOverSpeeding = (TextView)view.findViewById(R.id.over_speeding_value);
        mGeoFence = (TextView)view.findViewById(R.id.geofence_value);
        mIdlingCase = (TextView)view.findViewById(R.id.idling_value);
        mFuelFilling = (TextView)view.findViewById(R.id.fuel_value);
        mMosetIncidences = (TextView)view.findViewById(R.id.vehicle_reg_no);
        mLeastIncidences = (TextView)view.findViewById(R.id.least_incidences_value);
    }
}

我很困惑是怎么回事。

1 个答案:

答案 0 :(得分:0)

更改此:

DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),tabLayout.getTabCount());

对此:

DashboardTabPagerAdapter adapter = new DashboardTabPagerAdapter(((AppCompatActivity)getActivity()).getChildFragmentManager(),tabLayout.getTabCount());