ImageView的无效无法正常工作

时间:2012-10-05 05:25:06

标签: android

我已经为viewpager创建了页面指示器。当用户在viewpager中滑动时,我正在更改指示器图像。

以下是onPageSelected#ViewPager.OnPageChangeListener的代码

public void onPageSelected(int position) {
    int childCount = this.layout.getChildCount();
    for (int i = 0; i < childCount; i++) {
        PageItem child = (PageItem) this.layout.getChildAt(position);
        if (i == position) {
            child.setActiveState();
        } else {
            child.resetState();
        }
    }
    this.layout.invalidate();
}

以下是我的PageItem类

public class PageItem extends ImageView {

private int activeBitmap;
private int inActiveBitmap;
private boolean isActive;

public PageItem(Context context, AttributeSet attrs, int defStyle,
        int activeBitmapId, int inActiveBitmapId) {
    super(context, attrs, defStyle);
    this.activeBitmap = activeBitmapId;
    this.inActiveBitmap = inActiveBitmapId;
    this.setImageResource(inActiveBitmap);
    isActive = false;
}

public PageItem(Context context, AttributeSet attrs, int activeBitmapId,
        int inActiveBitmapId) {
    super(context, attrs);
    this.activeBitmap = activeBitmapId;
    this.inActiveBitmap = inActiveBitmapId;
    this.setImageResource(inActiveBitmap);
    isActive = false;
}

public PageItem(Context context, int activeBitmapId, int inActiveBitmapId) {
    super(context);
    this.activeBitmap = activeBitmapId;
    this.inActiveBitmap = inActiveBitmapId;
    this.setImageResource(inActiveBitmap);
    isActive = false;
}

public void resetState() {
    this.setImageDrawable(this.getContext().getResources()
            .getDrawable(inActiveBitmap));
    this.invalidate();
    isActive = false;
}

public void setActiveState() {
    this.setImageDrawable(this.getContext().getResources()
            .getDrawable(activeBitmap));
    this.invalidate();
    isActive = true;
}

}

现在,在我第一次刷卡后启动我的应用程序时,它会更改图像,但之后不会更改图像。

提前感谢。

1 个答案:

答案 0 :(得分:0)

看起来应该是:

PageItem child = (PageItem) this.layout.getChildAt(i);