美文网首页
ListView优化注意点

ListView优化注意点

作者: ModestStorm | 来源:发表于2020-06-08 20:22 被阅读0次
    ListView的优化:

    1.复用convertView,使用listview中RecycleBin机制.

    /**
             * Views that were on screen at the start of layout. This array is populated at the start of
             * layout, and at the end of layout all view in mActiveViews are moved to mScrapViews.
             * Views in mActiveViews represent a contiguous range of Views, with position of the first
             * view store in mFirstActivePosition.
             */
            private View[] mActiveViews = new View[0];
    
            /**
             * Unsorted views that can be used by the adapter as a convert view.
             */
            private ArrayList<View>[] mScrapViews;
    
    

    listview的许多view呈现在Ui上,这样的View对我们来说是可见的,可以称为OnScreen的view(也为ActionView)。
    view被上滚移出屏幕,这样的view称为offScreenView(也称为ScrapView)。
    然后ScrapView会被listview删除,而RecycleBin会将这部分保存。
    当listview底部需要显示view时会从RecycleBin里面取出一个ScrapView,减少对象的创建。

    2.使用viewHolder避免重复的调用findViewById查找id

    3.快速滑动不适合做大量异步任务,在滑动停止后进行异步请求,避免频繁的网络请求。

    4.分页加载数据,避免一次请求过多数据。

    5.getView中每次显示item都会调用,所以应该避免在里面执行耗时操作,如果是图片可以做三级缓存。

    推荐的优化处理方式:
    public View getView(int position, View convertView, ViewGroup parent) {
       3:     Log.d("MyAdapter", "Position:" + position + "---"
       4:             + String.valueOf(System.currentTimeMillis()));
       5:     ViewHolder holder;
       6:     if (convertView == null) {
       7:         final LayoutInflater inflater = (LayoutInflater) mContext
       8:                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       9:         convertView = inflater.inflate(R.layout.list_item_icon_text, null);
      10:         holder = new ViewHolder();
      11:         holder.icon = (ImageView) convertView.findViewById(R.id.icon);
      12:         holder.text = (TextView) convertView.findViewById(R.id.text);
      13:         convertView.setTag(holder);
      14:     } else {
      15:         holder = (ViewHolder) convertView.getTag();
      16:     }
      17:     holder.icon.setImageResource(R.drawable.icon);
      18:     holder.text.setText(mData[position]);
      19:     return convertView;
      20: }
      21:  
      22: static class ViewHolder {
      23:     ImageView icon;
      24:  
      25:     TextView text;
    

    ListView的RecycleBin机制-结合源码分析

    相关文章

      网友评论

          本文标题:ListView优化注意点

          本文链接:https://www.haomeiwen.com/subject/fyjdtktx.html