zoukankan      html  css  js  c++  java
  • ListView视图缓存错位问题

    由于之前写Scroller应用:ListView滑动删除遇到Item视图错位问题,观察发现第1item位置改变后,第1+10的item布局也跟着改变。假设使用ScrollView+ListView,把ListView的高度測量出来然后再滑动就不会出现错位问题,继续查看之所以间隔10,我屏幕上显示10条数据,这个就涉及到getCount()和getChildCount()问题。进一步追踪发现应该是ListView视图缓存的问题,事实上这个问题跟数据是一样的。只是我们在Adapter的getView中依据不同的position设置不同的数据,这个动作是实时改变的。getChildCount()返回的是内存中的item数目,getCount()返回的是实际的数据数目。ListView中实际的item布局也就是getChildCount()的值,假设採用測量ListView设置其高度的话,那getChildCount()等于getCount()。在ListView中有这么一段代码

    	    // Pull all children into the RecycleBin.
                // These views will be reused if possible
                final int firstPosition = mFirstPosition;
                final RecycleBin recycleBin = mRecycler;
    
                // reset the focus restoration
                View focusLayoutRestoreDirectChild = null;
    
                // Don't put header or footer views into the Recycler. Those are
                // already cached in mHeaderViews;
                if (dataChanged) {
                    for (int i = 0; i < childCount; i++) {
                        recycleBin.addScrapView(getChildAt(i), firstPosition+i);
                    }
                } else {
                    recycleBin.fillActiveViews(childCount, firstPosition);
                }
    firstPosition是第一个可见的item的位置,recycleBin.addScrapView(getChildAt(i), firstPosition+i);缓存隐藏item的时候。都是使用getChildCount()内的现有的View视图,所以解决错位问题必须採用位置的标记来处理,并且标记也不能在自己定义的item中进行,必须在外部比如自己定义ListView中记录。

    Scroller应用:ListView滑动删除中採用的是规避的方式,滑动就关闭之前打开的item,看了QQ消息列表也是滑动的话就隐藏删除button,我不知道是否规避这个问题,由于ListView内存的item数据一般大于屏幕可见的item数据,所以即使屏幕大也看不到。



  • 相关阅读:
    pandas,对dataFrame中某一个列的数据进行处理
    pandas的简单使用
    pandas,读取或存储DataFrames的数据到mysql中
    pandas,pd.ExcelWriter保存结果到已存在的excel文件中
    用命令让vbox的虚拟硬盘文件转换成vmware的vmdk
    vbox磁盘空间如何扩容
    Linux ext3/ext4数据恢复
    VirtualBox安装64位系统
    ubuntu 12.04启用休眠
    美化你的GRUB,全面支持中文(菜单、提示、帮助)适用7.04-9.04
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5329905.html
Copyright © 2011-2022 走看看