Android TabLayout 切换片段后不再显示内容
Android TabLayout does not display contents anymore as soon as fragment is switched
我在我的项目中使用了一个导航抽屉,里面有 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, 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 工作正常。当我使用导航抽屉切换到另一个片段并切换回该片段时,它什么也不显示,如上所述。
编辑:我是唯一遇到此问题的人吗?如果有人遇到问题,他们是如何解决的?
当我使用 viewPager 并重新打开它时,在我使用 getSupportFragmentManager
.
切换之前,我的片段是空的
尝试使用:getChildFragmentManager
.
我在我的项目中使用了一个导航抽屉,里面有 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, 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 工作正常。当我使用导航抽屉切换到另一个片段并切换回该片段时,它什么也不显示,如上所述。
编辑:我是唯一遇到此问题的人吗?如果有人遇到问题,他们是如何解决的?
当我使用 viewPager 并重新打开它时,在我使用 getSupportFragmentManager
.
尝试使用:getChildFragmentManager
.