RecyclerView 在ScrollView部分机型无法下拉显示不全(兼容问题)

来源:互联网 发布:java向数据库添加数据 编辑:程序博客网 时间:2024/05/22 07:57

本文出自:http://blog.csdn.net/dt235201314/article/details/53006062

一丶简述

上个月一篇博客没写,原因忙成狗,手上一直有任务,总结下来,能写出几篇博客

二丶问题描述

ScrollView套RecyclerView展示数据。

三星,华为等较新热门机型测试无BUG,测试组部分手机无法下拉显示更多。

三丶分析

1.部分手机无法下拉,表示兼容性问题

2.好几个页面都是这种类型但惟独两个页面不行,特点都是RecyclerView  + ScrollView不行,而ListView + ScrollView可以。

3.Android群讨论,有用信息: 1.RecyclerView  + ScrollView和ListView + ScrollView都可能会出现显示不全问题,各自需要重写对应方法

      2.RecyclerView   V7包就是解决兼容性包。

      3.RecyclerView   属性比ListView丰富,大有替代之势。

四丶博客参考

鸿洋大神的(RecyclerView基本用法与自定义)

Android 自定义RecyclerView 实现真正的Gallery效果

关于滑动冲突(这个是与问题最接近,也讲得最有道理的)

       Scrollview 嵌套 RecyclerView 及在Android 5.1版本滑动时 惯性消失问题

五丶问题解决

快发版了,处于稳定考虑,直接将RecyclerView改成ListView

注意点:重写ListView的onMeasure

public class ExpandListView extends ListView {    public ExpandListView(Context context) {        super(context);    }    public ExpandListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ExpandListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @TargetApi(Build.VERSION_CODES.LOLLIPOP)    public ExpandListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2                , MeasureSpec.AT_MOST);        super.onMeasure(widthMeasureSpec, expandSpec);    }}

ListView填充方法:

public void setListViewHeightBasedOnChildren(ListView listView) {    ListAdapter listAdapter = listView.getAdapter();    if (listAdapter == null) {        return;    }    int totalHeight = 0;    for (int i = 0; i < listAdapter.getCount(); i++) {        View listItem = listAdapter.getView(i, null, listView);        listItem.measure(0, 0);        totalHeight += listItem.getMeasuredHeight();    }    ViewGroup.LayoutParams params = listView.getLayoutParams();    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));    params.height += 5;//if without this statement,the listview will be a little short    listView.setLayoutParams(params);}

总结:第一次遇到这种问题,查阅了很多相关的解决办法,遗憾的是群里大神跟我讲解源码的时候蒙逼了

   有时间了我要去深挖源码。








0 0
原创粉丝点击