android 如何实现分片的滑动查看

hfsqlsce  于 2023-03-28  发布在  Android
关注(0)|答案(1)|浏览(180)

我正在尝试在一个片段中实现滑动!我的应用程序使用导航抽屉布局,我用抽屉改变视图。我显示的所有视图都是片段。现在,我想在一个特定的片段中实现水平滑动。我如何才能做到这一点?我的代码如下:

public class MybookingFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    final View Rootview = inflater.inflate(R.layout.mybooking, parent, false);
    ViewPager viewPager = (ViewPager) Rootview.findViewById(R.id.viewpager);
    FragmentManager fragmentManager = getChildFragmentManager();
    viewPager.setAdapter(new SlidingPagerAdapter(fragmentManager));
    return Rootview;
}

我的适配器代码在这里

public class SlidingPagerAdapter extends FragmentStatePagerAdapter {
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
public SlidingPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    Class fragmentClass = null;
    switch (position) {
        case 0:
            fragmentClass = UpcomingOrder.class;
        case 1:
            fragmentClass = RunningOrder.class;
            break;
        case 2:
            fragmentClass = CompletedOrder.class;
            break;
        default:
            fragmentClass = UpcomingOrder.class;
            break;
    }
    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fragment;
}

@Override
public int getCount() {
    // For this contrived example, we have a 100-object collection.
    return 3;
}

@Override
public CharSequence getPageTitle(int position) {
    return tabTitles[position];
}

}

mzillmmw

mzillmmw1#

试试下面的例子。我想你只是在问这个问题
Example 1
Example 2
Tabs swipe
你必须使用main fragment。在那个fragment中,你必须加载所有的子fragment(tab)。

相关问题