我正在尝试动态更新主线程中的android LinearLayout
。
不幸的是,我在网上教程中查明任何内容时遇到了很多麻烦。他们似乎都没有提供如何在线程之间进行通信的完整图片。
我的想法是这样的:
public class MainActivity extends AppCompatActivity {
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
setContentView(layout);
Updater updater = new Updater();
Thread workerThread = new Thread(updater);
//somehow update layout
更新程序类看起来像这样:
public class Updater implements Runnable {
private int count = 0;
public Updater() {}
@Override
public void run()
{
for (int i = 0; i < 10; i ++){
try {
count++;
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我知道我需要Handler
才能在线程之间传递消息,但我不知道如何设置它。
我想避免使用匿名类,并在TextView
有新消息时动态创建新的Updater
。
答案 0 :(得分:3)
WorkerThreadListener
界面:public interface WorkerThreadListener { void onUpdate(int counter); }
Updater
课程:public class Updater implements Runnable { private final WorkerThreadListener mWorkerThreadListener; private final Handler mHandler; private int count = 0; public Updater(final WorkerThreadListener workerThreadListener) { this.mWorkerThreadListener = workerThreadListener; this.mHandler = new Handler(); } @Override public void run() { for (int i = 0; i < 10; i++) { try { count++; mHandler.post(new Runnable() { @Override public void run() { mWorkerThreadListener.onUpdate(count); } }); TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } } } }
MainActivity
课程:public class MainActivity extends AppCompatActivity { private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); layout = new LinearLayout(this); setContentView(layout); Updater updater = new Updater(new WorkerThreadListener() { @Override public void onUpdate(int counter) { //update layout here } }); Thread workerThread = new Thread(updater); workerThread.start(); } }
答案 1 :(得分:0)
嗨,请检查我的下面的答案希望它可以帮到你。
public class ProgressTestActivity extends Activity {
private ProgressBar progress;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = (ProgressBar) findViewById(R.id.progressBar1);
text = (TextView) findViewById(R.id.textView1);
}
public void startProgress(View view) {
// do something long
Runnable runnable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
final int value = i;
doFakeWork();
progress.post(new Runnable() {
@Override
public void run() {
// here you can add any view or anyof your logic which is related to UI put it into here.
text.setText("Updating");
progress.setProgress(value);
}
});
}
}
};
new Thread(runnable).start();
}
// Simulating something timeconsuming
private void doFakeWork() {
SystemClock.sleep(5000);e.printStackTrace();
}
}
其他方式也是可能的。如果您有任何疑问,请在下面发表评论我会详细解释您。
答案 2 :(得分:0)
如果您只想使用滴答计时器并将进度设置为ui线程。您可以使用CountDownTimer
。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView;
private CountDownTimer countDownTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
textView = (TextView) findViewById(R.id.textView);
findViewById(R.id.b2).setOnClickListener(this);
}
public void processData() {
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
textView.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
textView.setText("done!");
}
}.start();
}
@Override
protected void onStop() {
super.onStop();
if (countDownTimer != null) {
countDownTimer.cancel();
}
}
@Override
public void onClick(View v) {
processData();
}
}
除了在UI线程上发布回调之外,您还可以使用Handler。
Handler mainThreadHandler = new Handler(Looper.getMainLooper());
mainThreadHandler.post(new Runnable() {
@Override
public void run() {
}
});