【问题标题】:Sliding tabs with ViewPager使用 ViewPager 滑动标签
【发布时间】:2015-04-16 08:39:27
【问题描述】:

我想实现一个带有滑动标签的 Google Play 商店风格的菜单。我紧密地实现了这个tutorial,但是在我的实现中,我通过 CursorLoader 启动异步数据加载,我在 PagerAdapter 内部类的 instantiateItem 方法中对其进行初始化。

问题是PagerAdapter调用了instantiateItem两次(从而实例化了两个tab对应的view),启动了两次异步进程,每次用户点击不同的tab,又启动了两次异步加载,造成巨大的无法控制的混乱。如何让 PagerAdapter 调用 instantiateItem 一次?

谢谢。

编辑:这是我的 PagerAdapter sublcas 的代码,用于实现教程:

class SamplePagerAdapter extends PagerAdapter {

    /**
     * @return the number of pages to display
     */
    @Override
    public int getCount() {
        return 5;
    }

    /**
     * @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) {
        CharSequence title = "default";
        switch (position) {
            case 0:
                title = "Happiness";
            break;

            case 1:
                title = "Intelligence";
            break;

            case 2:
                title = "Memory";
            break;

            case 3:
                title = "Personality";
            break;

            case 4:
                title = "Success & Failure";
            break;
        }

    return title;
    }
    // END_INCLUDE (pageradapter_getpagetitle)

    /**
     * Instantiate the {@link android.view.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
        ListView view = (ListView) getActivity().getLayoutInflater().inflate(R.layout.article_list,
                container, false);
        // Add the newly created View to the ViewPager
        container.addView(view);

        switch (position) {
            case 0:
                Intent intent1 = new Intent(getActivity(), ArticleService.class);
                intent1.putExtra(ArticleService.CATEGORY_EXTRA, 1);
                getActivity().startService(intent1);

                getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
                mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
                view.setAdapter(mArticleAdapter);

                Log.v("PagerAdapter", "Adapter Set");

                Log.v("PagerAdapter", "View added");

                break;
            case 1:
                Intent intent2 = new Intent(getActivity(), ArticleService.class);
                intent2.putExtra(ArticleService.CATEGORY_EXTRA, 2);
                getActivity().startService(intent2);

                getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
                mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
                view.setAdapter(mArticleAdapter);
                break;
            case 2:
                Intent intent3 = new Intent(getActivity(), ArticleService.class);
                intent3.putExtra(ArticleService.CATEGORY_EXTRA, 3);
                getActivity().startService(intent3);

                getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
                mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
                view.setAdapter(mArticleAdapter);
                break;
            case 3:
                Intent intent4 = new Intent(getActivity(), ArticleService.class);
                intent4.putExtra(ArticleService.CATEGORY_EXTRA, 4);
                getActivity().startService(intent4);

                getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
                mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
                view.setAdapter(mArticleAdapter);
                break;
            case 4:
                Intent intent5 = new Intent(getActivity(), ArticleService.class);
                intent5.putExtra(ArticleService.CATEGORY_EXTRA, 5);
                getActivity().startService(intent5);

                getLoaderManager().initLoader(ARTICLE_LOADER, null, KnowledgeFragment.this);
                mArticleAdapter = new ArticleAdapter(getActivity(), null, 0);
                view.setAdapter(mArticleAdapter);
                break;
        }

        // 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);
        Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
    }

}

我知道为了优化内存使用和平滑滚动,两次调用该方法是 PagerAdapter 的默认行为。本质上我是在问是否有办法改变它。

【问题讨论】:

  • 添加代码,您的代码应该与您所采取的示例有所不同。
  • @HarshaVardhan 查看编辑。
  • 添加您的自定义适配器类代码..
  • @HarshaVardhan 再次查看编辑。

标签: android android-cursorloader android-pageradapter pagerslidingtabstrip


【解决方案1】:

我使用 getItem 而不是 instantiateItem,见下文:

@Override
public Fragment getItem(int position) {
    return MyFrag.newInstance(position);
}

编辑:

getItem() 在 android.support.v4.app.FragmentPagerAdapter 中,应该与 Fragments 一起使用

然后您的片段可以单独处理加载。

【讨论】:

  • 在使用这种方法时,是否必须为当前标签页对应的每个“页面”定义一个片段?
  • 顺便说一句,我正在检查 PagerAdapter 文档,并且那里没有 getItem 方法。我不确定你是否理解我的问题。
  • 是的,对不起。我正在使用 android.support.v4.app.FragmentPagerAdapter,它需要每个项目的片段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
相关资源
最近更新 更多