我有一个包含 ViewPager 的片段.ViewPager 与包含一组片段的适配器相关联.
I have a fragment which contains a ViewPager. The ViewPager is associated with an adapter that contains a set of fragments.
在加载父片段时,我遇到了一个 IllegalStateException
消息:java.lang.IllegalStateException: Recursive entry to executePendingTransactions
.
Upon loading the parent fragment, I am met with an IllegalStateException
with the message: java.lang.IllegalStateException: Recursive entry to executePendingTransactions
.
一些研究使我得出结论,系统无法在另一个片段中显示片段,但是似乎有迹象表明使用 ViewPager 可以做到这一点(ViewPager 与其他片段一起使用的错误).
Some research has led me to the conclusion that the system is unable display fragments within another fragment, HOWEVER there seems to be some indication that it is possible to do exactly this with the use of a ViewPager (A bug in ViewPager using it with other fragment).
事实上,如果我向我的父片段添加一个按钮,该按钮在按下时在我的 ViewPager 上调用 mPager.setAdapter(mAdapter)
,则 ViewPager 会成功加载.这并不理想.
In fact, if I add a button to my parent fragment which calls mPager.setAdapter(mAdapter)
on my ViewPager when pressed, the ViewPager successfully loads. This is not ideal.
那么问题必须与片段生命周期有关.因此,我的问题是:有没有其他人找到解决这个问题的方法,如果有,如何解决?
The issue then must be related to the fragment lifecycle. My question therefore, is this: Has anybody else found a way around this issue, and if so, how?
有什么方法可以将 ViewPager 上的适配器设置延迟到片段事务之后?
Is there some way to delay settings the adapter on the ViewPager until after the fragment transaction?
这是我的父片段代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.team_card_master, container, false);
mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);
final Button button = (Button)mView.findViewById(R.id.load_viewpager_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setAdapter(mAdapter);
button.setVisibility(View.GONE);
}
});
mAdapter = new ViewPagerAdapter(getFragmentManager());
// mViewPager.setAdapter(mAdapter);
return mView;
}
还有适配器:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
if (mCursor == null) return 0;
else return mCursor.getCount();
}
@Override
public Fragment getItem(int position) {
Bundle b = createBundle(position, mCursor);
return TeamCardFragment.newInstance(b);
}
}
使用 AsyncTask 设置 viewPager 的适配器.这个对我有用.asyncTask 是为了使原始片段完成它的转换.然后我们继续 viewPager 片段,基本上是为了避免递归.
use AsyncTask to set the adapter for viewPager. It works for me. The asyncTask is to make the original fragment complete it's transition. and then we proceed with viewPager fragments, basically to avoid recursion.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.team_card_master, container, false);
mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);
final Button button = (Button)mView.findViewById(R.id.load_viewpager_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setAdapter(mAdapter);
button.setVisibility(View.GONE);
}
});
mAdapter = new ViewPagerAdapter(getFragmentManager());
new setAdapterTask().execute();
return mView;
}
private class setAdapterTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
mViewPager.setAdapter(mAdapter);
}
}
这篇关于在片段中显示片段查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!