简体   繁体   English

带有不同页面布局的viewpager的操作栏选项卡

[英]Action bar tabs with viewpager with different page layouts

Hi Im new in android and I want ask for help. 嗨,我是新的Android,我想要求帮助。 Im creating new project in android studio and I use navigation Action bar tabs with viewpager. 我正在android studio中创建新项目,我使用带有viewpager的导航操作栏选项卡。 Android studio generate this code. Android studio生成此代码。 I know work with one activity atc but now I want to learn work with swipe layouts with tabs. 我知道使用一个活动atc,但现在我想学习带有选项卡的滑动布局。 My question is: Is possible do more layouts with different items inside this? 我的问题是:是否可以在此内部使用不同的项目进行更多布局? For example, now I have on every fragment one textview and when I swipe I see any text on every page. 例如,现在我在每个片段上都有一个textview,当我滑动时,我会在每个页面上看到任何文本。 But I need for example on page(tab1) layout with textview, on tab2 I need layout edittext, on tab3 I need layout with image. 但我需要在页面(tab1)布局上使用textview,在tab2上我需要布局edittext,在tab3上我需要布局与图像。 Is this possible with this? 这有可能吗? Because I can change text but now I have the same layout for all tabs. 因为我可以更改文本,但现在我对所有选项卡都有相同的布局。

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        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;
    }
}
public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";
    public static PlaceholderFragment newInstance(int sectionNumber) {
        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) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
        return rootView;
    }
}

} }

In this case you can define 3 separate fragment classes. 在这种情况下,您可以定义3个单独的片段类。 Then you can return them appropriately in getItem of your SectionsPagerAdapter : 然后你可以在SectionsPagerAdapter getItem中适当地返回它们:

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return new YourFragmentClass1();
        case 1:
            return new YourFragmentClass2();
        case 2:
            return new YourFragmentClass3();
    }
} 

To elaborate on Szymon's answer. 详细说明Szymon的答案。

Step 1 Delete the inner class PlaceHolderFragment Step 2 : Create your fragment classes, with corresponding layouts. 步骤1删除内部类PlaceHolderFragment 步骤2 :使用相应的布局创建片段类。 For example, ImageViewFragment (which extends Fragment of course) and then image_view_fragment_layout . 例如, ImageViewFragment (当然扩展Fragment)然后是image_view_fragment_layout Step 3 Have ImageViewFragment 's onCreateView method inflate image_view_fragment_layout . 步骤3让 ImageViewFragmentonCreateView方法膨胀image_view_fragment_layout

public ImageViewFragment extends Fragment {
  ...
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.image_view_fragment_layout, 
                                                    container, false);
    return rootView;
  }

Follow these steps for as many tabs you want to add. 对于要添加的多个选项卡,请按照以下步骤操作。

Then in the getItem() method follow Szymon's answer. 然后在getItem()方法中按照Szymon的回答。 Remember, the tab you want to appear first will be in position 0 and so on. 请记住,首先要显示的选项卡将位于0位置,依此类推。

One more Important thing, in your getCount method: 在您的getCount方法中,还有一件重要的事情:

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

the number returned should reflect the number of tabs you have added. 返回的数字应反映您添加的标签数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM