问题描述
我使用的是抽屉式导航栏在我的项目,里面有5片段。在一个片段我有在设计上支持库介绍TabLayout,其中包括2片段。一切工作正常,当我离开它具有TabLayout并切换回它的片段,所有的内容消失除。
I am using a navigation drawer in my project, with 5 fragments in it. In one fragment I have the in the design support library introduced TabLayout, which includes 2 Fragments. All works fine, except when I leave the fragment which has the TabLayout and switch back to it, all the content is gone.
在在TabLayout每个片断我有它一个ListView一个SwipeRefreshLayout,他们都不会显示在开关任何东西。
In each fragment in the TabLayout I have a SwipeRefreshLayout with a ListView in it, they both don't display anything upon switch.
我也注意到了TabLayout开始代理奇怪。由奇怪我的意思是白色的指示灯开始跳跃,你可以在这两个选项卡中间仍持有它...
I also noticed the TabLayout starts acting strange. by strange I mean the white indicator starts jumping, you are able to hold it still in the middle of the two tab...
所以,我失去的东西,在TabLayout行为如此奇怪?
So am I missing something that the TabLayout acts so strange?
在code在这里:
片段包含TabLayout:
The code here:Fragment which contains the 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, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity1, container, 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, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_lines_all, container, 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工作正常。当我切换到使用导航抽屉另一个片段切换回这个片段,它会显示什么,如上所述。
I am using the MainFragment as a singleton and replacing the content with it as soon as the user selects it in the navigation drawer. When I switch to it, everything works fine, the ListView contains its data, the SwipeRefreshLayout works fine. When I switch to another fragment using the navigation drawer and switch back to this fragment, it displays nothing, as described above.
编辑:我是有这个麻烦的唯一一个?如果任何人有问题,他们怎么解决?
Am I the only one having trouble with this? If anyone had problems, how did they fix it?
推荐答案
当我用viewPager和背部就可以了,我只好空的片段,直到开关,当我用 getSupportFragmentManager
。
When I used viewPager and back on it, I had empty fragments until switch when I used getSupportFragmentManager
.
尝试使用: getChildFragmentManager
Try to use: getChildFragmentManager
.
这篇关于机器人TabLayout已不只要片段被切换显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!