当前位置:  开发笔记 > 编程语言 > 正文

从BackStack返回片段后,Android ViewPager无法正常显示

如何解决《从BackStack返回片段后,AndroidViewPager无法正常显示》经验,为你挑选了1个好方法。

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

ViewPager中的每个项目都位于片段中,如下所示:

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

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

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

这全部都放在另一个片段中,它将适配器设置在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);

        // ... //
}

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

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

因此,在我的外部导航菜单片段中使用我的ViewPager片段时,当我从ViewPager中选择一个项目时,我将整个外部导航菜单放在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项后,图标会再次出现.有谁知道为什么会这样?我有点不确定当它们被放置在backStack上并从backStack中检索时片段发生了什么.谢谢.



1> Highway62..:

解决了!

几个小时后我终于解决了这个问题.我在这里找到的解决方案只是在设置适配器时用'getChildFragmentManager()'替换'getFragmentManager()':

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

推荐阅读
夏晶阳--艺术
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有