gpt4 book ai didi

从 backStack 返回 fragment 后,Android ViewPager 无法正确显示

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:02:52 26 4
gpt4 key购买 nike

我有一个 ViewPager 设置,它使用 fragment 来显示我可以循环显示的不同图标,以便用户可以导航不同的菜单选项。

ViewPager 中的每个项目都包含在一个 Fragment 中,如下所示:

public static Fragment newInstance(MainHandheldActivity context, int pos)
{
Bundle b = new Bundle();
b.putInt("pos", pos);
return Fragment.instantiate(context, ObjectFragment.class.getName(), b);
}

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

// Get menu image icons
int icons[] = new int[3];
icons[0] = R.mipmap.ic_text;
icons[1] = R.mipmap.ic_voice;
icons[2] = R.mipmap.ic_mail;

// Get root view of the fragment layout
LinearLayout rootView = (LinearLayout) inflater.inflate(
R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
int pos = args.getInt("pos");

// Set the current menu image and text
ImageView menuImg = (ImageView) rootView.findViewById(R.id.fragment_image);
menuImg.setImageResource(icons[pos]);

TextView menuText = (TextView) rootView.findViewById(R.id.menuTxt);
switch (pos){
case 0:
menuText.setText(MENU_HEADER_TEXT);
break;
case 1:
menuText.setText(MENU_HEADER_VOICE);
break;
case 2:
menuText.setText(MENU_HEADER_MAIL);
// TODO Get number of messages and concatenate to string eg Inbox (3)
break;
}

// Set the current menu selection
MainActivityInterface ai = (MainActivityInterface) getActivity();
ai.setCurrentSelection(pos);
return rootView;
}

这些 fragment 由页面适配器使用,如下所示:

public class CollectionPageAdapter extends FragmentPagerAdapter implements
ViewPager.OnPageChangeListener {

private MainHandheldActivity context;
private FragmentManager fm;

public CollectionPageAdapter(MainHandheldActivity context, FragmentManager fm) {
super(fm);
this.fm = fm;
this.context = context;
}

@Override
public Fragment getItem(int position)
{
position = position % MainHandheldActivity.PAGES;
Log.d("POS", "POSITION: " + position);
return ObjectFragment.newInstance(context, position);
}

@Override
public int getCount()
{
return MainHandheldActivity.PAGES * MainHandheldActivity.LOOPS;
}
}

这一切都包含在另一个 fragment 中,该 fragment 在其 onCreateView 方法中设置适配器:

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

// ... //

// Scrolling menu
pager = (ViewPager) view.findViewById(R.id.pager);
adapter = new CollectionPageAdapter((MainHandheldActivity)getActivity(),getFragmentManager());
pager.setAdapter(adapter);
pager.addOnPageChangeListener(adapter);
// Set current item to the middle page
pager.setCurrentItem(MainHandheldActivity.FIRST_PAGE);
currentPage = MainHandheldActivity.FIRST_PAGE;
// Set number of pages
pager.setOffscreenPageLimit(3);
// Set no margin so other pages are hidden
pager.setPageMargin(0);

// ... //
}

这个外部 fragment 被添加到一个事务中,如下所示:

mFragmentManagerer = getSupportFragmentManager();
mFragmentTransaction = mFragmentManagerer.beginTransaction();
mMainFragment = new MainFragment();
mFragmentTransaction.add(R.id.fragmentContainer, mMainFragment, FRAG_MAIN);
mFragmentTransaction.commit();
mFragmentManagerer.executePendingTransactions();

因此,当我从 ViewPager 中选择一个项目时,将我的 ViewPager fragment 放在我的外部导航菜单 fragment 中,我将整个外部导航菜单放在 backStack 上:

public void replaceFragment(Fragment frag, String fragTag) {
mFragmentTransaction = mFragmentManagerer.beginTransaction();
mFragmentTransaction.replace(R.id.fragmentContainer, frag, fragTag);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
}

到目前为止一切正常。然而,从 backStack 返回后(当我按下手机上的后退按钮时)viewPager 不显示图标,但它仍然允许我循环浏览 viewPager 并且在循环浏览 2 或 3 个项目后图标再次出现。有谁知道为什么会这样?我有点不确定当 fragment 被放置在 backStack 上并从中检索时到底发生了什么。谢谢。

最佳答案

已解决!

经过几个小时,我终于解决了这个问题。我找到的解决方案here ,只是在设置适配器时将“getFragmentManager()”替换为“getChildFragmentManager()”:

pager = (ViewPager) view.findViewById(R.id.pager);

// Added getChildFragmentManager in here
adapter = new CollectionPageAdapter((MainHandheldActivity)getActivity(),getChildFragmentManager());

pager.setAdapter(adapter);
pager.addOnPageChangeListener(adapter);
// Set current item to the middle page
pager.setCurrentItem(MainHandheldActivity.FIRST_PAGE);
Log.d("PAGER", "pager setCurrentItem: " + MainHandheldActivity.FIRST_PAGE);
currentPage = MainHandheldActivity.FIRST_PAGE;
// Set number of pages
pager.setOffscreenPageLimit(3);
// Set no margin so other pages are hidden
pager.setPageMargin(0);

关于从 backStack 返回 fragment 后,Android ViewPager 无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34688986/

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