片段是空白它被视为第二次 [英] Fragment is blank the second time it is viewed

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

问题描述

我有一个片段,接口在底部标签而在主视图中打开不同的片段。

我有一个特定的片段,其是项目的列表。如果用户在该列表中选择的项目之一,另一片段将打开其含有viewpager水平地滚动所有的项之间在列表在previous片段。这个伟大的工程。

的viewpager使用FragmentPagerAdapter显示的项目

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

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

FragmentPagerAdapter:

 公共类ViewPagerAdapter扩展FragmentPagerAdapter {
    光标mCursor;

    公共ViewPagerAdapter(FragmentManager FM,光标C){
        超(FM);
        mCursor = C;
    }

    公共无效changeCursor(光标C){
        mCursor = C;
        this.notifyDataSetChanged();
    }

    @覆盖
    公众诠释getCount将(){
        如果(mCursor == NULL)返回0;
        否则返回mCursor.getCount();
    }

    @覆盖
    公共片段的getItem(INT位置){
        mCursor.moveToPosition(位置);
        返回TeamCardFragment.newInstance(mCursor,位置);
    }
}
 

PagerFragment:

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    捆绑包= getArguments();
    mCursorPosition = bundle.getInt(TeamCardCommon.BUNDLE_KEY_CURSOR_POSITION);

    查看MVIEW = inflater.inflate(R.layout.team_card_master,集装箱,假);
    mViewPager =(ViewPager)mView.findViewById(R.id.team_card_master_view_pager);

    mAdapter =新ViewPagerAdapter(getFragmentManager(),光标);
    新setAdapterTask()执行()。

    返回MView的;
}

私有类setAdapterTask扩展的AsyncTask<虚空,虚空,虚空> {
    保护无效doInBackground(虚空...... PARAMS){
        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效的结果){
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(mCursorPosition);
    }
}
 

解决方案

我们通过重新实现视图寻呼机的项目,如标准的意见,而不是片段,并相应地改变适配器解决这个了。

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.

The viewpager uses a FragmentPagerAdapter to display the items.

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.

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.

这篇关于片段是空白它被视为第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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