gpt4 book ai didi

android - 当我返回 fragment 时 ViewPager 为空白

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:46 32 4
gpt4 key购买 nike

我有一个 AroundMeFragment fragment ,它有一个带有两个 fragment MapFragmentListFragment 的 View 寻呼机。当我第一次进入这个 fragment 时一切正常,但是当我点击一个项目进入一个新 fragment 的详细 View ,然后返回到 AroundMeFragment 时, View 是空白的,没有绘制任何项目。而且,如果我退出这个 fragment 并进入另一个时间 fragment 是空白的。

当我从细节 fragment 返回到 AroundMeFragment 时如何查看 fragment 的内容?

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();
}
}

主要 Activity 。只有一个 Activity 支持应用程序中的所有 fragment 。

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

最佳答案

mAdapter = new MyFragmentPagerAdapter(getChildFragmentManager());

代替

mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());

根据Austyn Mahoney

关于android - 当我返回 fragment 时 ViewPager 为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18097835/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com