0

我的 android 应用程序中有一个 ListView,它非常滞后。我在 ListView 中有一些图像,它们非常大,所以这可能就是问题所在。但是我需要它们很大,所以我想知道是否有什么办法可以让它们保持很大,而不会有所有的滞后?希望你能理解。这是我的代码。

package com.xxx.xxx;

import java.util.List;
import java.util.Vector;

import com.xxx.xxx.R;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;

public static final String[] title = new String[] {
        "IMPROSCENE", 
        "RASMUS WALTER",
        "VIL DU STUDERE I UDLANDET?",
        "INDIANS",
        "BAZAR’O’RAMA", 
        "ON AIR FESTIVAL",
        "DEN SORTE SKOLE",
        "THE FLOOR IS MADE OF LAVA",
        "The Punch Comedy Club",
        "HIGHASAKITE (N)",
        "SLIM CESSNA'S AUTO CLUB (US)", 
        "MARIE KEY",
        "WOA Metal Battle Tour",
        "FRITZ KALKBRENNER",
        "BARBARA MOLEKO", 
        "WHEN SAINTS GO MACHINE",};

public static final String[] detail = new String[] {
    "Tir 09/04 KL. 20:00 \nGratis Adgang",
    "Tor 11/04 KL. 21:00 \nEntré: Kr. 135 + evt. gebyr",
    "Tor 11/04 KL. 13:00 \nGratis Adgang",
    "Fre 12/04 KL. 21:00 \nEntré: Kr. 80 + evt. gebyr",
    "Søn 14/04 KL. 11:00 \nGratis Adgang",
    "Lør 20/04 KL. 15:00 \nGratis Adgang",
    "Tor 25/04 KL. 21:00 \nEntré: Kr. 130 + evt. gebyr",
    "Lør 27/04 KL. 21:00 \nEntré: Kr. 140 + evt. gebyr",
    "Tir 30/04 KL. 20:00 \nGratis Adgang", 
    "Tor 02/05 KL. 21:00 \nEntré: Kr. 80 + evt. gebyr",
    "Fre 03/05 KL. 22:00 \nEntré: Kr. 70 + evt. gebyr",
    "Lør 04/05 KL. 21:00 \nEntré: Kr. 130 + evt. gebyr",
    "Fre 10/05 KL. 21:00 \nEntré: Kr. 75 + evt. gebyr",
    "Lør 25/05 KL. 22:00 \nEntré: Kr. 100 + evt. gebyr",
    "Fre 13/09 KL. 21:00 \nEntré: Kr. 110 + evt. gebyr",
    "Lør 12/10 KL. 21:00 \nEntré: Kr. 140 + evt. gebyr",};

public static Integer[] imgid = { 
    R.drawable.1, R.drawable.2, 
    R.drawable.3, R.drawable.4, 
    R.drawable.5, R.drawable.6, 
    R.drawable.7, R.drawable.8,
    R.drawable.9, R.drawable.10, 
    R.drawable.11, R.drawable.12, 
    R.drawable.13, R.drawable.14, 
    R.drawable.15, R.drawable.16,
    R.drawable.17, R.drawable.18, 
    R.drawable.19, R.drawable.20, 

};

public static final String[] footer = new String[] {
    "\nTEXT", 
    "\nTEXT",
    "\nTEXT",
    "\nTEXT,
            "\nTEXT", 
    "\nTEXT",
    "\nTEXT",
    "\nTEXT,
            "\nTEXT", 
    "\nTEXT",
    "\nTEXT",
    "\nTEXT,
            "\nTEXT", 
    "\nTEXT",
    "\nTEXT",
    "\nTEXT,
    }; 

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mInflater = (LayoutInflater)                              getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    data = new Vector<RowData>();
    for (int i = 0; i < title.length; i++) {

        try {
            rd = new RowData(i, title[i], detail[i]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        data.add(rd);
    }
    CustomAdapter adapter = new CustomAdapter(this, R.layout.list_item,         R.id.title, data);
    setListAdapter(adapter);
    getListView().setTextFilterEnabled(true);
}

public void onListItemClick(ListView parent, View v, int position, long id) {
    Intent intent = new Intent(this, NewActivity.class);
    intent.putExtra("position", position);
    startActivity(intent);
}

private class RowData {
    protected int mId;
    protected String mTitle;
    protected String mDetail;

    RowData(int id, String title, String detail) {
        mId = id;
        mTitle = title;
        mDetail = detail;
    }

    @Override
    public String toString() {
        return mId + " " + mTitle + " " + mDetail;
    }
}

private class CustomAdapter extends ArrayAdapter<RowData> {

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<RowData> objects) {

        super(context, resource, textViewResourceId, objects);
    }

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

        ViewHolder holder = null;
        TextView title = null;
        TextView detail = null;
        ImageView i11 = null;
        RowData rowData = getItem(position);
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        title = holder.gettitle();
        title.setText(rowData.mTitle);
        detail = holder.getdetail();
        detail.setText(rowData.mDetail);

        i11 = holder.getImage();
        i11.setImageResource(imgid[rowData.mId]);
        return convertView;
    }

    private class ViewHolder {
        private View mRow;
        private TextView title = null;
        private TextView detail = null;
        private ImageView i11 = null;

        public ViewHolder(View row) {
            mRow = row;
        }

        public TextView gettitle() {
            if (null == title) {
                title = (TextView) mRow.findViewById(R.id.title);
            }
            return title;
        }

        public TextView getdetail() {
            if (null == detail) {
                detail = (TextView) mRow.findViewById(R.id.detail);
            }
            return detail;
        }

        public ImageView getImage() {
            if (null == i11) {
                i11 = (ImageView) mRow.findViewById(R.id.img);
            }
            return i11;
        }
    }
}
}
4

2 回答 2

1

可能有几个原因(大图像,元素数量......)可能会影响列表视图的一般性能。

您可能应该缓存所有图像数据(在 sdcard 上)并使用加载器加载图像(这里是一个示例)。

我可以向你推荐这个视频,还有一些其他技巧可以用 android 优化 listview。

于 2013-06-01T12:35:46.627 回答
0

你可以通用图像加载器。所以它会处理你所有的负担。

关联

于 2013-06-01T11:30:07.357 回答