Progressbar setProgress在UI线程上不起作用

问题描述 投票:1回答:1

正在尝试在将值附加到recyclerview时更新进度条。即使在创建新线程后,setProgress方法也无法正常工作。在这里附上我的代码

        progress_horizontal.setVisibility(View.VISIBLE);
        progress_horizontal.setProgress(0);
        progress_horizontal.setMax(titles.length);

        new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {
                        for (int i = 0; i < titles.length; i++) {
                            itemListsList.add(new ListItemRadioLists(titles[i], false));
                            progress_horizontal.setProgress(i);
                        }
                        rv_search.setAdapter(itemAdapter);
                        progress_horizontal.setVisibility(View.GONE);
                    }
                });
            }
        }).start();

有人可以帮我解决这个问题吗?

android progress-bar android-progressbar ui-thread
1个回答
0
投票

您应使用Looper在线程中查看ui。

Looper.prepare();

progress_horizo​​ntal.setProgress(i);

Looper.loop();

© www.soinside.com 2019 - 2024. All rights reserved.