1

グリッドビューにいくつかの画像を表示するためのカスタム アダプターがあります。

public class CustomAdapter extends BaseAdapter {


    private Context context;

    ArrayList<String> list = null;


    public CustomAdapter (Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    l
    public int getCount() {
        return list.size();
    }


    public Object getItem(int paramInt) {
        return paramInt;
    }


    public long getItemId(int paramInt) {
        return paramInt;

    }


    public View getView(int position, View child, ViewGroup parent) {
        String string= list.get(position);
        Bitmap bitmap = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.grid_item, null);
        RelativeLayout parentLayout = (RelativeLayout) view
                .findViewById(R.id.parentLayout);

        ImageView iView = (ImageView) view.findViewById(R.id.imageView);
        final ProgressBar progress = (ProgressBar) view.findViewById(R.id.progress);

        if (string != null) {
            bitmap = BitmapFactory.decodeFile(string);
        } else {
            bitmap = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.img_loading);
        }
        iView.setImageBitmap(bitmap);
        iView.setTag(position);

        return view;
    }


}

これは gridview 用のアダプターです。gridview アイテムを選択すると、対応するファイルがダウンロードされ、プログレスバーが表示されます。しかし、notifyDatasetChanged() を呼び出すと、プログレスバーは初期状態を保持します。

notifyDatasetChanged() が呼び出された場合でも、プログレスバーの状態/進行状況を保持/表示するにはどうすればよいですか?

ありがとう

4

1 に答える 1

0

notifyDatasetChanged() を実行すると、リストに表示されているすべてのアイテムに対して getView が呼び出されます。新しいビューであるため、進行状況が破棄されます。最適化の 1 つは、convertView を使用して、convertView が前のビューと同じかどうかを (リストからの文字列値で) チェックすることです。ほとんどの場合、リストを移動しない場合、 convertView はまったく同じビューである必要があり、変更を加えて返すことができます。同じプログレスバーになるので、進行状況が失われることはありません。すべてのケースで適切に機能させるには、現在ダウンロードされているすべてのアイテムの進行状況を覚えておく必要があります (たとえば、文字列のハッシュマップ、整数、名前 -> 進行状況)、getView メソッドで現在の進行状況を取得します。

getView(...){
   String string= list.get(position);
   Integer progress = map.get(string);
   if (progress != null){
      final ProgressBar progress = (ProgressBar) view.findViewById(R.id.progress);
      progress.setProgress(progress);
   }
   ....
}

PS。あなたのコードでは、次のように表示されます。

public View getView(int position, View child, ViewGroup parent)

getView の 2 番目のパラメーターは「子」ではなく「convertView」であり、リストの最適化に使用されます。主な考え方は、convertView が null の場合にのみビューをインフレートする必要があるということです。それ以外の場合は、ビューを更新して使用する必要があります。これは常に画面から消えるビューです。

編集:1つのことを忘れていました。ダウンロード中にプログレスバーを更新すると思います。ダウンローダー タスクは、彼が更新する ProgressBar への参照を保持します。彼に新しいプログレスバーを渡すか、彼が使用しているものを保存し (たとえば、Integer の代わりに HashMap String -> ProgressBar)、getView メソッドで何らかの方法で使用する必要があります。たとえば、addChild ...これが常にProgressBarの同じインスタンスであることを確認すると、すべてが正常に機能します:)

于 2012-11-02T07:55:12.017 に答える