Since the reference:
Android View exposure statistics implementation scheme
List exposure implementation
Android list exposure data statistics comprehensive analysis
/ / testing recyclerView scroll event mRecyclerView addOnScrollListener (new recyclerView. OnScrollListener () {@ Override public void OnScrollStateChanged (RecyclerView RecyclerView, int newState) {SCROLL_STATE_IDLE: stop scrolling; SCROLL_STATE_DRAGGING: // Focus: Scroll_state_Demystified: Inertial scrolling if (newState because = = RecyclerView SCROLL_STATE_IDLE | | newState because = = RecyclerView. SCROLL_STATE_DRAGGING | | newState because = = RecyclerView.SCROLL_STATE_SETTLING) { handleCurrentVisibleItems(); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); / / including the first in the list of statistics current screen visible views handleCurrentVisibleItems (); }});Copy the code
/ * * * deal with the current mRecyclerView visible on the screen of the item view * / public void handleCurrentVisibleItems () {/ / 1, the getGlobalVisibleRect (new Rect()), true means the view is visible no matter how much is visible. if (mRecyclerView == null || mRecyclerView.getVisibility() ! = View.VISIBLE || ! mRecyclerView.isShown() || ! mRecyclerView.getGlobalVisibleRect(new Rect())) { return; } range = new int[2]; range = new int[2]; range = new int[2]; int orientation = -1; RecyclerView.LayoutManager manager = mRecyclerView.getLayoutManager(); if (manager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) manager; range = findRangeLinear(linearLayoutManager); orientation = linearLayoutManager.getOrientation(); } else if (manager instanceof GridLayoutManager) { GridLayoutManager gridLayoutManager = (GridLayoutManager) manager; range = findRangeGrid(gridLayoutManager); orientation = gridLayoutManager.getOrientation(); } else if (manager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) manager; range = findRangeStaggeredGrid(staggeredGridLayoutManager); orientation = staggeredGridLayoutManager.getOrientation(); } if (range == null || range.length < 2) { return; } XLogUtil. D (" the original position of the item can be seen in the screen: "+ range [0] +" -- - "+ range [1]). // view for (int I = range[0]; i <= range[1]; i++) { View view = manager.findViewByPosition(i); setCallbackForLogicVisibleView(view, i, orientation); } } catch (Exception e) { e.printStackTrace(); XLogUtil.d(e.getMessage()); }}Copy the code
/** * Set visibility callback for logically visible views * Logically visible -- visible and 50% of height (width) > View height (width) * @param View View that is visible to the item * @param position Position that is visible to the item * @param orientation RecyclerView direction * / private void setCallbackForLogicVisibleView (View View, int the position, int orientation) { if (view == null || view.getVisibility() ! = View.VISIBLE || ! view.isShown() || ! view.getGlobalVisibleRect(new Rect())) { return; } Rect rect = new Rect(); boolean cover = view.getGlobalVisibleRect(rect); //item is logically visible: The visible and the height (width) > view line height (width) 50% Boolean visibleHeightEnough orientation of = = = OrientationHelper. VERTICAL && the rect. Height () > view.getMeasuredHeight() / 2; boolean visibleWidthEnough = orientation == OrientationHelper.HORIZONTAL && rect.width() > view.getMeasuredWidth() / 2; boolean isItemViewVisibleInLogic = visibleHeightEnough || visibleWidthEnough; if (cover && mIsRecyclerViewVisibleInLogic && isItemViewVisibleInLogic) { mItemOnExposeListener.onItemViewVisible(true, position); }else { mItemOnExposeListener.onItemViewVisible(false, position); }}Copy the code