调用notifyDataSetChanged

时间:2015-10-06 17:43:28

标签: android android-viewpager fragment fragmentpageradapter

我正在尝试使用FragmentPagerAdapter更新视图寻呼机。每当我从我的适配器调用notifyDataSetChanged时,我都会收到一个强制关闭此错误"java.lang.IllegalStateException: Recursive entry to executePendingTransactions".

关于这方面的任何指示都会对我发布下面的代码有所帮助,我会在此之后发布2个关于此主题的其他2个问题的链接。更新此ViewPager一直让我头疼,所以任何建议都将受到赞赏。

Removing Tabs using FragmentPagerAdapterhttps://stackoverflow.com/questions/32834861/adding-and-removing-tabs-using-fragmentpageradapter

public void setTabsToSHow() {

    Toast.makeText(getActivity(), Integer.toString(listOfTitles.size()), Toast.LENGTH_LONG).show();

    mViewPager.getAdapter().notifyDataSetChanged();
}






class TabPageAdapter extends FragmentPagerAdapter {

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case JudgeActivity.JUDGE_MAIN:
                if (mMainFragment == null) {
                    mMainFragment = JudgeMainFragment.newInstance();
                }
                return mMainFragment;
            case JudgeActivity.JUDGE_VERDICTS:
                if (mVerdictFragment == null) {
                    mVerdictFragment = JudgeVerdictFragment.newInstance();
                }
                return mVerdictFragment;
            case JudgeActivity.JUDGE_CLASSIFY:
                if (mClassifyFragment == null) {
                    mClassifyFragment = JudgeClassifyFragment.newInstance();
                }
                return mClassifyFragment;
            case JudgeActivity.JUDGE_SIDEBAR:
                if (mSidebarFragment == null) {
                    mSidebarFragment = JudgeSidebarFragment.newInstance((SidebarCall) mActivity);
                }
                return mSidebarFragment;
        }
        return null;
    }

    @Override
    public int getCount() {
        return listOfTitles.size();
    }


    @Override
    public int getItemPosition(Object object) {
        Toast.makeText(getActivity(), "itempostioton",Toast.LENGTH_SHORT).show();
        return POSITION_NONE ;
    }


    @Override
    public CharSequence getPageTitle(int position) {
    //  return CONTENT[position % CONTENT.length].toUpperCase(Locale.US);

        //Notify data set changed can only be called from inside this class
        notifyDataSetChanged();
        return listOfTitles.get(position);
    }








}

1 个答案:

答案 0 :(得分:0)

notifyDataSetChanged();方法

中删除public CharSequence getPageTitle(int position)

其次,不要从case语句返回,而是分配给局部变量,最后返回片段。在每个案例块之后使用break