Android ScrollView嵌套listView显示不全

01、ScrollView嵌套ListView显示不全的问题 -- 方法计算条目高度

 

/**
     * scrollview嵌套listview显示不全解决
     *
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            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));
        listView.setLayoutParams(params);
    }


02、ScrollView嵌套ListView 或者GridView -- 扩展ListView或者GridView 

 

 

/**
 * 解决ScollView嵌套ListView 内容显示不全
 */

public class ScollViewListView extends ListView {
    public ScollViewListView(Context context) {
        super(context);
    }

    public ScollViewListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScollViewListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

 

03、ScrollView 自定义滑动监听

 

 

 

public class MyScrollView extends ScrollView {

    private OnScrollistener onScrollistener;

    public OnScrollistener getOnScrollistener() {
        return onScrollistener;
    }

    public void setOnScrollistener(OnScrollistener onScrollistener) {
        this.onScrollistener = onScrollistener;
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context) {
        super(context);
    }

    public interface OnScrollistener {
        void onScroll(int startY, int endY);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        onScrollistener.onScroll(oldt, t);
        super.onScrollChanged(l, t, oldl, oldt);
    }
}


使用

 

 

 

mLevelPrivilege.setOnScrollistener(new MyScrollView.OnScrollistener() {
            @Override
            public void onScroll(int startY, int endY) {
                //根据scrollview滑动更改标题栏透明度
                dynamicChangeAphla(startY, endY);
            }
        });


透明度设置

 

 

 /**
     * 根据内容窗体的移动改变标题栏背景透明度
     *
     * @param startY scrollview开始滑动的y坐标(相对值)
     * @param endY   scrollview结束滑动的y坐标(相对值)
     */
    private void dynamicChangeAphla(int startY, int endY) {
        //获取标题高度
        int titleHeight = llayoutLevelTitle.getMeasuredHeight();
        //获取背景高度
        int backHeight = rlayoutLevelTitle.getMeasuredHeight();

        //获取控件的绝对位置坐标
        int[] location = new int[2];
        rlayoutLevelTitle.getLocationInWindow(location);
        //从屏幕顶部到控件顶部的坐标位置Y
        int currentY = location[1];
        //表示回到原位(滑动到顶部)
        if (currentY >= 0) {
            llayoutLevelTitle.getBackground().mutate().setAlpha(0);
        }

        DevUtil.e("zpan","=titleHeight=" + titleHeight + "=backHeight=" + backHeight + "=currentY="+currentY);
        //颜色透明度改变
        if (currentY < titleHeight && currentY >= - (backHeight - titleHeight)) {
            //计算出滚动过程中改变的透明值
            double y = Math.abs(currentY) * 1.0;
            double height = (backHeight - titleHeight) * 1.0;
            int changeValue = (int) (y / height * 255);

            DevUtil.e("zpan", "changeValue=" + changeValue);
            //判断是向上还是向下
            if (endY > startY) {//向上;透明度值增加,实际透明度减小
                llayoutLevelTitle.getBackground().mutate().setAlpha(changeValue);
            } else if (endY < startY) {//向下;透明度值减小,实际透明度增加
                llayoutLevelTitle.getBackground().mutate().setAlpha(changeValue);
            }
        }
        //红色背景移除屏幕
        if (currentY < -(backHeight - titleHeight)) {
            llayoutLevelTitle.getBackground().mutate().setAlpha(255);
        }
    }

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值