2012-01-02 6 views
4

イメージとテキストビューが入っているframelayoutを再利用しようとしていますが、私はこのようなことはしていないと思います。コードは機能し、表示は正しいですが、パフォーマンスは非常に悪く、アダプターがアイテムの位置に戻るたびに新しいImageViewとTextViewを作成するためです。Android:埋め込みビューをgridviewで再利用する[ソリューション投稿]

新しいオブジェクトを作成せずに、埋め込まれたImageView(i)とTextView(t)を再利用する方法を教えてもらえますか?私はJavaにとって非常に新しく、これはAndroidアプリケーションを構築しようとする私の試みです。

 public View getView(int position, View convertView, ViewGroup parent) { 

      FrameLayout F; 
      FrameLayout ImageBorder; 
      FrameLayout TextBG; 

      ImageView i; 
      TextView t; 

      if(convertView == null) { 
       F = new FrameLayout(mContext); 

      } else { 
       F = (FrameLayout) convertView; 
      } 

      ImageBorder = new FrameLayout(F.getContext()); 
      FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,300,Gravity.BOTTOM); 
      ImageBorder.setLayoutParams(params1); 

      i = new ImageView(F.getContext()); 
      TextBG = new FrameLayout(F.getContext()); 
      t = new TextView(F.getContext()); 

      F.setBackgroundColor(Color.BLACK); 
      ImageBorder.setPadding(2, 2, 2, 2); 
      ImageBorder.setBackgroundColor(Color.BLACK); 

      FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,40,Gravity.BOTTOM); 

      TextBG.setLayoutParams(params); 
      TextBG.setBackgroundColor(Color.BLACK); 
      TextBG.setAlpha(.6f); 

      t.setLayoutParams(params); 

      t.setGravity(Gravity.CENTER_VERTICAL); 

      String pathToPhoto = FileList.get(position).toString(); 
      String fileDescription = pathToPhoto.replaceAll("/mnt/external1/PaliPhotography/",""); 

      fileDescription = fileDescription.replaceAll(".jpg",""); 
      fileDescription = fileDescription.toUpperCase(); 


      Bitmap bm = Cache.getCacheFile("thumb",pathToPhoto); 

      if (bm == null) { 
       ImageDownloader downloader = new ImageDownloader(i); 
       downloader.execute("thumb", pathToPhoto, "400", "400"); 
      } else { 

       i.setImageBitmap(bm); 
       i.setScaleType(ImageView.ScaleType.CENTER_CROP); 

       t.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); 
       t.setText(" " + fileDescription); 

      } 

      ImageBorder.addView(i); 
      ImageBorder.addView(TextBG); 
      ImageBorder.addView(t); 

      F.addView(ImageBorder); 

      return F; 

     } 
    } 

ありがとうございます!

[EDIT]

--------------------------- SOLUTION -------- ---------------------------------------------

私は以下のフィードバックに基づいて実装したソリューションです!ありがとうございました!

 public View getView(int position, View convertView, ViewGroup parent) { 

      View ReturnThisView; 
      ViewHolder holder; 

      LayoutInflater inflater; 
      holder = new ViewHolder(); 

      if(convertView == null) { 
       inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       ReturnThisView = inflater.inflate(R.layout.imagecell, null); 
       ReturnThisView.setTag(holder); 
      } else { 
       ReturnThisView = convertView; 
      } 

      holder.TextDescription = (TextView) ReturnThisView.findViewById(R.id.PhotoDesc); 
      holder.ImageThumbnail = (ImageView) ReturnThisView.findViewById(R.id.Thumbnail); 

      String pathToPhoto = FileList.get(position).toString(); 
      String fileDescription = pathToPhoto.replaceAll("/mnt/external1/PaliPhotography/",""); 

      fileDescription = fileDescription.replaceAll(".jpg",""); 
      fileDescription = fileDescription.toUpperCase(); 

      Bitmap bm = Cache.getCacheFile("thumb",pathToPhoto); 

      if (bm == null) { 
       ImageDownloader downloader = new ImageDownloader(holder.ImageThumbnail); 
       downloader.execute("thumb", pathToPhoto, "400", "400"); 
      } else { 

       holder.ImageThumbnail.setImageBitmap(bm); 
       holder.ImageThumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP); 

       holder.TextDescription.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); 
       holder.TextDescription.setText(" " + fileDescription); 

      } 

      return ReturnThisView; 
     } 
    } 

    static class ViewHolder { 
    TextView TextDescription; 
    ImageView ImageThumbnail; 
} 

答えて

2
  1. 代わりに動的要素ごとに、あなたのビューを作成するためのXMLレイアウトファイルを作成し、row.xmlを言います。あなたはconvertView == null
  2. があなたの新たに発見されたのTextViewとImageViewの
  3. 保存ホルダーへの参照を保持するのに役立ちますホルダーオブジェクトを作成するビュー#findViewByIdを使用してのTextViewとImageViewのを見つけインフレータを使用して、新しい行を膨らませることを検出した場合
  4. そうconvertView.setTag(holder)
  5. convertViewは、これら2つの保存されたオブジェクトのためのholder = convertView.getTag()
  6. セットテキストとイメージをすることによってホルダーオブジェクトを見つけ、既存のタグ、例えばなどholder.txt.setText("Foo")
  7. Androidのアダプタが膨張した行
  8. おそらくも、あなたは要素の再初期化を避けるために、ビューの初期化と一度レイアウトや使用ホルダーパターンを行うことができ、あなたのコードのための

のインスタンスを再利用する限り、残りを行いますが、私はXMLがあなたに良い経験を与えると思う。

+0

ご協力いただきありがとうございます。私はこのソリューションの半分がすでに実装されているが、ホルダーオブジェクトが何であるか、実装する方法がわからない。私は画像とテキストの両方のビューをそれ自身で保持できるオブジェクトですか?私は説明を検索しようとしましたが、ホルダーオブジェクトの使用に関する良い情報は見つかりませんでした。 – Apachi2K

+0

気にしないで、説明した例が見つかりました! – Apachi2K

+1

ちょうど繰り返す - ホルダー(またはそれを与えることに決めた名前)は、参照を保持する単純なPOJO(データ)オブジェクトです。複雑な状況では、私は通常、reset()メソッド – Bostone

0

あなたが提供した解決策は正確ではありません。 ViewHolderは主にビューの膨張を避け、findViewById()を複数回呼び出すのを避けるために使用されます。あなたのコードで

  • 新しいホルダーオブジェクトを毎回作成しないでください!代わりに、convertView = nullの場合にのみ新しいビュー所有者を作成し、ReturnThisViewタグに保存する必要があります。 convertViewがnullでない場合は、以前に保存したビューホールダーをロードする必要があります。

  • あなたはビューを膨らませないようにしていますので、うまくいきましたが、まだfindViewById()を使用しています。 convertView = nullの場合にのみfindViewById()を使用し、ビュー所有者のビューを初期化しています。 convertViewがnullでない場合は、ロードしたviewHolderオブジェクトで保存されたビューを使用します。ここで

例です:

public View getView(int position, View convertView, ViewGroup parent) { 

     View ReturnThisView; 
     ViewHolder holder; 

     LayoutInflater inflater; 


     if(convertView == null) { 
      inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      ReturnThisView = inflater.inflate(R.layout.imagecell, null); 
      holder = new ViewHolder(); 
      holder.TextDescription = (TextView) ReturnThisView.findViewById(R.id.PhotoDesc); 
      holder.ImageThumbnail = (ImageView) ReturnThisView.findViewById(R.id.Thumbnail); 
      ReturnThisView.setTag(holder); 
     } else { 
      ReturnThisView = convertView; 
      holder = (ViewHolder)ReturnThisView.getTag(); 
     } 



     String pathToPhoto = FileList.get(position).toString(); 
     String fileDescription = pathToPhoto.replaceAll("/mnt/external1/PaliPhotography/",""); 

     fileDescription = fileDescription.replaceAll(".jpg",""); 
     fileDescription = fileDescription.toUpperCase(); 

     Bitmap bm = Cache.getCacheFile("thumb",pathToPhoto); 

     if (bm == null) { 
      ImageDownloader downloader = new ImageDownloader(holder.ImageThumbnail); 
      downloader.execute("thumb", pathToPhoto, "400", "400"); 
     } else { 

      holder.ImageThumbnail.setImageBitmap(bm); 
      holder.ImageThumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP); 

      holder.TextDescription.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large); 
      holder.TextDescription.setText(" " + fileDescription); 

     } 

     return ReturnThisView; 
    } 
} 

static class ViewHolder { 
TextView TextDescription; 
ImageView ImageThumbnail; 

}

私はこのsiteからそれを学びました。

関連する問題