2012-04-07 18 views
0


多くのrss情報を持つAsyncTaskをUIに適用しています。 私はロードプロセスのためのインジケータを持っています。バックグラウンドで情報をロードしている間、UIは円滑に実行されますが、UIを編集できるようにPublishProgress()を呼び出すと、インジケータがフリーズします。ここ は、コードのビットは、より良い、それを説明することです:AsyncTaskが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を変更するメソッドです。


ありがとうございました!

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); 
    } 
+1

'InitHome'メソッドを追加します。 – Luksprog

+0

まあ、かなり長いです。それは重いメソッドです... コードを追加... –

+0

あなたが 'publishProgress'を使うときにUIがフリーズし、メソッドが"重い "と言えば、おそらくそれを投稿したいと思うでしょう。 – Luksprog

答えて

1

onProgressUpdate(...)実行 - そもそもAsyncTaskの使用を破ります。

理想的には、onProgressUpdate(...)は、パーセント完了などの基本的な進捗状況の更新や、おそらく進行状況を示す簡単なテキストメッセージを提供するだけです。

プログレッションを表示するために使用されるすべてのビューは事前に作成する必要がありますが、多くの人がこのためにonPreExecute()を使用します。

関連する問題