我正在項目中使用導航抽屜,其中有5個片段。在一個片段中,我在設計支持庫中引入了TabLayout,其中包含2個片段。所有的工作都很好,除了當我離開具有TabLayout並切換回它的片段時,所有的內容都消失了。Android 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工作正常。當我使用導航抽屜切換到另一個片段並切換回該片段時,它不顯示任何內容,如上所述。
編輯:我是唯一一個有這個麻煩嗎?如果有人遇到問題,他們是如何解決問題的?
非常感謝!你拯救了我的一天。我掙扎了好幾個小時...... –
它爲我工作。謝謝。 –
是的,這幫助了我,getSupportFragmentManager不起作用,但getChildFragmentManager正在完美工作。 –