在setTag()的实现和FragmentStatePagerAdapter的findViewWithTag()在instantiateItem() [英] Implementation of the setTag() and findViewWithTag() in instantiateItem() of the FragmentStatePagerAdapter
问题描述
引用下面的回答,我试图unimplement我目前在做的一个项目,即使用以下code:
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;
}
由于由于目前实施我的 ViewPager
再次重装所有页面。究其原因,为什么我实现了这个在第1位的是重新加载一个片段(根据用户输入这是动态的)。
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).
阅读引用后,我得到了我应该做一个模糊的想法,我推翻了 instantiateItem()
方法,但在我试图 setTag()
我糊涂了,我怎么是实现这一点。我曾尝试以下内容:
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");
目前的实现给我的 NullPointerException异常
The current implementation gives me a NullPointerException
在 viewA.setTag(碎裂);
有人可以帮助我实现这个 setTag()
和 findViewWithTag()
的实施?
Can someone help me implement this setTag()
and findViewWithTag()
implementation ?
推荐答案
在分别致电viewA.setTag(碎裂)时,ViewGroup中尚未初始化。这是此异常的直接原因。
When you were calling viewA.setTag("FragmentA"), the viewgroup has not been initialized. This is the direct cause of this exception.
要避免这个问题,你需要的ViewGroup已经设置了之后,也就是,你已经膨胀的视图布局或创建的片段集标记。
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.
例如,默认的code为FragmentStatePagerAdapter的instantiateItem如下。
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;
}
您可以创建片段/视图时,在方法的getItem设置标签。
You can set the tag in the getItem method when creating your fragment / view.
这篇关于在setTag()的实现和FragmentStatePagerAdapter的findViewWithTag()在instantiateItem()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!