1

我有一个FragmentActivity使用 aViewPager左右翻转数据页(两个ListFragments)的 a。

public class StopsActivity extends FragmentActivity {
   private ViewPager mViewPager;
   private PagerTabStrip mPagerTabStrip;

   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_stops);

     mPagerTabStrip =(PagerTabStrip)findViewById(R.id.pager_header);
     mViewPager = (ViewPager)findViewById(R.id.pager);

     mPagerTabStrip.setDrawFullUnderline(true);
     mPagerTabStrip.setTabIndicatorColorResource(R.color.pagerTabStrip);
     mViewPager.setAdapter(new StopsAdapter(getSupportFragmentManager()));
     mViewPager.setCurrentItem(0);
   }

   private class StopsAdapter extends FragmentPagerAdapter {
     public StopsAdapter(FragmentManager fm) {
        super(fm);
     }

     @Override
     public Fragment getItem(int position) {
        switch (position) {
           case 0:
             return StopsFragment.newInstance(routename, Stop.FORWARD);
           case 1:
             return StopsFragment.newInstance(routename, Stop.BACKWARD);
        }
        return null;
     }

     @Override
     public int getCount() { return 2;}

     @Override
     public CharSequence getPageTitle(int position) { /* implementation ... */}
   }
} 

一切运行正常,但我认为第二个的实例化在调用StopFragment时会使第一个的数据无效。getItem

public class StopsFragment extends ListFragment {
   private StopsAdapter mStopsAdapter;
   private ListView mListView;
   private String routename;
   private int direction;

  public static StopsFragment newInstance(String routename, int direction) {
     StopsFragment stopsFragment = new StopsFragment();

     // Supply arguments
     Bundle args = new Bundle();
     args.putString("routename", routename);
     args.putInt("direction", direction);
     stopsFragment.setArguments(args);
     return stopsFragment;
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     // Setup the adapter
     ArrayList<Stop> stops = ...
     mStopsAdapter = new StopsAdapter(getActivity(), stops);

     setRetainInstance(true);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle inState) {
     View rootView = inflater.inflate(R.layout.fragment_stops, container, false);
     // Attach the adapter
     mListView = (ListView) rootView.findViewById(android.R.id.list);
     mListView.setAdapter(mStopsAdapter);
     return rootView;
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
  } 
}

页面“IDA”对应于StopFragmentwith 参数Stop.FORWARD,页面“VUELTA”对应于StopFragmentwith 参数Stop.BACKWARD。正如您在下图中看到的那样,仅填充了其中一个(最后一个实例化):

我做错了什么?

编辑

这是StopsAdapter

class StopsAdapter extends BaseAdapter {
   private ArrayList<Stop> stops;
   private LayoutInflater inflater;

   public StopsAdapter(Context context, ArrayList<Stop> stops) {
      this.stops = stops;
      this.inflater = LayoutInflater.from(context);
   }

   @Override
   public int getCount() {
      return stops.size();
   }

   @Override
   public Object getItem(int position) {
      return stops.get(position);
   }

   @Override
   public long getItemId(int position) {
      return (long)position;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = inflater.inflate(R.layout.item_stop, parent, false);
      }
      TextView name = (TextView)convertView.findViewById(R.id.tvStopName);
      TextView description = (TextView)convertView.findViewById(R.id.tvStopDescription);
      Stop stop = (Stop)getItem(position);
      name.setText(stop.name);
      if (stop.info != null) {
         description.setText(stop.info);
      }

      return convertView;
   }
}
4

1 回答 1

0

好吧,我的错。给我带来问题的代码是我唯一没有发布的代码(ArrayList<Stop> stops = ...)。关于 Fragments 和 ViewPager 的代码可以正常工作。

于 2013-11-03T19:38:10.043 回答