其他分享
首页 > 其他分享> > android-ViewPager杀死它后,片段布局不显示

android-ViewPager杀死它后,片段布局不显示

作者:互联网

现在,我正在使用Jake Wharton的ViewPagerIndicator库.我在其中添加了几个片段,可以来回滑动.如果我从Fragment滑动多个面板,则会将其删除(称为onDestroyView()).相反,如果我从Fragment在一个面板中滑动,则会将其重新添加(称为onCreateView()).

当我的片段再次被销毁/重新添加时,曾经存在于其中的视图被销毁,我回到空白屏幕.我在片段中添加了TextViews的LinearLayout.重新创建LinearLayout时,调用.size()会显示正确的计数,但是我不知道要调用什么才能使其再次显示.我曾尝试在onResume()中添加< LinearLayout> .requestLayout(),但这没有用.

让我知道是否应该发布任何相关代码.我认为这并不是我的Fragment真正特定的.

编辑:因此,本来我以为我必须处理onSavedInstanceState(),但是我之前从未保存过整个布局.我的另一个想法是迭代TextViews的LinkedList,然后将它们添加回我的LinearLayout中.这给我一个错误,因为View已经有一个父级,这是有道理的.

这是我添加TextView的方法:

public void addQuickLook(QuickLookView v)
{
    mViews2.add(v); //LinkedList of QuickLookViews
    mMeterLayout.addView(v); //Adding to the LinearLayout
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.fragment_quicklook, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    mMeterLayout = (LinearLayout) getActivity().findViewById(R.id.quickLookList);
}

解决方法:

我想我已经解决了.我的操作在FragmentPagerAdapter中覆盖了destroyItem()方法,并将其留空.我应该更仔细地阅读FragmentPagerAdapter的开发人员页面,以了解其预期的行为,尤其是所有内容都已加载到内存中,并且当不再可见时,它会杀死Fragment的Views以节省内存.不确定这是否是正确的方法,但目前效果良好.

标签:android-fragments,android-layout,android-viewpager,android
来源: https://codeday.me/bug/20191201/2077393.html