Java Code Examples for android.support.v7.widget.OrientationHelper#getDecoratedMeasurement()

The following examples show how to use android.support.v7.widget.OrientationHelper#getDecoratedMeasurement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StartSnapHelper.java    From SimpleRecyclerView with Apache License 2.0 6 votes vote down vote up
private View getStartView(RecyclerView.LayoutManager layoutManager,
                          OrientationHelper helper) {
  if (!(layoutManager instanceof LinearLayoutManager)) {
    return super.findSnapView(layoutManager);
  }

  LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;

  int firstChild = linearLayoutManager.findFirstVisibleItemPosition();

  boolean isLastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1;

  if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
    return null;
  }

  View child = layoutManager.findViewByPosition(firstChild);

  if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
    && helper.getDecoratedEnd(child) > 0) {
    return child;
  } else {
    return layoutManager.findViewByPosition(firstChild + 1);
  }
}
 
Example 2
Source File: ScrollLinearHelper.java    From YCBanner with Apache License 2.0 6 votes vote down vote up
private View findStartView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) {
    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        //需要判断是否是最后一个Item,如果是最后一个则不让对齐,以免出现最后一个显示不完全。
        boolean isLastItem = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;
        if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
            return null;
        }
        View child = layoutManager.findViewByPosition(firstChild);
        if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
                && helper.getDecoratedEnd(child) > 0) {
            return child;
        } else {
            if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1) {
                return null;
            } else {
                return layoutManager.findViewByPosition(firstChild + 1);
            }
        }
    }
    return super.findSnapView(layoutManager);
}
 
Example 3
Source File: ScrollSnapHelper.java    From YCBanner with Apache License 2.0 6 votes vote down vote up
private View findStartView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) {
    if (layoutManager instanceof LinearLayoutManager) {
        int firstChildPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        if (firstChildPosition == RecyclerView.NO_POSITION) {
            return null;
        }
        if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1) {
            return null;
        }
        View firstChildView = layoutManager.findViewByPosition(firstChildPosition);
        if (helper.getDecoratedEnd(firstChildView) >= helper.getDecoratedMeasurement(firstChildView) / 2 && helper.getDecoratedEnd(firstChildView) > 0) {
            return firstChildView;
        } else {
            return layoutManager.findViewByPosition(firstChildPosition + 1);
        }
    } else {
        return null;
    }
}
 
Example 4
Source File: GravitySnapHelper.java    From date_picker_converter with Apache License 2.0 5 votes vote down vote up
private View findEndView(RecyclerView.LayoutManager layoutManager,
                         OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();

        if (lastChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(lastChild);

        float visibleWidth;

        if (isRtlHorizontal) {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        }

        // If we're at the start of the list, we shouldn't snap
        // to avoid having the first item not completely visible.
        boolean startOfList = ((LinearLayoutManager) layoutManager)
                .findFirstCompletelyVisibleItemPosition() == 0;

        if (visibleWidth > 0.5f && !startOfList) {
            return child;
        } else if (startOfList) {
            return null;
        } else {
            // If the child wasn't returned, we need to return the previous view
            return layoutManager.findViewByPosition(lastChild - 1);
        }
    }
    return null;
}
 
Example 5
Source File: StartSnapHelper.java    From Capstone-Project with MIT License 5 votes vote down vote up
private View getStartView(RecyclerView.LayoutManager layoutManager,
                          OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();

        boolean isLastItem = ((LinearLayoutManager) layoutManager)
                .findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;

        if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
                && helper.getDecoratedEnd(child) > 0) {
            return child;
        } else {
            if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1) {
                return null;
            } else {
                return layoutManager.findViewByPosition(firstChild + 1);
            }
        }
    }

    return super.findSnapView(layoutManager);
}
 
Example 6
Source File: GravitySnapHelper.java    From MangoBloggerAndroidApp with Mozilla Public License 2.0 5 votes vote down vote up
private View findEndView(RecyclerView.LayoutManager layoutManager,
                         OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();

        if (lastChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(lastChild);

        float visibleWidth;

        if (mIsRtlHorizontal) {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        }

        // If we're at the start of the list, we shouldn't snap
        // to avoid having the first item not completely visible.
        boolean startOfList = ((LinearLayoutManager) layoutManager)
                .findFirstCompletelyVisibleItemPosition() == 0;

        if (visibleWidth > 0.5f && !startOfList) {
            return child;
        } else if (mSnapLastItemEnabled && startOfList) {
            return child;
        } else if (startOfList) {
            return null;
        } else {
            // If the child wasn't returned, we need to return the previous view
            return layoutManager.findViewByPosition(lastChild - 1);
        }
    }
    return null;
}
 
Example 7
Source File: PagerSnapHelper.java    From Dagger2-Sample with MIT License 5 votes vote down vote up
private View getStartView(RecyclerView.LayoutManager layoutManager,
                          OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();

        boolean isLastItem = ((LinearLayoutManager) layoutManager)
                .findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;

        if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
                && helper.getDecoratedEnd(child) > 0) {
            return child;
        } else {
            if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1) {
                return null;
            } else {
                return layoutManager.findViewByPosition(firstChild + 1);
            }
        }
    }

    return super.findSnapView(layoutManager);
}
 
Example 8
Source File: ScrollPageHelper.java    From YCBanner with Apache License 2.0 4 votes vote down vote up
@Nullable
private View findEndView(RecyclerView.LayoutManager layoutManager,
                         @NonNull OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        boolean reverseLayout = linearLayoutManager.getReverseLayout();
        int lastChild = reverseLayout ? linearLayoutManager.findFirstVisibleItemPosition()
                : linearLayoutManager.findLastVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (lastChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(lastChild);

        float visibleWidth;

        if (isRtlHorizontal) {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        }

        boolean startOfList;
        if (!reverseLayout) {
            startOfList = ((LinearLayoutManager) layoutManager)
                    .findFirstCompletelyVisibleItemPosition() == 0;
        } else {
            startOfList = ((LinearLayoutManager) layoutManager)
                    .findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1;
        }

        if (visibleWidth > 0.5f && !startOfList) {
            return child;
        } else if (snapLastItem && startOfList) {
            return child;
        } else if (startOfList) {
            return null;
        } else {
            return reverseLayout ? layoutManager.findViewByPosition(lastChild + offset)
                    : layoutManager.findViewByPosition(lastChild - offset);
        }
    }
    return null;
}
 
Example 9
Source File: ScrollPageHelper.java    From YCBanner with Apache License 2.0 4 votes vote down vote up
@Nullable
private View findStartView(RecyclerView.LayoutManager layoutManager,
                           @NonNull OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        boolean reverseLayout = linearLayoutManager.getReverseLayout();
        int firstChild = reverseLayout ? linearLayoutManager.findLastVisibleItemPosition()
                : linearLayoutManager.findFirstVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (firstChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        float visibleWidth;
        if (isRtlHorizontal) {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        }
        boolean endOfList;
        if (!reverseLayout) {
            endOfList = ((LinearLayoutManager) layoutManager)
                    .findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1;
        } else {
            endOfList = ((LinearLayoutManager) layoutManager)
                    .findFirstCompletelyVisibleItemPosition()
                    == 0;
        }

        if (visibleWidth > 0.5f && !endOfList) {
            return child;
        } else if (snapLastItem && endOfList) {
            return child;
        } else if (endOfList) {
            return null;
        } else {
            return reverseLayout ? layoutManager.findViewByPosition(firstChild - offset)
                    : layoutManager.findViewByPosition(firstChild + offset);
        }
    }

    return null;
}
 
Example 10
Source File: GravityDelegate.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
private View findEndView(RecyclerView.LayoutManager layoutManager,
                         OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int lastChild = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (lastChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(lastChild);

        float visibleWidth;

        if (isRtlHorizontal) {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        }

        // If we're at the start of the list, we shouldn't snap
        // to avoid having the first item not completely visible.
        boolean startOfList = ((LinearLayoutManager) layoutManager)
                .findFirstCompletelyVisibleItemPosition() == 0;

        if (visibleWidth > 0.5f && !startOfList) {
            return child;
        } else if (snapLastItem && startOfList) {
            return child;
        } else if (startOfList) {
            return null;
        } else {
            // If the child wasn't returned, we need to return the previous view
            return layoutManager.findViewByPosition(lastChild - offset);
        }
    }
    return null;
}
 
Example 11
Source File: GravityDelegate.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns the first view that we should snap to.
 *
 * @param layoutManager the recyclerview's layout manager
 * @param helper        orientation helper to calculate view sizes
 * @return the first view in the LayoutManager to snap to
 */
private View findStartView(RecyclerView.LayoutManager layoutManager,
                           OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (firstChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        float visibleWidth;

        // We should return the child if it's visible width
        // is greater than 0.5 of it's total width.
        // In a RTL configuration, we need to check the start point and in LTR the end point
        if (isRtlHorizontal) {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        }

        // If we're at the end of the list, we shouldn't snap
        // to avoid having the last item not completely visible.
        boolean endOfList = ((LinearLayoutManager) layoutManager)
                .findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;

        if (visibleWidth > 0.5f && !endOfList) {
            return child;
        } else if (snapLastItem && endOfList) {
            return child;
        } else if (endOfList) {
            return null;
        } else {
            // If the child wasn't returned, we need to return
            // the next view close to the start.
            return layoutManager.findViewByPosition(firstChild + offset);
        }
    }

    return null;
}
 
Example 12
Source File: GravitySnapHelper.java    From MangoBloggerAndroidApp with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Returns the first view that we should snap to.
 *
 * @param layoutManager the recyclerview's layout manager
 * @param helper        orientation helper to calculate view sizes
 * @return the first view in the LayoutManager to snap to
 */
private View findStartView(RecyclerView.LayoutManager layoutManager,
                           OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();

        if (firstChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        float visibleWidth;

        // We should return the child if it's visible width
        // is greater than 0.5 of it's total width.
        // In a RTL configuration, we need to check the start point and in LTR the end point
        if (mIsRtlHorizontal) {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        }

        // If we're at the end of the list, we shouldn't snap
        // to avoid having the last item not completely visible.
        boolean endOfList = ((LinearLayoutManager) layoutManager)
                .findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;

        if (visibleWidth > 0.5f && !endOfList) {
            return child;
        } else if (mSnapLastItemEnabled && endOfList) {
            return child;
        } else if (endOfList) {
            return null;
        } else {
            // If the child wasn't returned, we need to return
            // the next view close to the start.
            return layoutManager.findViewByPosition(firstChild + 1);
        }
    }

    return null;
}
 
Example 13
Source File: ScrollPageHelper.java    From YCRefreshView with Apache License 2.0 4 votes vote down vote up
@Nullable
private View findStartView(RecyclerView.LayoutManager layoutManager,
                           @NonNull OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        boolean reverseLayout = linearLayoutManager.getReverseLayout();
        int firstChild = reverseLayout ? linearLayoutManager.findLastVisibleItemPosition()
                : linearLayoutManager.findFirstVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (firstChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        float visibleWidth;
        if (isRtlHorizontal) {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        }
        boolean endOfList;
        if (!reverseLayout) {
            endOfList = ((LinearLayoutManager) layoutManager)
                    .findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1;
        } else {
            endOfList = ((LinearLayoutManager) layoutManager)
                    .findFirstCompletelyVisibleItemPosition()
                    == 0;
        }

        if (visibleWidth > 0.5f && !endOfList) {
            return child;
        } else if (snapLastItem && endOfList) {
            return child;
        } else if (endOfList) {
            return null;
        } else {
            return reverseLayout ? layoutManager.findViewByPosition(firstChild - offset)
                    : layoutManager.findViewByPosition(firstChild + offset);
        }
    }

    return null;
}
 
Example 14
Source File: ScrollPageHelper.java    From YCRefreshView with Apache License 2.0 4 votes vote down vote up
@Nullable
private View findEndView(RecyclerView.LayoutManager layoutManager,
                         @NonNull OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        boolean reverseLayout = linearLayoutManager.getReverseLayout();
        int lastChild = reverseLayout ? linearLayoutManager.findFirstVisibleItemPosition()
                : linearLayoutManager.findLastVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (lastChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(lastChild);

        float visibleWidth;

        if (isRtlHorizontal) {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        }

        boolean startOfList;
        if (!reverseLayout) {
            startOfList = ((LinearLayoutManager) layoutManager)
                    .findFirstCompletelyVisibleItemPosition() == 0;
        } else {
            startOfList = ((LinearLayoutManager) layoutManager)
                    .findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1;
        }

        if (visibleWidth > 0.5f && !startOfList) {
            return child;
        } else if (snapLastItem && startOfList) {
            return child;
        } else if (startOfList) {
            return null;
        } else {
            return reverseLayout ? layoutManager.findViewByPosition(lastChild + offset)
                    : layoutManager.findViewByPosition(lastChild - offset);
        }
    }
    return null;
}
 
Example 15
Source File: ScrollPageHelper.java    From YCScrollPager with Apache License 2.0 4 votes vote down vote up
@Nullable
private View findEndView(RecyclerView.LayoutManager layoutManager,
                         @NonNull OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        boolean reverseLayout = linearLayoutManager.getReverseLayout();
        int lastChild = reverseLayout ? linearLayoutManager.findFirstVisibleItemPosition()
                : linearLayoutManager.findLastVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (lastChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(lastChild);

        float visibleWidth;

        if (isRtlHorizontal) {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        }

        boolean startOfList;
        if (!reverseLayout) {
            startOfList = ((LinearLayoutManager) layoutManager)
                    .findFirstCompletelyVisibleItemPosition() == 0;
        } else {
            startOfList = ((LinearLayoutManager) layoutManager)
                    .findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1;
        }

        if (visibleWidth > 0.5f && !startOfList) {
            return child;
        } else if (snapLastItem && startOfList) {
            return child;
        } else if (startOfList) {
            return null;
        } else {
            return reverseLayout ? layoutManager.findViewByPosition(lastChild + offset)
                    : layoutManager.findViewByPosition(lastChild - offset);
        }
    }
    return null;
}
 
Example 16
Source File: GravitySnapHelper.java    From date_picker_converter with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the first view that we should snap to.
 *
 * @param layoutManager the recyclerview's layout manager
 * @param helper        orientation helper to calculate view sizes
 * @return the first view in the LayoutManager to snap to
 */
private View findStartView(RecyclerView.LayoutManager layoutManager,
                           OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();

        if (firstChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        float visibleWidth;

        // We should return the child if it's visible width
        // is greater than 0.5 of it's total width.
        // In a RTL configuration, we need to check the start point and in LTR the end point
        if (isRtlHorizontal) {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        }

        // If we're at the end of the list, we shouldn't snap
        // to avoid having the last item not completely visible.
        boolean endOfList = ((LinearLayoutManager) layoutManager)
                .findLastCompletelyVisibleItemPosition()
                == layoutManager.getItemCount() - 1;

        if (visibleWidth > 0.5f && !endOfList) {
            return child;
        } else if (endOfList) {
            return null;
        } else {
            // If the child wasn't returned, we need to return
            // the next view close to the start.
            return layoutManager.findViewByPosition(firstChild + 1);
        }
    }

    return null;
}
 
Example 17
Source File: ScrollPageHelper.java    From YCScrollPager with Apache License 2.0 4 votes vote down vote up
@Nullable
private View findStartView(RecyclerView.LayoutManager layoutManager,
                           @NonNull OrientationHelper helper) {

    if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        boolean reverseLayout = linearLayoutManager.getReverseLayout();
        int firstChild = reverseLayout ? linearLayoutManager.findLastVisibleItemPosition()
                : linearLayoutManager.findFirstVisibleItemPosition();
        int offset = 1;

        if (layoutManager instanceof GridLayoutManager) {
            offset += ((GridLayoutManager) layoutManager).getSpanCount() - 1;
        }

        if (firstChild == RecyclerView.NO_POSITION) {
            return null;
        }

        View child = layoutManager.findViewByPosition(firstChild);

        float visibleWidth;
        if (isRtlHorizontal) {
            visibleWidth = (float) (helper.getTotalSpace() - helper.getDecoratedStart(child))
                    / helper.getDecoratedMeasurement(child);
        } else {
            visibleWidth = (float) helper.getDecoratedEnd(child)
                    / helper.getDecoratedMeasurement(child);
        }
        boolean endOfList;
        if (!reverseLayout) {
            endOfList = ((LinearLayoutManager) layoutManager)
                    .findLastCompletelyVisibleItemPosition()
                    == layoutManager.getItemCount() - 1;
        } else {
            endOfList = ((LinearLayoutManager) layoutManager)
                    .findFirstCompletelyVisibleItemPosition()
                    == 0;
        }

        if (visibleWidth > 0.5f && !endOfList) {
            return child;
        } else if (snapLastItem && endOfList) {
            return child;
        } else if (endOfList) {
            return null;
        } else {
            return reverseLayout ? layoutManager.findViewByPosition(firstChild - offset)
                    : layoutManager.findViewByPosition(firstChild + offset);
        }
    }

    return null;
}