0

我试图理解缓存的概念。listview当我向上或向下滚动时,我的滞后。我尝试了解有关此的 android 教程。但这对我没有多大帮助。

正如帮助我的人所说,我改变了对 viewHolder 的看法。

但现在我遇到了一个新错误。

返回类型不兼容ArrayAdapter<Movie>.getView(int, View, ViewGroup)

public class MovieRatingsActivity extends ListActivity
{
    private ArrayList<Movie> movies = new ArrayList<Movie>();
    private LayoutInflater mInflater;
    private LruCache<String, Bitmap> cache;
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeUI();
    }

    private void initializeUI()
    {
        mInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
        InputStream inputStream = getResources().openRawResource(   R.raw.ratings);
        movies = Movie.loadFromFile(inputStream);       
        setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));
    }

    /** Custom row adatper -- that displays an icon next to the movie name */
    class RowIconAdapter extends ArrayAdapter<Movie> 
    {
        private ArrayList<Movie> movies;        
        public RowIconAdapter(Context c, int rowResourceId, int textViewResourceId, 
                ArrayList<Movie> items)
        {
            super(c, rowResourceId, textViewResourceId, items);
            movies  = items;
        }
        /*
        public View getView(int pos, View convertView, ViewGroup parent)
        {
            View row = mInflater.inflate(R.layout.listrow, parent, false);
            Movie currMovie = movies.get(pos);


            if (currMovie != null)
            {
                ImageView icon = (ImageView) row.findViewById(R.id.row_icon);
                TextView movieText = (TextView) row.findViewById(R.id.row_label);
                TextView votesText = (TextView) row.findViewById(R.id.row_subtext);
                movieText.setText(currMovie.getName());
                String votesStr = currMovie.getVotes()+" votes";
                votesText.setText(votesStr);
                Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating());
                icon.setImageBitmap(movieIcon);
                Log.w("MVMVMVMVMVMV", "Creating row view at position "+pos+" movie "+currMovie.getName());
            }


            return row;         
        }
    }
    */
    /*NEW CODE WHERE ERROR IS THROWN*/

    public ViewHolder getView(int pos, View convertView, ViewGroup parent)
    {
        ViewHolder holder = new ViewHolder();

        Movie currMovie = movies.get(pos);

        if (currMovie != null)
        {
            holder.icon = (ImageView) convertView.findViewById(R.id.row_icon);
            holder.movieText = (TextView) convertView.findViewById(R.id.row_label);
            holder.votesText = (TextView) convertView.findViewById(R.id.row_subtext);
            holder.movieText.setText(currMovie.getName());
            String votesStr = currMovie.getVotes()+" votes";
            holder.votesText.setText(votesStr);
            Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating());
            holder.icon.setImageBitmap(movieIcon);
            Log.w("MVMVMVMVMVMV", "Creating row view at position "+pos+" movie "+currMovie.getName());
        }
        return holder;
    }
}

/** Creates a unique movie icon based on name and rating */
private Bitmap getMovieIcon(String movieName, String movieRating)
{
    int bgColor = getColor(movieName);
    Bitmap b = Bitmap.createBitmap(48, 48, Bitmap.Config.ARGB_8888);
    b.eraseColor(bgColor); // fill bitmap with the color
    Canvas c = new Canvas(b);
    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setColor(getTextColor(bgColor));
    p.setTextSize(24.0f);
    c.drawText(movieRating, 8, 32, p);
    return b;
}

/** Construct a color from a movie name */
private int getColor(String name)
{
    String hex = toHexString(name);
    String red = "#"+hex.substring(0,2);
    String green = "#"+hex.substring(2,4);
    String blue = "#"+hex.substring(4,6);
    String alpha = "#"+hex.substring(6,8);
    int color = Color.argb(Integer.decode(alpha), Integer.decode(red), 
                            Integer.decode(green), Integer.decode(blue));
    return color;
}

/** Given a movie name -- generate a hex value from its hashcode */
private String toHexString(String name)
{
    int hc = name.hashCode();
    String hex = Integer.toHexString(hc);
    if (hex.length() < 8)
    {
        hex = hex+hex+hex;
        hex = hex.substring(0,8); // use default color value
    }
    return hex;
}

/** Crude optimization to obtain a contrasting color -- does not work well yet */
private int getTextColor(int bg)
{

    int r = Color.red(bg);
    int g = Color.green(bg);
    int b = Color.blue(bg);
    String hex = Integer.toHexString(r)+Integer.toHexString(g);
    hex += Integer.toHexString(b);

    int cDec = Integer.decode("#"+hex);
    if (cDec > 0xFFFFFF/2)  // go dark for lighter shades
        return Color.rgb(0, 0, 0);
    else
    {
        r = (r+128)%256;
        g = (g+128)%256;
        b = (b+128)%256;
        return Color.rgb(r,g,b);
    }
}
4

2 回答 2

2

有一些事情可以提高性能。

  1. 研究使用ViewHolder 模式,以防止findViewById()调用
  2. 通过实现 ViewHolder 模式,您还可以消除布局的不必要膨胀
  3. 创建一个绘画对象是昂贵的实例化一个 onCreate 并通过更新更改的属性来重用它。
于 2013-05-20T05:03:43.633 回答
1

根据您的代码,您每次调用 getview 时都会增加行,并且您还在检查视图是否为空

View row=null;
if(convertView==null)
{
    row = mInflater.inflate(R.layout.listrow, parent, false);
}

您还必须使用支架模式才能平滑滚动

关联

于 2013-05-20T05:04:15.863 回答