zoukankan      html  css  js  c++  java
  • 提升布局性能____Making ListView Scrolling Smooth

         listview是一个比较重要的UI组件,一切影响UI的操作,比如适配器从磁盘、网络或者数据库中加载数据的操作,最好都放在子线程中完成。子线程可以使用thread,不过那样比较老土,官方推荐使用AsyncTask。

         AsyncTask会自动排队 execute() 任务,并且顺序执行。你的应用进程只需使用它就是了。

        

    // Using an AsyncTask to load the slow images in a background thread
    new AsyncTask<ViewHolder, Void, Bitmap>() {
        // ViewHolder缓存着适配器中每一个convertView上的可视组件
    private ViewHolder v; @Override protected Bitmap doInBackground(ViewHolder... params) { v = params[0]; // 以某种方式获取到了bitmap,返回该图片
    return mFakeImageLoader.getImage(); } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (v.position == position) { // If this item hasn't been recycled already, hide the // progress and set and show the image // 每一个converView上的等待提示消失
    v.progress.setVisibility(View.GONE); // ImageView可见
    v.icon.setVisibility(View.VISIBLE); v.icon.setImageBitmap(result); } } }.execute(holder);

    通过ViewHolder,可以避免由于findViewById()带来的性能瓶颈。

    static class ViewHolder {
      TextView text;
      TextView timestamp;
      ImageView icon;
      ProgressBar progress;
      int position;
    }
    ViewHolder holder = new ViewHolder();
    holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
    holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
    holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
    holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
    convertView.setTag(holder);

    再举个例子,来自于http://blog.csdn.net/fmoonstar/article/details/7748857

    当每个ListView的item的converView为空时,将每一个小组件缓存在ViewHolder上,当convertView重复利用时,则取之前已经缓存的小组件,提高ListView的滚动性能。

  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/itblog/p/2812058.html
Copyright © 2011-2022 走看看