我有一个包含项目列表的主片段,点击一个项目,我在一个新片段中打开项目的详细视图
使用
transaction.replace(R.id.container, FeedDetail.newInstance(title, position)).commit();
详细片段包含FragmentStatePagerAdapter以显示项目的滑动列表。一切正常,但项目的主要列表在后台更新(AsyncTask)。在详细视图中滑动项目时,我得到了
java.lang.IllegalStateException:应用程序的PagerAdapter 无需调用就更改了适配器的内容 PagerAdapter#notifyDataSetChanged!预期的适配器项目数:30, 发现:40寻呼机身份
我正确地从onPostExecute()调用adapter.notifyDataSetChanged(),仍无法解决此问题。我想这个问题可能与此处共享http://translate.google.co.in/translate?hl=en&sl=zh-CN&u=http://blog.csdn.net/hlglinglong/article/details/34845519&prev=search类似,但不确定。我尝试使用适配器的getItemPosition()中的不同值,但没有成功。在这个问题上得到一些暗示会很好。我在这里粘贴详细的片段代码以供参考,
public class FeedDetail extends Fragment {
private ViewPager mPager = null;
private PagerAdapter mPagerAdapter;
private OnFragmentInteractionListener mListener;
private FragmentActivity myContext;
public static String title;
public static Integer pos;
private LayoutInflater mInflater;
private View mRootView = null;
ViewGroup mViewGroup = null;
public static FeedDetail newInstance(String title, int pos) {
FeedDetail fragment = new FeedDetail();
Bundle args = new Bundle();
args.putString("title", title);
args.putInt("pos", pos);
fragment.setArguments(args);
return fragment;
}
public FeedDetail() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString("title");
pos = getArguments().getInt("pos");
}
}
@Override
public void onDestroyView()
{
super.onDestroyView();
ViewGroup parentViewGroup = (ViewGroup) mRootView.getParent();
if( null != parentViewGroup ) {
parentViewGroup.removeAllViews();
}
mViewGroup.removeAllViews();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mViewGroup = container;
if (mRootView == null) {
mRootView = (View) inflater.inflate(R.layout.feedpager, null, false);
}
else {
ViewGroup parent = (ViewGroup) mRootView.getParent();
parent.removeView(mRootView);
}
mPager = (ViewPager) mRootView.findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(pos);
mRootView.setFocusableInTouchMode(true);
mRootView.requestFocus();
return mRootView;
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static interface OnFragmentInteractionListener {
public void onFragmentInteraction(Uri uri);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
setRetainInstance(true);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.newInstance(position);
}
@Override
public int getItemPosition(Object object) {
return 1;
}
@Override
public int getCount() {
return Navigation.adapter.getCount();
}
@Override
public CharSequence getPageTitle(int position) {
return Navigation.adapter.getFeeds().get(position).title;
}
}
}