zoukankan      html  css  js  c++  java
  • [Android] ListView关于adapter多种view设置

    使用的关键点是在adapter覆盖两个方法

    public int getItemViewType(int position)

    public int getViewTypeCount()

    其它的可另google,这里要说个重点的问题。

    我做多个view,总是会报错

    java.lang.ArrayIndexOutOfBoundsException
      at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:3523)
      at android.widget.ListView.measureHeightOfChildren(ListView.java:1158)
      at android.widget.ListView.onMeasure(ListView.java:1060)
      at android.view.View.measure(View.java:7703)

    找了很久很久,有一篇外国文章说到问题所在:

    http://stackoverflow.com/questions/2596547/arrayindexoutofboundsexception-with-custom-android-adapter-for-multiple-views-in

    getItemViewType() is >= getViewTypeCount()

    看懂一点,就是不能大于视图数量,后再继续找到这文章

    http://blog.csdn.net/clarketang/article/details/6873393

    其中,关键点说到了原因:

    当ListView要显示某一项时,getItemViewType方法被调用,根据返回值在mRecycler搜索得到缓存的视图。这也是为什么getViewTypeCount返回值要比定义的视图类型常量值大的原因,否则会导致数组越界异常。

    Have fun with Android!

  • 相关阅读:
    查找算法
    Android IPC
    运输层和TCP/IP协议
    二叉树的学习笔记
    java 和 JVM
    转载: GIt远程操作详解
    java-jpa-criteriaBuilder使用
    java项目构建工具Maven
    虚拟机下安装Maven
    validate表单验证-单独验证
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/6075316.html
Copyright © 2011-2022 走看看