0

我的问题是,当调用destroyItem 时,它当前会破坏LinearLayouts,但我的instantiateItem 方法会同时实例化相对布局和线性布局。因此,当调用 destroyItem 时,会出现一个异常,即不能将相对布局转换为线性布局。我试过如果(object instanceof linearlayout)破坏那种等等......

@Override
public Object instantiateItem(View collection, int position) {

    RelativeLayout wholeView = new RelativeLayout(collection.getContext());

    // images view
    RelativeLayout images = new RelativeLayout(collection.getContext());
    for (int i = 0; i < 2; i++) {
        ImageView image = new ImageView(collection.getContext());
        image.setPadding(0, 0, 0, 0);
        images.addView(image);
        image.setImageResource(GalImages[i]);
        if (i==1) {
            image.setScaleType(ImageView.ScaleType.FIT_END);
        }
    }

    // text labels view
    LinearLayout textLabels = new LinearLayout(collection.getContext());
    textLabels.setOrientation(LinearLayout.VERTICAL);

    TextView activityDescription = new TextView(collection.getContext());
    activityDescription.setText("Warm-up");
    activityDescription.setPadding(200, 200, 0, 0);
    activityDescription.setTextSize(30);
    textLabels.addView(activityDescription);

    TextView timeLeftForThisActivity = new TextView(collection.getContext());
    timeLeftForThisActivity.setText("00:00");
    timeLeftForThisActivity.setPadding(200, 0, 0, 0);
    timeLeftForThisActivity.setTextSize(60);
    textLabels.addView(timeLeftForThisActivity);

    TextView elapsedTimeStatic = new TextView(collection.getContext());
    elapsedTimeStatic.setText("Elapsed Time: ");
    elapsedTimeStatic.setPadding(200, 0, 0, 0);
    elapsedTimeStatic.setTextSize(20);
    textLabels.addView(elapsedTimeStatic);

    // adding images and text to overall view
    wholeView.addView(images);
    wholeView.addView(textLabels);

    ((ViewPager) collection).addView(wholeView, 0);

    return wholeView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout) object);
}
4

1 回答 1

1

更改 cast-removeView 不需要 LinearLayout,它只需要一个 View。而且LinearLayout 和RelativeLayout 都派生自View。

于 2013-07-08T15:03:00.087 回答