0

カスタムリストビューがあります。リストビューをスクロールすると、Androidは(私が理解している限り)画面に表示されているアイテムをメモリに保持し、非表示の(スクロールされていない)アイテムを保持しません。

私の場合 (私が思うに) すべてのリスト アイテムを保持することは、非表示のアイテムを生成するよりも優れていると思います。

では、すべてのアイテムをメモリに保持するようにアンドロイドに「伝える」方法は? (15-20 項目)。PS: リソースを浪費している場合は、試してみたいと思います。

私のアダプター(いくつかの機能):

private View newView(Context context, ViewGroup parent) {
  LayoutInflater layoutInflater = LayoutInflater.from(context);
  return layoutInflater.inflate(R.layout.myl,parent,false);
}
public View getView(int position,View convertView,ViewGroup parent) {
  View view=null;
  if(convertView!=null) view=convertView; else view=newView(context,parent);
  HashMap<String,String> d=new HashMap<String,String>();
  d=data.get(position); 
  String qweqwe=d.get("qweqwe"); //9 more lines like this.
  TextView txt=(TextView)view.findViewById(R.id.mfmf); //
  txt.setText(qweqwe); //
  txt.setTypeface(mlf); //5 more blocks of 3 lines like this.
  if (smth.equals("0")){
    view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.mvmv));
  } else {
    view.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.mvmv2));
  }
  return view;
}
4

2 に答える 2

2

わかりました。回避策でラグを修正する代わりに、ここで最適化できることがいくつかあります:)

ビューへの参照をmyl.xmlに格納できる静的クラスを実装する必要があります。myl.xmlで操作するビューごとに、この静的クラスにビューを作成します。したがって、TextViewが10個ある場合は、このクラスに10個のTextViewを入力します。

static class AdapterViewsHolder {
    TextView txt1;
    TextView txt2;
    TextView txt3;
    ...
    ImageView img1;
    ... etc etc.
}

アダプタでは、convertViewがnullの場合にのみfindViewById()呼び出しを実行するようになりました。findViewById()は安価ではないため、呼び出しの量を制限するとパフォーマンスが向上します。

private HashMap<String, String> mData;
private LayoutInflater mInflater;
private TypeFace mCustomTypeFace;

// Some contructor for passing data into the Adapter.
public BaseAdapter(HashMap<String, String> data, Context ctx) {
    mData = data;
    mInflater = LayoutInflater.from(ctx);
    mCustomTypeFace = Typeface.createFromAsset(ctx.getAssets(), "yourTypeFace.ttf");
}

public View getView(int position, View convertView, ViewGroup parent) {
    // This AdapterViewsHolder will hold references to your views, so you don't have to 
    // call findViewById() all the time :)
    AdapterViewsHolder holder;

    // Check if convertView is null
    if(convertView == null) {
        // If it is, we have to inflate a new view. You can probably use the newView() call here if you want.
        convertView = mInflater.inflate(R.layout.myl, null);

        // Initialize the holder
        holder = new AdapterViewsHolder();

        // Now we do the smart thing: We store references to the views we need, in the  holder. Just find all the views you need, by id.
        holder.txt1 = (TextView) convertView.findViewById(R.id.textview1);
        holder.txt2 = (TextView) convertView.findViewById(R.id.textview2);
        ...
        holder.img1 = (ImageView) convertView.findViewById(R.id.imageview1);

        // Store the holder in the convertViews tag
        convertView.setTag(holder);
    }
    else {
        // If convertView is not null, we can get get the holder we stored in the tag.
        // This holder now contains references to all the views we need :)
        holder = (AdapterViewsHolder) convertView.getTag();
    }



    // Now we can start assigning values to the textviews and the imageviews etc etc
    holder.txt1.setText(mData.get(position));
    ...
    holder.txt1.setTypeface(mCustomTypeFace);
    ...
    holder.img1.setImageResource("IMAGE RESOURCE HERE");  

    if(someThing.equals("sometext") {
         convertView.setBackgroundDrawable(somedrawable);
    }
    else {
         convertView.setBackgroundDrawable(someotherdrawable);
    }

    // Finally, we return the convertView
    return convertView;
}

データがどのように編成されているかわからないため、このコードを少し変更する必要があります。

ラグを引き起こす可能性のあるもう1つのことは、android:cacheColorHintxml属性です。通常、これはアプリケーションの背景と同じ色または透明に設定します。透明に設定すると、場合によってはガベージコレクションが迅速に行われることが知られています。

于 2012-05-31T23:27:56.993 に答える
0

Adapter をオーバーライドして、コンストラクター内のすべてのビューを拡張し、getView() で適切なビューを返すことができます。ビューをデータオブジェクト(配列、リストなど)に保存すると簡単になる場合があります。

しかし、実際には、システムが設計されたように convertView を使用できるようにする必要があります。全体的に、そのようにするとパフォーマンスが向上すると思います。

于 2012-05-31T21:43:14.707 に答える