0

我有自定义列表视图。当我滚动我的列表视图时,android会保留在内存中(据我所知)显示在屏幕上的项目并且不会保留隐藏的项目(未滚动到)。

在我的情况下(我认为)保留所有列表项会比生成隐藏项更好。

那么,如何“告诉”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 中操作的每个视图,您可以在此静态类中创建一个视图。因此,如果您有 10 个 TextView,则使用 10 个 TextView 填充这个类。

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

在适配器中,您现在仅在 convertView 为空时才执行 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;
}

我不知道您的数据是如何组织的,因此您必须稍微更改此代码。

One more thing that can cause lag is the android:cacheColorHint xml attribute. Usually you set this to either the same color as you application background, or transparent. Setting it transparent have been known to cause rapid Garbage collections on some occasions.

于 2012-05-31T23:27:56.993 回答
0

您可以覆盖适配器并让它膨胀构造函数中的所有视图,然后使用 getView() 返回正确的视图。如果您将视图存储在某个数据对象(数组、列表等)中,可能会变得容易

但实际上你应该让系统像设计的那样使用 convertView。总的来说,我认为这样做会获得更好的性能。

于 2012-05-31T21:43:14.707 回答