因为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();
}
}