zoukankan      html  css  js  c++  java
  • ListView只更新某个item


    方案1:

    针对需要更新的item调用public View getView(int position, View convertView, ViewGroup parent)即可。

    如:
    public class AuthorListAdapter extends BaseAdapter {
    
        ...
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ...
            return convertView;
        }
    
        /**
         * 更新Item视图,减少不必要的重绘
         *
         * @param listView
         * @param position
         */
        public void updateItemView(ListView listView, int position) {
            //换算成 Item View 在 ViewGroup 中的 index
            int index = position - listView.getFirstVisiblePosition();
            if (index >= 0 && index < listView.getChildCount()) {
                //更新数据
                AuthorInfo authorInfo = mAuthorInfoList.get(position);
                authorInfo.setNickName("Google Android");
                authorInfo.setMotto("My name is Android .");
                authorInfo.setPortrait(R.mipmap.ic_launcher);
                //更新单个Item
                View itemView = listView.getChildAt(index);
                getView(position, itemView, listView);
            }
        }
    
    }
    

     注意:在ListView中,getChildAt(int position)返回的item是指的可视区域内的第position个元素。使用getChildAt(index)的取值,只能是当前可见区域(列表可滚动)的子项! 


        即取值范围在 >= ListView.getFirstVisiblePosition() &&  <= ListView.getLastVisiblePosition();

     这里涉及到listview的复用原理,简单来说:一个元素很多的listview,如果页面的可视区域内最多可以看到6个元素,假如它们的index是0-5,那么在内存中只有6个item对象。当第7个item(即index=6)进入到可视区域的时候,那么其实是在复用index=0的item。由于在滑动时getView这个更新控件的方法执行非常频繁,所以肉眼是丝毫看不出这种复用的。

    方案2
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = super.getView(position, convertView, parent);
        view.setTag(getItemId(position));
        return view;
    }
    
    For the update check every element of list, if a view with given id is there it's visible so we perform the update.
    private void update(long id)
    {
    
        int c = list.getChildCount();
        for (int i = 0; i < c; i++)
        {
            View view = list.getChildAt(i);
            if ((Long)view.getTag() == id)
            {
                // update view
            }
        }
    }

    It's actually easier than other methods and better when you dealing with ids not positions! Also you must call update for items which get visible.

  • 相关阅读:
    AspNet Core 核心 通过依赖注入(注入服务)
    AspNetCore 中使用 InentityServer4(2)
    AspNetCore中使用Ocelot之 IdentityServer4(1)
    AspNetCore+Swagger 生成Model 描述
    scp带密码拷贝文件
    redis集群之节点少于六个错误-解决
    [AWS DA Guru] Serverless compute Exam Tips
    [AWS] Lambda Invocation types
    [AWS Developer Guru] CI/CD
    [AWS] Lab: CloudFormation nested stack
  • 原文地址:https://www.cnblogs.com/ryq2014/p/5973253.html
Copyright © 2011-2022 走看看