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

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

问题描述

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

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

我有一个特定的片段,是一个项目列表。如果用户选择此列表中的一个项目,则将打开另一个片段,其中包含一个viewpager,该viewpager在上一个片段的列表中的所有项目之间水平滚动。

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天全站免登陆