setVisibility仅在ImageView上运行一次

时间:2012-10-27 15:06:07

标签: android gridview view imageview

我试图让GridView中的视图变得不可见和可见。我可以在第一次调用INVISIBLE AND VISIBLE时使其工作,但后续尝试不起作用。

  private void flashImageSeq(){
    int i = 0;
    while (i < 9){
        animate(i);         
        ++i;
    }
}

private void startThread() {
  t = new Thread() {
    public void run () {
       try {
        sleep(500);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
  };

  t.start();
}

private void animate(int position){
    gridview.findViewById(R.id.gridview);
  // imgView is defined in View getView for Adapter code not shown
    ImageView imgView = (ImageView)gridview.getChildAt(position);
    imgView.setVisibility(ImageView.INVISIBLE);
  startThread();
  imgView.setVisibility(ImageView.VISIBLE);
}

第二次调用flashImageSeq()不起作用

2 个答案:

答案 0 :(得分:0)

尝试

private void flashImageSeq(){
int i = 0;
while (i < 9){
    animate(i);         
    ++i;
}
}

private void startThread(final ImageView im) {
  t = new Thread() {
    public void run () {
       try {
        sleep(500);
        yourActivity.this.runOnUiThread(new Runnable() {

            public void run() {

            im.setVisibility(ImageView.VISIBLE);

            }
        });
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
  };

  t.start();
}

private void animate(int position){
    gridview.findViewById(R.id.gridview);
  // imgView is defined in View getView for Adapter code not shown
    ImageView imgView = (ImageView)gridview.getChildAt(position);
    imgView.setVisibility(ImageView.INVISIBLE);
  startThread();

}

答案 1 :(得分:0)

您正在创建一个休眠500毫秒的新线程,但是您的两个setVisibility调用在一个分裂毫秒内发生。

您应该使用Thread.sleep(500)来实际暂停主线程但是这真的很糟糕!

最好创建一个运行循环的线程,并确保使用“runOnUIThread”执行setVisi调用,因为另一个答案显示它