如何使用ViewPager切换到其他布局? [英] How to Use ViewPager to switch to other layout?

查看:102
本文介绍了如何使用ViewPager切换到其他布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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.

这篇关于如何使用ViewPager切换到其他布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆