如何使用ViewPager为标签设置不同的内容?

时间:2014-05-11 11:32:42

标签: android gridview android-fragments tabs android-viewpager

有没有办法使用ViewPager为标签设置不同的内容?我的意思是而不为每个标签创建片段。

所以我有一个片段,它应用于每个标签。但每个的内容应该不同。在我的情况下,所有3个标签都有相同的项目。

这是我添加标签的方式:

    @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Here we load the xml layout we created above
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

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

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
    {
        @Override
        public void onPageSelected(int position)
        {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.


    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++)
    {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.

        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

每个标签都有此gridview布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:numColumns="2" />
</FrameLayout>

我有一个适配器,其中项目被添加到此网格中:

private class MyAdapter extends BaseAdapter
{
    private List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;

    public MyAdapter(Context context)
    {
        inflater = LayoutInflater.from(context);

        items.add(new Item("Item1", R.drawable.picture1));
        items.add(new Item("Item2", R.drawable.picture2));
    }

private class Item
    {
        final String name;
        final int picId;

        Item(String name, int drawableId)
        {
            this.name = name;
            this.picId = drawableId;
        }
    }

是的,还有PlaceholderFragment分配了这个gridview:

public class PlaceholderFragment extends Fragment
{
    public PlaceholderFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        // Here we inflate the layout we created above
        GridView gridView = (GridView) rootView.findViewById(R.id.gridview);
        gridView.setAdapter(new MyAdapter(MainActivity.this.getApplicationContext()));
        return rootView;
    }
}

0 个答案:

没有答案
相关问题