导航回FragmentPagerAdapter,listFragment为空

时间:2014-01-23 22:00:59

标签: java android android-fragments uipageviewcontroller fragmentpageradapter

重新加载片段后,我在FragmentPageAdapter上看不到ListFrangment。 我有一个带有3个选项的滑动菜单,当我单击一个选项时,FragmentPageAdapter的片段更改并在选项卡中加载ListFragment。 问题开始当我再次在同一选项中单击时...片段更改但加载的片段为空,任何列表...

有谁知道会发生这种情况?

代码是: 是一个代码例子..

调用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);
    }
}

由于

1 个答案:

答案 0 :(得分:3)

我调查了这个问题并解决了这个问题.... 问题是:

FragmentPagerAdapter adapter = new GoogleMusicAdapter(getActivity().getSupportFragmentManager());

解决这个问题:

FragmentPagerAdapter adapter = new GoogleMusicAdapter(getChildFragmentManager());

信息来源:

FragmentPagerAdapter inside Fragment

Navigating back to FragmentPagerAdapter -> fragments are empty

谢谢=)