【问题标题】:Getting each tab of SlidingTabsBasicFragment to display different content让 SlidingTabsBasicFragment 的每个选项卡显示不同的内容
【发布时间】:2015-02-08 16:41:35
【问题描述】:

使用 Android Developer 示例中的 SlidingTabsBasicFragment 示例,我有 3 个选项卡,我想在其中显示不同的内容。当前的问题是,在单击或滑动到 3 个选项卡中的任何一个时,它只是加载相同的默认 pager_item .xml,显示“Page”和“Page Number”

我知道我应该让每个选项卡完全加载不同的 xml。哪位好心人可以告诉我如何做到这一点?

class SamplePagerAdapter extends PagerAdapter {

    /**
     * Sets the Titles for the various tabs
     */

    private String[] titles={"Browse", "Activity", "Me"};
    /**
     * @return the number of pages to display
     */
    @Override
    public int getCount() {
        return 3;
    }

    /**
     * @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
     * same object as the {@link View} added to the {@link ViewPager}.
     */
    @Override
    public boolean isViewFromObject(View view, Object o) {
        return o == view;
    }

    // BEGIN_INCLUDE (pageradapter_getpagetitle)
    /**
     * Return the title of the item at {@code position}. This is important as what this method
     * returns is what is displayed in the {@link SlidingTabLayout}.
     * <p>
     * Here we construct one using the position value, but for real application the title should
     * refer to the item's contents.
     */
    @Override
    public CharSequence getPageTitle(int position) {

        return titles[position];

    }


    // END_INCLUDE (pageradapter_getpagetitle)

    /**
     * Instantiate the {@link View} which should be displayed at {@code position}. Here we
     * inflate a layout from the apps resources and then change the text view to signify the position.
     */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // Inflate a new layout from our resources

        View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_main_menu,
                container, false);

        // Add the newly created View to the ViewPager
        container.addView(view);

        // Retrieve a TextView from the inflated View, and update it's text
        // TextView title = (TextView) view.findViewById(R.id.item_title);
        // title.setText(String.valueOf(position + 1));

        // Return the View
        return view;
    }

    /**
     * Destroy the item from the {@link ViewPager}. In our case this is simply removing the
     * {@link View}.
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);

    }

}

}

我知道这里必须做一些事情才能达到结果。

        @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // Inflate a new layout from our resources

        View view = getActivity().getLayoutInflater().inflate(R.layout.pager_item,
                container, false);

        // Add the newly created View to the ViewPager
        container.addView(view);

        // Retrieve a TextView from the inflated View, and update it's text
        TextView title = (TextView) view.findViewById(R.id.item_title);
        title.setText(String.valueOf(position + 1));

        // Return the View
        return view;
    }

【问题讨论】:

标签: android


【解决方案1】:

我和你有同样的问题,我遵循了一个使用片段到每个选项卡的教程(真的是一个简单的教程)。

看一看:Tabs tutorial

如果您想获得 Material Design 风格,请查看此库:Material Design Tabs

【讨论】:

    猜你喜欢
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2021-07-19
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2022-11-29
    相关资源
    最近更新 更多