8

我有一个片段 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();
    }
}

主要活动。只有一个活动支持应用程序中的所有片段。

 ....
 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();
4

2 回答 2

13
mAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());

代替

mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());

由奥斯汀·马奥尼( Austyn Mahoney)

于 2016-09-06T08:24:20.923 回答
5

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

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

getChildFragmentManager().beginTransaction().replace(R.id.container, list, "LIST").setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
于 2013-08-07T08:45:03.317 回答