如何使用ViewPager切换到其他布局?如何使用、切换到、布局、ViewPager

2023-09-07 10:17:47 作者:喜欢就要抢

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

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

The code above is the template from the SDK for ViewPager, it only put text 1 to 3 when I swipe from one page to one page. The question is how can I replace that text and put my own layout xml when I swipe it, and is it possible to use WebView with this feature. For example: when the activity loads it goes to google.com then when i swipe to other page it change the url of the WebView each time I swipe? Hope someone can help me...

推荐答案

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

You have to create classes that extend from android.support.v4.app.Fragment just like the class DummySectionFragment. Then, into the method getItem(int i) you should instantiate each class you want for each index and the method getCount() must return the number of Fragments you are instantiating.