当我返回到片段时,ViewPager为空白

时间:2013-08-07 08:01:18

标签: android android-viewpager fragment

我有一个片段AroundMeFragment,它有一个包含两个片段MapFragmentListFragment的视图寻呼机。当我第一次在这个片段中输入时,一切正常,但当我点击一个项目进入一个新片段中的详细视图,然后返回到AroundMeFragment时,视图为空,没有绘制任何项目。并且,如果我退出此片段并再次输入片段为空白。

当我从细节片段返回到AroundMeFragment时,如何看到碎片的内容?

ArouundMeFragment:

public ArroundMeFragment() {
}

public static Fragment newInstance(Bundle bundle) {
    ArroundMeFragment f = new ArroundMeFragment();
    f.setArguments(bundle);
    return f;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.activity = activity;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sectionShowed = MAP_VIEW_ARROUND_ME;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.arround_me_main_layout, container, false);

    mViewPager = (ViewPager) v.findViewById(R.id.viewPager);

    if (fragments == null) {
        fragments = new ArrayList<Fragment>();

        fragments.add(MapMainFragment.newInstance());

        Bundle bundle = new Bundle();
        bundle.putInt(ConstantsTypeSection.TYPE_VIEW_SECTION, ConstantsTypeSection.AROUND_ME_SECTION);
        fragments.add(DetailShapelessListViewFragment.newInstance(bundle));
    }

    mAdapter = new AroundMePagerAdapter(getFragmentManager(), fragments);
    mViewPager.setAdapter(mAdapter);



    // BTN CHANGE VIEW
    btn_change_view = (ImageButton) v.findViewById(R.id.btn_change_view);
    btn_change_view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            changeView();
        }
    });

    return v;
}

/**
 * Cambia una vista u otra
 */
private void changeView() {

    try {

        if (sectionShowed == LIST_VIEW_ARROUND_ME) {

            mViewPager.setCurrentItem(0);

            sectionShowed = MAP_VIEW_ARROUND_ME;

            btn_change_view.setImageResource(R.drawable.selector_btn_change_map_arroundme);

        } else if (sectionShowed == MAP_VIEW_ARROUND_ME) {

            mViewPager.setCurrentItem(1);

            sectionShowed = LIST_VIEW_ARROUND_ME;

            btn_change_view.setImageResource(R.drawable.selector_btn_change_list_arroundme);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}// End Method

private class AroundMePagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragments;

    public AroundMePagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

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

MainActivity。只有一个活动可以支持应用中的所有片段。

 ....
 case ConstantsMenu.ARROUND_ME_SECTION:
     Fragment = ArroundMeFragment.newInstance(bundle);  
     break;
 ....

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
 ft.replace(R.id.content_frame, mFragment);
 if (hasToAddToBackStack) {
      ft.addToBackStack(null);
 }
 ft.commit();

2 个答案:

答案 0 :(得分:7)

mAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());

而不是

mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());

by Austyn Mahoney

答案 1 :(得分:4)

嵌套片段在版本16及更低版本中不兼容。现在,您可以动态nest fragment

我更改了我的代码,现在我没有使用viewpager并添加像这段代码一样的片段来将片段切换到另一个片段。

getChildFragmentManager().beginTransaction().replace(R.id.container, list, "LIST").setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();