如何使用ViewPager切换到其他布局?(How to Use ViewPager to swit

2019-09-21 16:15发布

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return getString(R.string.title_section1).toUpperCase();
            case 1: return getString(R.string.title_section2).toUpperCase();
            case 2: return getString(R.string.title_section3).toUpperCase();
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    public DummySectionFragment() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        Bundle args = getArguments();
        textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
        return inflater.inflate(R.layout.fragment_content1, container, false);
    }
}
}

上面的代码是从SDK为ViewPager模板,它只是把文字1至3时,我刷卡从一个页面一个页面。 现在的问题是我如何可以替换的文字,把我自己的布局XML时,我刷它,是否有可能使用WebView此功能。 例如:当活动加载它去到google.com然后当我刷到其他页面它每次我刷卡时间改变的WebView的网址是什么? 希望可以有人帮帮我...

Answer 1:

你必须创建一个从android.support.v4.app.Fragment延长就像类DummySectionFragment类。 然后,进入方法的getItem(int i)以你应该实例希望为每个指标和方法getCount将(每类)必须返回你实例化碎片的数量。



Answer 2:

viewpager.setCurrentItem(index) ,其中index是所需片段索引。



文章来源: How to Use ViewPager to switch to other layout?