本文主要是介绍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();}}
这篇关于NestedScrollView嵌套RecyclerView,firstVisible和lastVisible不好用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!