使用 FragmentPagerAdapter 的 ViewPager 中的片段在第二次查看时为空白 [英] Fragment in ViewPager using FragmentPagerAdapter is blank the second time it is viewed

查看:16
本文介绍了使用 FragmentPagerAdapter 的 ViewPager 中的片段在第二次查看时为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段界面,底部带有选项卡,可在主视图中打开不同的片段.

I have a fragment interface with tabs along the bottom which open different fragments in the main view.

我有一个特定的片段,它是一个项目列表.如果用户选择此列表中的一个项目,则会打开另一个片段,其中包含一个视图页面,该页面在前一个片段的列表中的所有项目之间水平滚动.这很好用.

I have one particular fragment which is a list of items. If the user selects one of the items in this list, another fragment opens which contains a viewpager which scrolls horizontally between all of the items in the list in the previous fragment. This works great.

viewpager 使用 FragmentPagerAdapter 来显示项目.

The viewpager uses a FragmentPagerAdapter to display the items.

当用户选择列表中的一个项目,查看它,然后点击标签栏上的按钮返回列表,然后选择另一个项目时,问题就出现了.第二次选择一个项目时,会出现一个空白屏幕而不是 viewpager.发生这种情况时,我的 LogCat 中没有收到任何错误.

The problem comes when the user selects an item in the list, views it, then hits the button on the tab bar to go back to the list, then selects another item. The second time an item is selected, a blank screen appears instead of the viewpager. I receive no errors in my LogCat when this happens.

为什么viewpager只出现在第一次?

Why is the viewpager only appearing the first time?

FragmentPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    Cursor mCursor;

    public ViewPagerAdapter(FragmentManager fm, Cursor c) {
        super(fm);
        mCursor = c;
    }

    public void changeCursor(Cursor c) {
        mCursor = c;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mCursor == null) return 0;
        else return mCursor.getCount();
    }

    @Override
    public Fragment getItem(int position) {
        mCursor.moveToPosition(position);
        return TeamCardFragment.newInstance(mCursor, position);
    }
}

PagerFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle bundle = getArguments();
    mCursorPosition = bundle.getInt(TeamCardCommon.BUNDLE_KEY_CURSOR_POSITION);

    View mView = inflater.inflate(R.layout.team_card_master, container, false);
    mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager);

    mAdapter = new ViewPagerAdapter(getFragmentManager(), cursor);
    new setAdapterTask().execute();

    return mView;
}

private class setAdapterTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCursorPosition);
    }
}

推荐答案

我们通过将视图分页器项目重新实现为标准视图而不是片段并相应地更改适配器来解决此问题.

We got around this by re-implementing the view pager items as standard views rather than fragments and changing the adapter accordingly.

这篇关于使用 FragmentPagerAdapter 的 ViewPager 中的片段在第二次查看时为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆