重新加载片段后,在FragmentPageAdapter上看不到ListFragment。
我有一个带有3个选项的滑动菜单,当我单击一个选项时,fragmentpageadapter的片段会发生变化,并将列表片段加载到一个选项卡中。
当我在同一选项中再次单击时,问题就开始了…片段更改,但加载的片段为空,任何列表…
有人知道会发生这种事吗?
代码是:
是一个代码示例..
调用FragmentPageAdapter时的代码…
public class MyListaCompra extends Fragment {
View gv;
private static final String[] CONTENT = new String[] {"Pendientes","Realizadas" };
public static Intent newInstance(Activity activity,int pos) {
Intent intent = new Intent(activity, MyListaCompra.class);
return intent;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("Truiton FragmentList", "CREO LA VISTA");
gv = inflater.inflate(R.layout.inicio,container, false);
gv.setBackgroundResource(android.R.color.white);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(getActivity().getSupportFragmentManager());
ViewPager pager = (ViewPager) gv.findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) gv.findViewById(R.id.indicator);
indicator.setViewPager(pager);
return gv;
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("Truiton FragmentList", "ESTOY EN PAUSE");
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("Truiton FragmentList", "ESTOY EN RESUME");
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
Log.i("Truiton FragmentList", "VistaDestruida!");
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu, menu);
}
class GoogleMusicAdapter extends FragmentPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return FragmentListaPendiente.init(position);
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override
public int getCount() {
return CONTENT.length;
}
}
}
使用ListFragment为FragmentPageAdapter充气的代码
public class FragmentListaPendiente extends ListFragment{
int fragNum;
private Bundle savedState = null;
String arr[] = { "dates","exemple","1" };
public static FragmentListaPendiente init(int val) {
FragmentListaPendiente truitonList = new FragmentListaPendiente();
// Supply val input as an argument.
Bundle args = new Bundle();
args.putInt("val", val);
truitonList.setArguments(args);
return truitonList;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list,null);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arr));
this.getListView().setTextFilterEnabled(true);
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("Truiton FragmentList", "Item clicked: " + id);
}
}
谢谢
最佳答案
我调查这个问题然后解决这个问题…
问题是:
FragmentPagerAdapter adapter = new GoogleMusicAdapter(getActivity().getSupportFragmentManager());
为了解决这个问题:
FragmentPagerAdapter adapter = new GoogleMusicAdapter(getChildFragmentManager());
信息来源:
FragmentPagerAdapter inside Fragment
Navigating back to FragmentPagerAdapter -> fragments are empty
谢谢=)