我想在活动加载时显示进度条。但是进度条的工作时间只有一半,当进度条不起作用时,充电时会显示黑屏。
这是我的AsyncTask:
class Task extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
layout.setVisibility(View.VISIBLE);
titre_projet.setVisibility(View.GONE);
description_projet.setVisibility(View.GONE);
image_projet.setVisibility(View.GONE);
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean result) {
layout.setVisibility(View.GONE);
titre_projet.setVisibility(View.VISIBLE);
description_projet.setVisibility(View.VISIBLE);
image_projet.setVisibility(View.VISIBLE);
super.onPostExecute(result);
}
@Override
protected Boolean doInBackground(String... params) {
try {
runOnUiThread(new Runnable() {
public void run() {
Intent intent = getIntent();
if (intent != null) {
searchResultats(intent.getStringExtra(EXTRA_PROJET));
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
在此代码中,runOnUiThread
中的所有内容都是我显示内容的代码。
这就是我在OnCreate中所拥有的:
image_projet = (ImageView) findViewById(R.id.imageView_projet);
layout = (LinearLayout) findViewById(R.id.progressbar_view);
titre_projet = (TextView) findViewById(R.id.titre_projet);
description_projet = (TextView) findViewById(R.id.description_projet);
new Task().execute();
我的progressBar的XML:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
android:toDegrees="360"
android:layout_width="match_parent"
android:layout_height="match_parent">
<shape android:shape="ring" android:innerRadiusRatio="3"
android:thicknessRatio="8" android:useLevel="false">
<size android:width="76dip" android:height="76dip" />
<gradient android:type="sweep" android:useLevel="false"
android:startColor="#447a29"
android:endColor="#447a29"
android:angle="0"
/>
</shape>
</rotate>
答案 0 :(得分:1)
当您的新闻活动开始时,开始获取您的数据(文章)。在Activity的onCreate方法中,启动您的任务。在任务的doInBackground方法中,获取文章(我假设您需要通过网络获取它)。 完成任务后,在onPostExecute中更新UI。
class Task extends AsyncTask<String, Integer, Object> {
private Activity activity;
public Task(Activity a){
activity=a;
}
@Override
protected void onPostExecute(Object article) {
activity.onArticleFetched(article);
}
@Override
protected Object doInBackground(String... params) {
Object article = new Article();
//Fetch article... may take a few seconds, but you don't care because you are not in the UI thread
return article;
}
}
在活动中,执行以下操作:
class NewsActivity extends Activity {
@Override
public void onCreate(Bundle a){
super.onCreate(a);
//do some stuff.
task.execute();
progressBar.setVisibility(View.VISIBLE);
}
public void onArticleFetched(Object article){
progressBar.setVisibility(View.GONE);
//updateUI
}
}
答案 1 :(得分:1)
您已发布drawable
而不是layout
xml。我还没有猜到如何用它来表示进展。
请检查AsyncTask reference。 AsyncTask.doInBackground()
应该用于获取数据(网络,解析和UI线程中禁止的其他内容)。在那里,您需要调用AsyncTask.publishProgress()
并在该系统调用UI线程中的AsyncTask.onProgressUpdate()
之后。覆盖AsyncTask.onProgressUpdate()
以显示用户界面的进度(将级别设置为进度条或其他内容)。
最后将在UI线程中调用AsyncTask.onPostExecute()
。重写此方法以显示在AsyncTask.doInBackground()
中处理的数据,并根据需要修改视图。