使用开源控件PinnedSectionListView(头部驻留)的时候低版本api出现ClassCastException异常

    xiaoxiao2021-03-26  21

    问题描述:

        使用开源控件 PinnedSectionListView的时候,在android4.0以上显示正常,但在android4.0及以下滑动ListView的时候出现 java.lang.ClassCastException: android.view.ViewGroup$LayoutParams异常,异常信息如下:

    问题原因:(参考链接1:https://my.oschina.net/yulin/blog/378292,参考链接2:http://blog.csdn.net/zhangphil/article/details/50054555

        主要原因是因为 generateDefaultLayoutParams()方法在android4.0和android4.0以上返回的结果不一样, 低版本的generateDefaultLayoutParams生成的是ViewGroup的layoutparams, 如果强转成AbsListView.LayoutParams会出现 java.lang.ClassCastException: android.view.ViewGroup$LayoutParams异常

    解决方案:

        在 PinnedSectionListView中重写 generateDefaultLayoutParams()方法:      // add by Zhang Phil @      @Override      protected ViewGroup.LayoutParams generateDefaultLayoutParams() {          LayoutParams mLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);          return mLayoutParams;      }
    转载请注明原文地址: https://ju.6miu.com/read-660104.html

    最新回复(0)