现在我正在使用 Jake Wharton 的 ViewPagerIndicator 库。我已经添加了几个Fragments,并且可以来回滑动。如果我从 a 上滑动多个面板Fragment,则它会被删除(onDestroyView()调用)。相反,如果我从 a 在一个面板内滑动Fragment,则它会被添加回来(onCreateView()称为)。
当我Fragment的再次被破坏/添加回来时,Views曾经存在于其中的内容被破坏,我回到空白屏幕。我有一个LinearLayout我Fragment要添加TextViews的内容。重新创建时调用.size()显示LinearLayout正确的计数,但我无法弄清楚要调用什么才能再次显示它。我试过添加<LinearLayout>.requestLayout()in onResume(),但没有奏效。
让我知道是否应该发布任何相关代码。但是,我认为这一切都不是我的 Fragment 所特有的。
编辑:所以最初我认为我必须处理onSavedInstanceState(),但我以前从未保存过整个布局。我的另一个想法是迭代我LinkedList的TextViews,然后将它们添加回我的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);
}