NestedScrollView嵌套RecyclerView,firstVisible和lastVisible不好用

因为nestedScrollView嵌套RecyclerView之后,RecyclerView计算的高度大小最总大小,因此firstvisible永远是0,last永远是最后一个。

 

为了实现视频滑出屏幕就停止播放,只需要监听NestedScrollView的滑动距离,然后获取子View的位置,判断是否在范围内即可。

 

package com.bizrun.hibao.personalzone;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bizrun.hibao.utils.ScreenUtils;
import com.bizrun.hibao.view.HeadZoomNestScrollview;

/**
 * Discription:必须设置nestedScrollHeight
 *
 * @author sz
 * @date 2018/12/21
 */

public class HfNestedRecyclerViewUtil {
    private static final String TAG = HfNestedRecyclerViewUtil.class.getSimpleName();
    int hfOffsetY = 0;
    RecyclerView.LayoutManager layoutManager;
    //外层包裹的nestedScrollView的高度
    private HeadZoomNestScrollview headZoomNestScrollview;

    public HfNestedRecyclerViewUtil(RecyclerView.LayoutManager layoutManager) {
        this.layoutManager = layoutManager;
    }

    /**
     * 设置外层高度
     * @param nestedScrollView
     */
    public void setNestedScrollView(HeadZoomNestScrollview nestedScrollView) {
        this.headZoomNestScrollview = nestedScrollView;
    }


    /**
     * 设置偏移量
     * @param nestedScrollOffset
     */
    public void setOffset(int nestedScrollOffset){
        this.hfOffsetY = nestedScrollOffset;
    }

    /**
     * 判断child是否可见
     * @param child
     * @return
     */
    public boolean isChildVisible(View child){
        return this.isChildVisible(child, hfOffsetY);
    }

    /**
     * 判断当前View是否展示
     * @param child
     * @param nestedScrollOffset
     */
    public boolean isChildVisible(View child, int nestedScrollOffset){
        this.hfOffsetY = nestedScrollOffset;

        int start = getParentStart();
        int end = getParentEnd();
        int childStart = getChildStart(child);
        int childEnd = getChildEnd(child);

        if (childEnd > start && childStart < end){
            return true;
        }

        return false;
    }

    /**
     * 获取child的结束位置
     * @param view
     * @return
     */
    public int getChildEnd(View view) {
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
                view.getLayoutParams();
        return layoutManager.getDecoratedBottom(view) + params.bottomMargin + + hfOffsetY;
    }

    /**
     * 获取child的开始位置
     * @param view
     * @return
     */
    public int getChildStart(View view) {
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
                view.getLayoutParams();
        return getDecoratedTop(view) - params.topMargin;
    }

    /**
     * 获取child的布局位置
     * @param child
     * @return
     */
    public int getDecoratedTop(View child) {
        return child.getTop() - layoutManager.getTopDecorationHeight(child) + hfOffsetY;
    }

    /**
     *
     * @return
     */
    public int getParentStart() {
        //48是导航高度
        return hfOffsetY > ScreenUtils.dip2px(48) ? hfOffsetY:ScreenUtils.dip2px(48);
    }

    public int getParentEnd() {
        //offset 滑动减头部
        if (headZoomNestScrollview == null){
            return 0;
        }

        return headZoomNestScrollview.getHeight();
    }


}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值