使用ViewPager在一个FragmentActivity中使用相同ListFragment的两个实例

时间:2013-11-02 02:27:14

标签: android android-fragments android-viewpager

我有FragmentActivity使用ViewPager左右翻转数据页面(两个ListFragments)。

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”对应StopFragment参数Stop.FORWARD,页面“VUELTA”对应StopFragment参数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;
   }
}

1 个答案:

答案 0 :(得分:0)

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