在ViewPager上替换片段在屏幕上显示空(空白)

时间:2013-05-04 03:02:10

标签: android android-fragments android-viewpager

我的应用有2个标签和4个片段。

片段1和2被片段3和4替换: 片段1 - >片段3 | Fragment2 - >片段4

对于Fragments的这种交换,我正在使用FragmentPagerAdapter。完成片段交换是因为应用程序在LogCat中没有显示错误,无论是崩溃还是关闭。但是,应用程序屏幕变为空白!只显示一个空白屏幕。我不明白。

任何人都可以帮我弄清楚错误吗?

按照寻呼机适配器的代码:

    <!-- language: lang-java -->
public class PagerAdapter extends FragmentPagerAdapter{
        private final Context mContext;
        private final FragmentManager mFragmentManager;
        private final TabHost mTabHost;
        private final ViewPager mViewPager;

        private List<String> mTabTags = new ArrayList<String>();
        private HashMap<Integer,Stack<String>> mFragmentsBack = new HashMap<Integer,Stack<String>>();
        private HashMap<String, Info> mFragments = new HashMap<String, Info>();

        private boolean mBackFragent = true;
        private int mTabPosition = 0;

        public PagerAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
            super(activity.getSupportFragmentManager());

            this.mContext = activity;
            this.mFragmentManager = activity.getSupportFragmentManager();

            this.mTabHost = tabHost;
            this.mTabHost.setOnTabChangedListener(tabChangeListener);

            this.mViewPager = pager;
            this.mViewPager.setOnPageChangeListener(viewPageChangeListener);
            this.mViewPager.setAdapter(this);
            this.mViewPager.setPageMargin(8);
            this.mViewPager.setCurrentItem(0);       

            this.mTabTags.clear();
            this.mFragments.clear();
            this.mFragmentsBack.clear();

            this.mBackFragent = true;
            this.mTabPosition = 0;
        }

        @Override
        public Fragment getItem(int position) {
            String tag = mTabTags.get(position);
            Info info = mFragments.get(tag);
            Fragment fragment = info.getFragment();
            if(fragment == null){
                Log.d("getItem","null: "+info.getClss().getName());
                fragment = Fragment.instantiate(mContext, info.getClss().getName(), info.getArgs());
                info.setFragment(fragment);
                mFragments.put(tag,info);
            }else{
                Log.d("getItem","instatiated "+info.getClss().getName());
            }
            return fragment;
        }

        @Override
        public int getItemPosition(Object object){

            ArrayList<Class<?>> positionNoneHack = new ArrayList<Class<?>>();
            for(Info info: mFragments.values())
            {
               positionNoneHack.add(info.getClss());
            }
            if(positionNoneHack.contains(object.getClass()))
            {
                return POSITION_UNCHANGED;
            }    
            return POSITION_NONE;
        }

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

        public void addTab(TabSpec tabSpec)
        {
          try
            {    
                this.mTabHost.setup();
                tabSpec.setContent(new TabContent(this.mContext));
                this.mTabHost.addTab(tabSpec);
                this.mTabTags.add(tabSpec.getTag());
                this.mFragmentsBack.put(this.mTabPosition, new Stack<String>());
                this.mTabPosition++;
                this.notifyDataSetChanged();
            }
         catch(Exception ex){
                ex.printStackTrace();}

        }

        public void addFragment(String tag, Class<?> clss, Bundle args){
            Info info = new Info(tag, clss, args);
            this.mFragments.put(tag,info);
        }

        public void replaceFragment(String tagOld, String tagNew, Bundle argsNew)
        {
          try
            {
               Log.d("replaceFragment","old: "+tagOld+", new: "+tagNew);
               Fragment oldFragment =  mFragments.get(tagOld).getFragment();
               if(oldFragment == null) 
               {
                  return;
               }
               int oldPosition = mTabTags.indexOf(tagOld);
               mTabTags.set(oldPosition, tagNew);

               if(this.mBackFragent)
               {
                   int location = mViewPager.getCurrentItem();
                   Stack<String> stackFragmentsBack = this.mFragmentsBack.get(location);
                   stackFragmentsBack.add(tagOld);
                   this.mFragmentsBack.put(location, stackFragmentsBack);
               }
               else
               {
                   this.mBackFragent = true;
               }
                   Fragment newFragment = this.getItem(oldPosition);
               if(argsNew != null)
               {
                   newFragment.setArguments(argsNew);
               }

                this.startUpdate(mViewPager);
                FragmentTransaction ft = mFragmentManager.beginTransaction();
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                //ft.hide(oldFragment);           
                ft.remove(oldFragment);
                ft.add(mViewPager.getId(), newFragment);
                ft.show(newFragment);
                ft.commit();
                mFragmentManager.executePendingTransactions();
                this.notifyDataSetChanged();
                this.finishUpdate(mViewPager);

            }
          catch(Exception ex){
                ex.printStackTrace();}

        }

        public boolean back()
        {
            try
              {
                int location = mViewPager.getCurrentItem();
                Stack<String> stackFragmentsBack = mFragmentsBack.get(location);
                String tagBack = stackFragmentsBack.pop();

                if(tagBack == null)
                {
                    return false;
                }

                String tagCurrent = mTabTags.get(location);
                this.mBackFragent = false;
                this.mFragmentsBack.put(location, stackFragmentsBack);
                this.replaceFragment(tagCurrent, tagBack, null);
                return true;
            }
           catch(Exception ex){
                ex.printStackTrace();
                return false; }

        }

        private final TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener(){
            @Override
            public void onTabChanged(String tabId) {
                int pos = mTabHost.getCurrentTab();
                mViewPager.setCurrentItem(pos);
            }};   

        private final ViewPager.OnPageChangeListener viewPageChangeListener = new ViewPager.OnPageChangeListener(){

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onPageSelected(int position) {
                TabWidget widget = mTabHost.getTabWidget();
                int oldFocusability = widget.getDescendantFocusability();
                widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
                mTabHost.setCurrentTab(position);//original code just with this line
                widget.setDescendantFocusability(oldFocusability);           
            }

        };   
    }

0 个答案:

没有答案