How to achieve the exposure algorithm of the list item

 

First determine the number of displayed items

Count the number of items when consuming

    private void recordResume() {
        Point rangePosition = findRangePosition();
        if (rangePosition == null) {
            return;
        }
        int firstComPosition = rangePosition.x;
        int lastComPosition = rangePosition.y;
        if (mExposeCallback != null) {
            List<Integer> positionList = new ArrayList<>();
            for (int i = firstComPosition; i <= lastComPosition; i++) {
                templateAddData(i, positionList);
            }
            mExposeCallback.onExpose(positionList);
        } else {
            for (int i = firstComPosition; i <= lastComPosition; i++) {
                templateAddData(i, null);
            }
        }
    }

When sliding, count the number of items

    private void recordTouch() {
        Point rangePosition = findRangePosition();
        if (rangePosition == null) {
            return;
        }
        int firstComPosition = rangePosition.x;
        int lastComPosition = rangePosition.y;
        if (firstComPosition == mOldFirstComPt && lastComPosition == mOldLastComPt) {
            return;
        }
        List<Integer> positionList = new ArrayList<>();
        //首次&不包含相同项
        if (mOldLastComPt == -1 || firstComPosition > mOldLastComPt || lastComPosition < mOldFirstComPt) {
            for (int i = firstComPosition; i <= lastComPosition; i++) {
                templateAddData(i, positionList);
            }
        } else {
            //排除相同项
            if (firstComPosition < mOldFirstComPt) {
                for (int i = firstComPosition; i < mOldFirstComPt; i++) {
                    templateAddData(i, positionList);
                }
            }
            if (lastComPosition > mOldLastComPt) {
                for (int i = mOldLastComPt + 1; i <= lastComPosition; i++) {
                    templateAddData(i, positionList);
                }
            }
        }
        if (mExposeCallback != null) {
            mExposeCallback.onExpose(positionList);
        }
        mOldFirstComPt = firstComPosition;
        mOldLastComPt = lastComPosition;
    }

Calculate whether to add the current location to the exposure list

    /**
     * 计算是否将当前位置添加到曝光列表内
     *
     * @param position
     * @param positionList
     */
    private void templateAddData(int position, List<Integer> positionList) {
        int count = mReportData.get(position);
        mReportData.put(position, count + 1);
        if (null != positionList) {
            if (mReporterTime.get(makeMapKey(mViewName, position)) == null) {
                positionList.add(position);
                mReporterTime.put(makeMapKey(mViewName, position), System.currentTimeMillis());
            } else if (mReporterTime.get(makeMapKey(mViewName, position)) != null
                    && System.currentTimeMillis() - mReporterTime.get(makeMapKey(mViewName, position)) > mReportingInterval) {
                positionList.add(position);
                mReporterTime.put(makeMapKey(mViewName, position), System.currentTimeMillis());
            }
        }
    }

 

Guess you like

Origin blog.csdn.net/ly_xiamu/article/details/117655646