我在我的项目中使用导航抽屉,里面有5个片段.在一个片段中,我在设计支持库中引入了TabLayout,其中包括2个片段.一切正常,除非我离开具有TabLayout的片段并切换回它,所有内容都消失了.
在TabLayout的每个片段中,我有一个带有ListView的SwipeRefreshLayout,它们都不会在切换时显示任何内容.
我也注意到TabLayout开始表现得很奇怪.奇怪的是我的意思是白色指示器开始跳跃,你能够将它仍然保持在两个标签的中间……
所以我错过了TabLayout行为如此奇怪的东西吗?
这里的代码:
包含TabLayout的片段:
public class MainFragment extends Fragment { private static String locale; private static ViewPager viewPager; private static TabLayout tabLayout; @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { Utils.changeLanguage(getActivity()); final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(),R.style.MyStyle); LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper); View view = localInflater.inflate(R.layout.fragment1,container,false); viewPager = (ViewPager) view.findViewById(R.id.viewpager); tabLayout = (TabLayout) view.findViewById(R.id.tabs); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setupViewPager(viewPager); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager(ViewPager viewPager) { LinesTabsAdapter adapter = new LinesTabsAdapter(getActivity().getSupportFragmentManager()); adapter.addFragment(new Fragment1(),"Fragment 1"); adapter.addFragment(new Fragment2(),"Fragment2"); viewPager.setAdapter(adapter); } public static class Fragment extends Fragment { private static List<RowItems1> rowItems; private static ListView listview; private static Adapter1 adapter; @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity1,false); listview = (ListView) view.findViewById(R.id.listview1); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); updateInfo(true); } public static void updateInfo() { rowItems = new ArrayList<>(); adapter = new Adapter1(context,rowItems); listview.setAdapter(adapter); } } public static class Fragment2 extends Fragment { private static List<RowItems2> rowItems; private static ListView listview; private static Adapter1 adapter; @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_lines_all,false); listview = (ListView) view.findViewById(R.id.activity2); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); updateInfo(true); } public static void updateInfo() { rowItems = new ArrayList<>(); adapter = new Adapter2(context,rowItems); listview.setAdapter(adapter); } } }
我使用MainFragment作为单例,并在用户在导航栏中选择内容时立即用内容替换内容.当我切换到它,一切正常,ListView包含其数据,SwipeRefreshLayout工作正常.当我使用导航抽屉切换到另一个片段并切换回此片段时,它不会显示任何内容,如上所述.
编辑:我是唯一一个遇到麻烦的人吗?如果有人有问题,他们是如何解决的?
解决方法
当我使用viewPager并重新使用它时,我在使用getSupportFragmentManager时切换时会出现空片段.
尝试使用:getChildFragmentManager.