我有一个名为MainFragment
的片段TabLayout
。 TabLayout
有两个标签,每个标签都有一个片段。让我们称之为Fragment A
和Fragment B
。
当我打开MainFragment
时,Fragment A
和Fragment B
按预期填充在TabLayout
内。当我恢复MainFragment
时,Fragment A
和Fragment B
未恢复,我会收到两个空标签。
我的MainFragment
看起来像这样
@Override
public View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment, null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Injector.component().inject(this);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new FragmentA(), "Fragment A");
adapter.addFragment(new FragmentB(), "Fragment B");
viewPager.setAdapter(adapter);
}
我该如何解决这个问题?
答案 0 :(得分:3)
碎片B
& MainFragment
是getChildFragmentManager()
的子片段,使用getFragmentManager()
代替// JavaScript Document
var xmlhttp;
function generaGrafico(nome){
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null){
alert ("Browser does not support HTTP Request");
return;
}
var url = "php/creaGrafico.php";
url = url+"?nome="+nome;
xmlhttp.onreadystatechange = stateChanged;
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function stateChanged(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
var spanstat = document.getElementById("stats");
spanstat.innerHTML = xmlhttp.responseText;
}
}
function GetXmlHttpObject(){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject){
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}