Android的分页活动:与ViewPager操作栏标签:不同的布局为每个标签 [英] Android Tabbed Activity: Action Bar Tabs with ViewPager: different layout for each tab

查看:290
本文介绍了Android的分页活动:与ViewPager操作栏标签:不同的布局为每个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一共停电,我不熟悉的片段工作。

我与Eclipse助手创建了一个新的Andr​​oid项目:

在这里创建的默认类的一个片段:

  / **
 * A {@link FragmentPagerAdapter}返回对应片段
 *章节/标签/页面。
 * /
公共类SectionsPagerAdapter扩展FragmentPagerAdapter {    公共SectionsPagerAdapter(FragmentManager FM){
        超(FM);
    }    @覆盖
    公共片段的getItem(INT位置){
        //的getItem被称为实例化片段为给定的页面。
        //返回一个PlaceholderFragment(定义为静态内部类
        //下面)。
        返回PlaceholderFragment.newInstance(位置+ 1);
    }    @覆盖
    公众诠释的getCount(){
        //显示3总页​​数。
        返回3;
    }    @覆盖
    公共CharSequence的getPageTitle(INT位置){
        区域设置L = Locale.getDefault();
        开关(位置){
        情况下0:
            返回的getString(R.string.title_section1).toUpperCase(升);
        情况1:
            返回的getString(R.string.title_section2).toUpperCase(升);
        案例2:
            返回的getString(R.string.title_section3).toUpperCase(升);
        }
        返回null;
    }
}/ **
 *包含一个简单视图中的占位符片段。
 * /
公共静态类PlaceholderFragment扩展片段{
    / **
     *片段参数重新presenting本节号
     *片段。
     * /
    静态INT秒;
    私有静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;    / **
     *返回该片段对于给定部分数的新实例。
     * /
    公共静态的newInstance PlaceholderFragment(INT sectionNumber){
        秒= sectionNumber;
        Log.i(ARG_SECTION_NUMBER,+秒);
        PlaceholderFragment片段=新PlaceholderFragment();
        捆绑ARGS =新包();
        args.putInt(ARG_SECTION_NUMBER,sectionNumber);
        fragment.setArguments(参数);
        返回片段;
    }    公共PlaceholderFragment(){
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        INT JJJ =秒;        Log.i(onCreateView,+秒);
        查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,
                假);
        返回rootView;
    }
}

SectionsPagerAdapter用作适配器,并返回片段
3个片段都具有相同的布局

  fragment_main.xml

如何能分配到3个片段不同的布局?

在PlaceholderFragment包含一个捆绑的SECTION_NUMBER:

  args.putInt(ARG_SECTION_NUMBER,sectionNumber);

我可以使用此信息为determing至极布局在每个片段中显示?

  //创建一个将返回一个片段为三个的适配器
    //活性的主要部分。
    mSectionsPagerAdapter =新SectionsPagerAdapter(
            getSupportFragmentManager());    //设置的ViewPager与部分适配器。
    mViewPager =(ViewPager)findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);


解决方案

您需要让不同类别的每一页。一个页面从片段延伸。您只需加载不同的布局XML为每个片段。

 公共类FirstFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    查看rootView = inflater.inflate(R.layout< yourxmlhere>中容器,
            假);
    返回rootView;
}}

您知道有片段,但你仍然适配器需要知道哪些页面(位置)是片段。这在下面的函数决定:

  @覆盖
公共片段的getItem(INT位置){
    开关(位置){
    情况下0:
    //第1页
    返回新FirstFragment();
    打破;    情况1:
    //第2页
    返回新SecondFragment();
    打破;
    默认:
    //这个页面不存在
    返回null;
}

请确保您已设置页面的正确数量!

  @覆盖
公众诠释的getCount(){
    //页面的数量适配器知道
    返回< youramountofpages取代;
}

这应该让你和运行。

编辑:您可以只删除整个placeholderfragment类。它不再需要。

I have a total blackout, I am not familiar to work with fragments.

I created with the eclipse assistant a new Android project:

here a snippet of the created default class:

    /**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

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

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    static int sec;
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        sec = sectionNumber;
        Log.i("ARG_SECTION_NUMBER"," "+sec);
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        int jjj = sec;

        Log.i("onCreateView"," "+sec);
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

the SectionsPagerAdapter is used as an adapter and is returning Fragments the 3 fragments are having all the same layout

fragment_main.xml

how can different layouts assigned to the 3 fragments?

the PlaceholderFragment contains a Bundle with the section_number:

args.putInt(ARG_SECTION_NUMBER, sectionNumber);

can I use this Info for determing wich layout to show in each fragment?

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

解决方案

You need to make different classes for each page. A page extends from a Fragment. You can just load a different layout-xml for every fragment.

public class FirstFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {


    View rootView = inflater.inflate(R.layout.<yourxmlhere>, container,
            false);
    return rootView;
}

}

You know have the fragment, but your adapter still needs to know which page (position) is which fragment. This is decided in the following function:

@Override
public Fragment getItem(int position) {
    switch (position){
    case 0:
    //page 1
    return new FirstFragment();
    break;

    case 1:
    //page 2
    return new SecondFragment();
    break;
    default:
    //this page does not exists
    return null;
}

Make sure you have set the correct amount of pages!

@Override
public int getCount() {
    //the amount of pages your adapter knows
    return <youramountofpages>;
}

This should get you up and running.

EDIT: You can just delete the whole placeholderfragment class. It is not needed anymore.

这篇关于Android的分页活动:与ViewPager操作栏标签:不同的布局为每个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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