我正在使用V4兼容性JAR。我有一个ViewPager,显示有关某些乐队的信息。当我创建PagerAdapter时,我将它传递给Band对象的ArrayList。
在模拟器中,它始终显示首先显示ArrayList信息中的第二个波段。当我翻到最后一个视图时,视图始终没有数据。当我来回翻转时,数据会变得很糟糕。
BandsActivity
private ViewPager mBandsPager;
private BandsPagerAdapter mBandsPagerAdapter;
private static final String TAG = "Paging";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.awesomepager);
// Build a couple bands;
ArrayList<Band> bands = new ArrayList<Band>();
bands.add(new Band(1, "Led Zeppelin"));
bands.add(new Band(37, "Brand New"));
bands.add(new Band(49, "Jay-Z"));
mBandsPagerAdapter = new BandsPagerAdapter(this, bands);
mBandsPager = (ViewPager) findViewById(R.id.awesomepager);
mBandsPager.setAdapter(mBandsPagerAdapter);
}
BandsPagerAdapter
private Context mCtx;
private ArrayList<Band> mBands;
public BandsPagerAdapter(Context ctx, ArrayList<Band> bands) {
mCtx = ctx;
mBands = bands;
}
@Override
public int getCount() {
return mBands.size();
}
@Override
public Object instantiateItem(View collection, int position) {
// Inflate and create the view
LayoutInflater layoutInflater = (LayoutInflater)mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.featured_bands_layout, null);
// Build UI
TextView name = (TextView)collection.findViewById(R.id.featured_band_name);
// Populate UI with band data
Band band = mBands.get(position);
if(name != null) {
name.setText("Name: " + band.getName() + ", Pos: " + position);
}
// Add View to the ViewPager collection
((ViewPager)collection).addView(view,0);
return view;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewGroup) collection).removeView((View)view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
有什么建议吗?我看到提到实现ViewPager.SimpleOnPageChangeListener,但没看到如何正确使用它。
答案 0 :(得分:2)
你的BandsPagerAdapter似乎是个问题。这是我将如何实现它。看看它是否有所作为!
public class BandsPagerAdapter extends PageAdaptor{
private ArrayList<Band> mBands;
public BandsPagerAdapter(ArrayList<Band> bands) {
mBands = bands;
}
@Override
public int getCount() {
return mBands.size();
}
@Override
public Object instantiateItem(View collection, int position) {
// Inflate and create the view
LayoutInflater layoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.featured_bands_layout, null);
// Build UI
TextView name = (TextView) view.findViewById(R.id.featured_band_name);
// Populate UI with band data
Band band = mBands.get(position);
if(name != null) {
name.setText("Name: " + band.getName() + ", Pos: " + position);
}
// Add View to the ViewPager collection
((ViewPager) collection).addView(view,0);
return view;
}
@Override
public void destroyItem(View collection, int position, Object view)
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}