ViewPager未显示正确的视图。

3
我正在使用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,但没有看到如何正确使用它。


这与答案无关,但将"collection.findViewById"更改为"view.findViewById"更合适。 - Yashwanth Kumar
当您旋转屏幕时,您的活动将被重新创建,因此上下文将被更改;因此,在某种程度上,您的页面适配器正在保留不再存在的上下文,并将导致内存泄漏! - Raunak
结果证明这是正确的答案!我需要使用刚刚填充的视图对象。TextView name =(TextView)view.findViewById(R.id.featured_band_name); - Andy Lawton
1个回答

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) {}
}

完美!就像我上面说的一样,你必须使用充气视图。如果使用集合中的Context来避免内存泄漏,那就更棒了。 - Andy Lawton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接