2013-07-08 68 views
0

我的問題是,當調用destroyItem時,它當前銷燬LinearLayouts,但我的instantiateItem方法實例化相對和線性佈局。因此,當調用destroyItem時,它會得到一個異常,即相對佈局不能轉換爲線性佈局。我用盡如果(對象的instanceof的LinearLayout)破壞那種等等如果instantiateItem使用LinearLayout和RelativeLayout,如何處理destroyItem?

@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); 
} 

回答

1

更改cast- removeView不需要的LinearLayout,它只是需要一個視圖。 LinearLayout和RelativeLayout都來自View。

+0

謝謝,我會接受時間限制。 –

相關問題