Implementation of the setTag() and findViewWithTag() in instantiateItem() of the FragmentStatePagerAdapter

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

Referencing the following answer, I tried to unimplement what I was currently doing in a project, i.e., using the following code:

@Override public int getItemPosition(Object object) {     return POSITION_NONE; } 

Because due to the current implementation all the pages in my ViewPager was reloaded again. The reason as to why I implemented this in the 1st place was to reload a fragment (which was dynamic according to the user input).

After reading the reference, I got a vague idea on what I was supposed to do, I overrode the instantiateItem() method, but in my attempt to setTag() I got confused as to how I was to implement this. I did try the following:

@Override public Object instantiateItem(ViewGroup viewgroup, int position) {     // TODO Auto-generated method stub     View viewA = viewgroup.getChildAt(0);     viewA.setTag("FragmentA");     View viewB  = viewgroup.getChildAt(1);     viewB.setTag("FragmentB");     View viewC = viewgroup.getChildAt(2);     viewC.setTag("FragmentC");     return super.instantiateItem(viewgroup, position);  } 

And in my interface implementation I called the following:

        PlayingFragment fragment = new PlayingFragment();         fragment.setArguments(element);         getSupportFragmentManager().beginTransaction().add(fragment,"NowPlaying").commit();         adapter.notifyDataSetChanged();         pager.findViewWithTag("FragmentC"); 

The current implementation gives me a NullPointerException

at : viewA.setTag("FragmentA");

Can someone help me implement this setTag() and findViewWithTag() implementation ?

回答1:

When you were calling viewA.setTag("FragmentA"), the viewgroup has not been initialized. This is the direct cause of this exception.

To avoid this issue, you need set tag after the viewGroup has been set up, i.e., you have inflated a view layout or created a fragment.

For example, the default code for instantiateItem of FragmentStatePagerAdapter is as follows.

@Override public Object instantiateItem(ViewGroup container, int position) {     // If we already have this item instantiated, there is nothing     // to do.  This can happen when we are restoring the entire pager     // from its saved state, where the fragment manager has already     // taken care of restoring the fragments we previously had instantiated.     if (mFragments.size() > position) {         Fragment f = mFragments.get(position);         if (f != null) {             return f;         }     }      if (mCurTransaction == null) {         mCurTransaction = mFragmentManager.beginTransaction();     }      Fragment fragment = getItem(position);     if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);     if (mSavedState.size() > position) {         Fragment.SavedState fss = mSavedState.get(position);         if (fss != null) {             fragment.setInitialSavedState(fss);         }     }     while (mFragments.size() <= position) {         mFragments.add(null);     }     fragment.setMenuVisibility(false);     fragment.setUserVisibleHint(false);     mFragments.set(position, fragment);     mCurTransaction.add(container.getId(), fragment);      return fragment; } 

You can set the tag in the getItem method when creating your fragment / view.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!