繁体   English   中英

Android系统。 处理程序。 可运行

[英]Android. Handler. Runnable

 public void DRAW() {
    handler = new Handler(Looper.getMainLooper());

    for (Object[] o : list) {
        final Object[] oo = o;
        final Runnable r = new Runnable() {
            public void run() {

                if ((boolean) oo[0] == true) {
                    drawCM((boolean) oo[0], (int) oo[1], (boolean) oo[2]);
                } else {
                    draw((boolean) oo[0], (int) oo[1], (boolean) oo[2]);
                }

            }
        };
        handler.postDelayed(r, 1000);
    }

    list.clear();
}

我在ArrayList中存储有关2个不同类别的imageButtons的数据。 draw()和drawCM()方法仅更改imageButtons的背景。

现在,代码将在1秒钟后更改背景,但是对于我同时在列表中有数据的所有imageButtons而言。

我想在imageButton之后逐步更改其背景,即imageButton。

您为每个对象添加了一个处理程序,因此您可以简单地更改每个对象的延迟,例如1000个next 2000 ...

或者仅使用一个处理程序,将循环放入run方法中,并在循环结束时添加延迟

试试以下代码片段:

public class MainActivity extends AppCompatActivity {

    private int index;
    private Handler handler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        handler.post(changeBgRunnable);
    }

    List<Object[]> list = null;

    public Runnable changeBgRunnable = new Runnable() {
        @Override
        public void run() {
            if (list != null && list.size() > index) {
                Object[] oo = list.get(index);
                if ((boolean) oo[0]) {
                    drawCM((boolean) oo[0], (int) oo[1], (boolean) oo[2]);
                } else {
                    draw((boolean) oo[0], (int) oo[1], (boolean) oo[2]);
                }

                index++;
                handler.postDelayed(changeBgRunnable, 1000);
            }
        }
    };

    public void draw(boolean b, int i, boolean bb) {

    }

    public void drawCM(boolean b, int i, boolean bb) {

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM