gpt4 book ai didi

java - AsyncTask 卡住 UI

转载 作者:行者123 更新时间:2023-12-01 15:26:44 37 4
gpt4 key购买 nike


我有一个 AsyncTask 那么多 rss 信息,然后将其应用到 UI。我有一个加载过程指示器,当它在后台加载信息时,UI 运行顺利,但是当我调用 PublishProgress() 以便编辑 UI 时,指示器卡住了。这里有一些代码可以更好地解释它:

                class LoadRss extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...voids) {
changeRSS(checkedId);
publishProgress();
return null;
}
protected void onPostExecute(Void voids) {

frameAnimation.stop();
syncHomeSymbol.setImageResource(R.drawable.none);
syncHomeSymbol.setBackgroundColor(color.background_dark);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
InitHome();
}
}
new LoadRss().execute();

changeRSS 是提取 rss 信息的方法。
InitHome 是更改 UI 的方法。


非常感谢!

初始化首页

private void InitHome() {
if (isNetworkAvailable()) {
homeLayout.removeAllViews();
int enclosureCounter = 0;
for (int i=0;i<rssRes.getLength();i++) {
String title;
final String link;
String description, pubDate;
EnclosureItem enclosure;
title = rssRes.getTitle()[i];
link = rssRes.getLink()[i];
description = rssRes.getDescription()[i];
pubDate = rssRes.getPubDate()[i];
try {
enclosure = rssRes.getEnclosure().get(enclosureCounter);
if (enclosure.getPos() == i) {
LinearLayout horizontal = new LinearLayout(this);
horizontal.setOrientation(LinearLayout.HORIZONTAL);
horizontal.setBackgroundResource(R.drawable.rounded_layout);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(10, 10, 10, 10);
horizontal.setLayoutParams(p);
horizontal.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(browserIntent);
}
});
ImageView img = new ImageView(this);
Drawable drawable = LoadImageFromWebOperations(enclosure.getUrl());
img.setImageDrawable(drawable);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(120, LinearLayout.LayoutParams.FILL_PARENT);
img.setLayoutParams(layoutParams);
img.setScaleType(ScaleType.CENTER_CROP);
horizontal.addView(img);
LinearLayout vertical = new LinearLayout(this);
vertical.setOrientation(LinearLayout.VERTICAL);

TextView textTitle = new TextView(this);
if (SPM.getFix()) {
textTitle.setGravity(Gravity.LEFT);
}
else {
textTitle.setGravity(Gravity.RIGHT);
}
textTitle.setTextSize(1, 18);
textTitle.setText(Html.fromHtml("<font color='#92b64a'>"+title+"</font><br />"));
vertical.addView(textTitle);

TextView textDescription = new TextView(this);
if (SPM.getFix()) {
textDescription.setGravity(Gravity.LEFT);
}
else {
textDescription.setGravity(Gravity.RIGHT);
}
String str = Html.fromHtml(description).toString();
try{
str = str.substring(0,str.lastIndexOf("\n\n"));
}
catch (IndexOutOfBoundsException e) { }
textDescription.setText(Html.fromHtml(new RssOrganizer(str,false).getDescription()));
vertical.addView(textDescription);

TextView textpubDate = new TextView(this);
if (SPM.getFix()) {
textpubDate.setGravity(Gravity.LEFT);
}
else {
textpubDate.setGravity(Gravity.RIGHT);
}
textpubDate.setText(new RssOrganizer(pubDate,true).getPubDate()+"\n\n");
vertical.addView(textpubDate);
enclosureCounter++;
horizontal.addView(vertical);
homeLayout.addView(horizontal);
}
else {
setRegHome(i);
}
}
catch (IndexOutOfBoundsException e) {
setRegHome(i);
}
}
}

else
{
tRSS.setText(R.string.noINNERinternetConnection);
}
}

private void setRegHome(int i) {

String title;
final String link;
String description, pubDate;
title = rssRes.getTitle()[i];
link = rssRes.getLink()[i];
description = rssRes.getDescription()[i];
pubDate = rssRes.getPubDate()[i];
LinearLayout vertical = new LinearLayout(this);
vertical.setOrientation(LinearLayout.VERTICAL);
vertical.setBackgroundResource(R.drawable.rounded_layout);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(10, 10, 10, 10);
vertical.setLayoutParams(p);
vertical.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(browserIntent);
}
});
TextView textTitle = new TextView(this);
if (SPM.getFix()) {
textTitle.setGravity(Gravity.LEFT);
}
else {
textTitle.setGravity(Gravity.RIGHT);
}
textTitle.setTextSize(1, 18);
textTitle.setText(Html.fromHtml("<font color='#92b64a'>"+title+"</font><br />"));
vertical.addView(textTitle);

TextView textDescription = new TextView(this);
if (SPM.getFix()) {
textDescription.setGravity(Gravity.LEFT);
}
else {
textDescription.setGravity(Gravity.RIGHT);
}
String str = Html.fromHtml(description).toString();
try{
str = str.substring(0,str.lastIndexOf("\n\n"));
}
catch (IndexOutOfBoundsException e) { }
textDescription.setText(Html.fromHtml(new RssOrganizer(str,false).getDescription()));
vertical.addView(textDescription);

TextView textpubDate = new TextView(this);
if (SPM.getFix()) {
textpubDate.setGravity(Gravity.LEFT);
}
else {
textpubDate.setGravity(Gravity.RIGHT);
}
textpubDate.setText(new RssOrganizer(pubDate,true).getPubDate()+"\n\n");
vertical.addView(textpubDate);
homeLayout.addView(vertical);
}

最佳答案

onProgressUpdate(...) 在 UI 线程上运行,因此它不应该执行大量耗时的工作 - 这首先就无法使用 AsyncTask。

理想情况下,onProgressUpdate(...) 应仅提供基本进度更新,例如完成百分比或指示进度的简单文本消息。

用于显示进度的所有 View 都应事先创建,但是,许多人为此使用 onPreExecute()

关于java - AsyncTask 卡住 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10058123/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com