如何摆脱listview中的滞后/抖动?

时间:2013-05-20 04:57:01

标签: android listview

我试图理解缓存的概念。当我向上或向下滚动时,我的listview会滞后。我尝试了解这个Android教程。但它对我帮助不大。

正如帮助过我的人所说的那样,我改变了观点,以便观看霍尔德。

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

  

返回类型与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);
    }
}

2 个答案:

答案 0 :(得分:2)

有一些事情可以改善表现。

  1. 使用ViewHolder pattern查看,以防止findViewById()来电
  2. 通过实施ViewHolder模式,您还可以删除布局中不必要的通胀
  3. 创建一个paint对象很昂贵,实例化一个onCreate并通过更新更改的属性来重用它。

答案 1 :(得分:1)

根据你的代码,每次调用getview时你都会进行行内容,并且你也在查看null的视图

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

您还必须使用支架图案进行平滑滚动

link

相关问题