Java Code Examples for androidx.recyclerview.widget.OrientationHelper#HORIZONTAL

The following examples show how to use androidx.recyclerview.widget.OrientationHelper#HORIZONTAL . 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: CenteringRecyclerView.java    From centering-recycler-view with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates and returns the center offset size.
 *
 * @param orientation   The layout orientation.
 * @param childPosition The visible child position.
 * @return the center offset or the last known offset if a child at the position is null.
 */
private int getCenterOffset(int orientation, int childPosition) {
    View child = getChildAt(childPosition);
    if (child == null) {
        return mFallbackCenterOffset;
    }

    final Rect r = new Rect();
    if (getGlobalVisibleRect(r)) {
        if (orientation == OrientationHelper.HORIZONTAL) {
            mFallbackCenterOffset = r.width() / 2 - child.getWidth() / 2;
        } else {
            mFallbackCenterOffset = r.height() / 2 - child.getHeight() / 2;
        }
    } else {
        if (orientation == OrientationHelper.HORIZONTAL) {
            mFallbackCenterOffset = getWidth() / 2 - child.getWidth() / 2;
        } else {
            mFallbackCenterOffset = getHeight() / 2 - child.getHeight() / 2;
        }
    }

    return mFallbackCenterOffset;
}
 
Example 2
Source File: CenteringRecyclerView.java    From centering-recycler-view with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates and returns the bottom offset size.
 *
 * @param orientation   The layout orientation.
 * @param childPosition The visible child position.
 * @return the bottom offset or the last known offset if a child at the position is null.
 */
private int getBottomOffset(int orientation, int childPosition) {
    View child = getChildAt(childPosition);
    if (child == null) {
        return mFallbackBottomOffset;
    }

    final Rect r = new Rect();
    if (getGlobalVisibleRect(r)) {
        if (orientation == OrientationHelper.HORIZONTAL) {
            mFallbackBottomOffset = r.width() - child.getWidth();
        } else {
            mFallbackBottomOffset = r.height() - child.getHeight();
        }
    } else {
        if (orientation == OrientationHelper.HORIZONTAL) {
            mFallbackBottomOffset = getWidth() - child.getWidth();
        } else {
            mFallbackBottomOffset = getHeight() - child.getHeight();
        }
    }

    return mFallbackBottomOffset;
}
 
Example 3
Source File: ImageActivity.java    From cloudinary_android with MIT License 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(View.GONE);
    imageView = (ImageView) findViewById(R.id.image_view);
    descriptionTextView = (TextView) findViewById(R.id.effectDescription);
    recyclerView = (RecyclerView) findViewById(R.id.effectsGallery);
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, OrientationHelper.HORIZONTAL, false);
    recyclerView.setLayoutManager(layoutManager);

    initExoPlayer();

    fetchImageFromIntent(getIntent());
}
 
Example 4
Source File: RecyclerBinder.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * If measure is called with measure specs that cannot be used to measure the recyclerview, the
 * size of one of an item will be used to determine how to measure instead.
 *
 * @return a size value that can be used to measure the dimension of the recycler that has unknown
 *     size, which is width for vertical scrolling recyclers or height for horizontal scrolling
 *     recyclers.
 */
private int getSizeForMeasuring() {
  if (mSizeForMeasure == null) {
    return UNSET;
  }

  return mLayoutInfo.getScrollDirection() == OrientationHelper.HORIZONTAL
      ? mSizeForMeasure.height
      : mSizeForMeasure.width;
}
 
Example 5
Source File: LinearLayoutInfo.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsPredictiveItemAnimations() {
  // Predictive animation in RecyclerView has some bugs that surface occasionally when one
  // RecyclerView is used as a child of another RecyclerView and it is very hard to repro
  // (https://issuetracker.google.com/issues/37007605). This one disables that optional
  // feature for Horizontal one which is used as inner RecyclerView most of the cases.
  // Disabling predictive animations lets RecyclerView use the simple animations instead.
  return getOrientation() == OrientationHelper.HORIZONTAL
      ? false
      : super.supportsPredictiveItemAnimations();
}
 
Example 6
Source File: TopSnappedSmoothScroller.java    From FlexibleAdapter with Apache License 2.0 5 votes vote down vote up
/**
 * Controls the direction in which smoothScroll looks for your view
 *
 * @return the vector position
 */
@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
    final int firstChildPos = layoutManager.findFirstCompletelyVisibleItemPosition();
    final int direction = targetPosition < firstChildPos ? -1 : 1;

    if (layoutManager.getOrientation() == OrientationHelper.HORIZONTAL) {
        vectorPosition.set(direction, 0);
        return vectorPosition;
    } else {
        vectorPosition.set(0, direction);
        return vectorPosition;
    }
}
 
Example 7
Source File: FamiliarDefaultItemDecoration.java    From FamiliarRecyclerView with MIT License 5 votes vote down vote up
private void initLayoutManagerType() {
    mLayoutManagerType = mFamiliarRecyclerView.getCurLayoutManagerType();
    RecyclerView.LayoutManager layoutManager = mFamiliarRecyclerView.getLayoutManager();
    switch (mLayoutManagerType) {
        case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR:
            LinearLayoutManager curLinearLayoutManager = (LinearLayoutManager) layoutManager;
            if (curLinearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
                mOrientation = OrientationHelper.HORIZONTAL;
            } else {
                mOrientation = OrientationHelper.VERTICAL;
            }
            break;
        case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_GRID:
            GridLayoutManager curGridLayoutManager = (GridLayoutManager) layoutManager;
            mGridSpanCount = curGridLayoutManager.getSpanCount();
            if (curGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
                mOrientation = OrientationHelper.HORIZONTAL;
            } else {
                mOrientation = OrientationHelper.VERTICAL;
            }
            break;
        case FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_STAGGERED_GRID:
            StaggeredGridLayoutManager curStaggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            mGridSpanCount = curStaggeredGridLayoutManager.getSpanCount();
            if (curStaggeredGridLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
                mOrientation = OrientationHelper.HORIZONTAL;
            } else {
                mOrientation = OrientationHelper.VERTICAL;
            }
            break;
        default:
            mLayoutManagerType = FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_LINEAR;
    }

    initDivisible();
}
 
Example 8
Source File: RecyclerBinder.java    From litho with Apache License 2.0 4 votes vote down vote up
private Size getInitialMeasuredSize(
    int parentWidthSpec, int parentHeightSpec, boolean canRemeasure) {
  final Size out = new Size();
  final int scrollDirection = mLayoutInfo.getScrollDirection();

  final int measuredWidth;
  final int measuredHeight;

  final boolean shouldMeasureItemForSize =
      shouldMeasureItemForSize(parentWidthSpec, parentHeightSpec, scrollDirection, canRemeasure);

  switch (scrollDirection) {
    case OrientationHelper.VERTICAL:
      measuredHeight = SizeSpec.getSize(parentHeightSpec);

      if (!shouldMeasureItemForSize) {
        measuredWidth = SizeSpec.getSize(parentWidthSpec);
      } else if (mSizeForMeasure != null) {
        measuredWidth = mSizeForMeasure.width;
      } else {
        measuredWidth = 0;
      }
      break;

    case OrientationHelper.HORIZONTAL:
    default:
      measuredWidth = SizeSpec.getSize(parentWidthSpec);

      if (!shouldMeasureItemForSize) {
        measuredHeight = SizeSpec.getSize(parentHeightSpec);
      } else if (mSizeForMeasure != null) {
        measuredHeight = mSizeForMeasure.height;
      } else {
        measuredHeight = 0;
      }
      break;
  }

  out.width = measuredWidth;
  out.height = measuredHeight;

  return out;
}
 
Example 9
Source File: StickyHeaderHelper.java    From FlexibleAdapter with Apache License 2.0 4 votes vote down vote up
private void translateHeader() {
    // Sticky at zero offset (no translation)
    int headerOffsetX = 0, headerOffsetY = 0;
    // Get calculated elevation
    float elevation = mElevation;

    // Search for the position where the next header item is found and translate the new offset
    for (int i = 0; i < mRecyclerView.getChildCount(); i++) {
        final View nextChild = mRecyclerView.getChildAt(i);
        if (nextChild != null) {
            int adapterPos = mRecyclerView.getChildAdapterPosition(nextChild);
            int nextHeaderPosition = getStickyPosition(adapterPos);
            if (mHeaderPosition != nextHeaderPosition) {
                if (mAdapter.getFlexibleLayoutManager().getOrientation() == OrientationHelper.HORIZONTAL) {
                    if (nextChild.getLeft() > 0) {
                        int headerWidth = mStickyHolderLayout.getMeasuredWidth();
                        int nextHeaderOffsetX = nextChild.getLeft() - headerWidth -
                                mRecyclerView.getLayoutManager().getLeftDecorationWidth(nextChild) -
                                mRecyclerView.getLayoutManager().getRightDecorationWidth(nextChild);
                        headerOffsetX = Math.min(nextHeaderOffsetX, 0);
                        // Early remove the elevation/shadow to match with the next view
                        if (nextHeaderOffsetX < 5) {
                            elevation = 0f;
                        }
                        if (headerOffsetX < 0) {
                            break;
                        }
                    }
                } else {
                    if (nextChild.getTop() > 0) {
                        int headerHeight = mStickyHolderLayout.getMeasuredHeight();
                        int nextHeaderOffsetY = nextChild.getTop() - headerHeight -
                                mRecyclerView.getLayoutManager().getTopDecorationHeight(nextChild) -
                                mRecyclerView.getLayoutManager().getBottomDecorationHeight(nextChild);
                        headerOffsetY = Math.min(nextHeaderOffsetY, 0);
                        // Early remove the elevation/shadow to match with the next view
                        if (nextHeaderOffsetY < 5) {
                            elevation = 0f;
                        }
                        if (headerOffsetY < 0) {
                            break;
                        }
                    }
                }
            }
        }
    }
    // Apply the user elevation to the sticky container
    ViewCompat.setElevation(mStickyHolderLayout, elevation);
    // Apply translation (pushed up by another header)
    mStickyHolderLayout.setTranslationX(headerOffsetX);
    mStickyHolderLayout.setTranslationY(headerOffsetY);
    //Log.v("TranslationX=%s TranslationY=%s", headerOffsetX, headerOffsetY);
}
 
Example 10
Source File: FamiliarDefaultItemDecoration.java    From FamiliarRecyclerView with MIT License 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (mVerticalDividerDrawableHeight <= 0 && mHorizontalDividerDrawableHeight <= 0) return;

    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();
    int position = params.getViewAdapterPosition();
    int headersCount;
    int footerCount;
    int itemViewCount;
    FamiliarRecyclerView curFamiliarRecyclerView = null;
    if (parent instanceof FamiliarRecyclerView) {
        curFamiliarRecyclerView = (FamiliarRecyclerView) parent;

        footerCount = curFamiliarRecyclerView.getFooterViewsCount();
        headersCount = curFamiliarRecyclerView.getHeaderViewsCount();
        itemViewCount = curFamiliarRecyclerView.getAdapter().getItemCount() - headersCount - footerCount;
    } else {
        headersCount = 0;
        footerCount = 0;
        itemViewCount = parent.getAdapter().getItemCount();
    }

    // intercept filter
    if (isInterceptFilter(position, headersCount, footerCount, itemViewCount)) return;

    // set headView or footerView
    if (isHeadViewPos(headersCount, position)) {
        // head
        if (mOrientation == OrientationHelper.HORIZONTAL) {
            outRect.set(mVerticalDividerDrawableHeight, 0, 0, 0);
        } else {
            outRect.set(0, mHorizontalDividerDrawableHeight, 0, 0);
        }
        return;
    } else if (isFooterViewPos(headersCount, footerCount, itemViewCount, position)) {
        // footer
        if (mOrientation == OrientationHelper.HORIZONTAL) {
            outRect.set(mVerticalDividerDrawableHeight, 0, 0, 0);
        } else {
            outRect.set(0, mHorizontalDividerDrawableHeight, 0, 0);
        }
        return;
    } else if (isEmptyView(curFamiliarRecyclerView, position, headersCount)) {
        // emptyView
        if (isHeaderDividersEnabled && headersCount > 0) {
            if (mOrientation == OrientationHelper.HORIZONTAL) {
                outRect.set(mVerticalDividerDrawableHeight, 0, 0, 0);
            } else {
                outRect.set(0, mHorizontalDividerDrawableHeight, 0, 0);
            }
        }
        return;
    }

    // set itemView
    if (mLayoutManagerType == FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_GRID || mLayoutManagerType == FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_STAGGERED_GRID) {
        processGridOffsets(outRect, position, headersCount, view);
    } else {
        int topOrLeftSize;
        if ((!isHeaderDividersEnabled || headersCount == 0) && position - headersCount == 0) {
            topOrLeftSize = 0;
        } else {
            topOrLeftSize = mOrientation == OrientationHelper.VERTICAL ? mHorizontalDividerDrawableHeight : mVerticalDividerDrawableHeight;
        }

        if (mOrientation == OrientationHelper.HORIZONTAL) {
            outRect.set(topOrLeftSize, mItemViewBothSidesMargin, 0, mItemViewBothSidesMargin);
        } else {
            outRect.set(mItemViewBothSidesMargin, topOrLeftSize, mItemViewBothSidesMargin, 0);
        }
    }
}
 
Example 11
Source File: FamiliarDefaultItemDecoration.java    From FamiliarRecyclerView with MIT License 4 votes vote down vote up
private void processGridOffsets(Rect outRect, int position, int headersCount, View view) {
    int curGridNum;
    if (mLayoutManagerType == FamiliarRecyclerView.LAYOUT_MANAGER_TYPE_GRID) {
        curGridNum = (position - headersCount) % mGridSpanCount;
    } else {
        curGridNum = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
    }

    int leftOrTopOffset, rightOrBottomOffset;
    float mDividerLeftBaseOffset, mDividerRightBaseOffset;

    final int tempDividerDrawableSize = mOrientation == OrientationHelper.HORIZONTAL ? mHorizontalDividerDrawableHeight : mVerticalDividerDrawableHeight;
    if (mItemViewBothSidesMargin > 0) {
        // item view left and right margin
        mDividerLeftBaseOffset = (float) tempDividerDrawableSize / mGridSpanCount * curGridNum - mItemViewBothSidesMargin * 2 / mGridSpanCount * curGridNum + mItemViewBothSidesMargin;
        mDividerRightBaseOffset = (float) tempDividerDrawableSize / mGridSpanCount * (mGridSpanCount - (curGridNum + 1)) + mItemViewBothSidesMargin * 2 / mGridSpanCount * (curGridNum + 1) - mItemViewBothSidesMargin;
    } else {
        mDividerLeftBaseOffset = (float) tempDividerDrawableSize / mGridSpanCount * curGridNum;
        mDividerRightBaseOffset = (float) tempDividerDrawableSize / mGridSpanCount * (mGridSpanCount - (curGridNum + 1));
    }

    if (!isDivisible && mUnDivisibleValue > 0) {
        leftOrTopOffset = Math.round(mDividerLeftBaseOffset - mUnDivisibleValue * (curGridNum + 1));
        rightOrBottomOffset = Math.round(mDividerRightBaseOffset + mUnDivisibleValue * (curGridNum + 1));
    } else {
        leftOrTopOffset = (int) mDividerLeftBaseOffset;
        rightOrBottomOffset = (int) mDividerRightBaseOffset;
    }

    int topOrLeftSize;
    if ((!isHeaderDividersEnabled || headersCount == 0) && isGridItemLayoutFirstRow(position, headersCount)) {
        topOrLeftSize = 0;
    } else {
        topOrLeftSize = mOrientation == OrientationHelper.VERTICAL ? mHorizontalDividerDrawableHeight : mVerticalDividerDrawableHeight;
    }

    if (isGridItemLayoutLastColumn(position, headersCount, view)) {
        // last Column
        if (mOrientation == OrientationHelper.HORIZONTAL) {
            outRect.set(topOrLeftSize, leftOrTopOffset, 0, mItemViewBothSidesMargin);
        } else {
            outRect.set(leftOrTopOffset, topOrLeftSize, mItemViewBothSidesMargin, 0);
        }
    } else if (isGridItemLayoutFirstColumn(position, headersCount, view)) {
        // first column
        if (mOrientation == OrientationHelper.HORIZONTAL) {
            outRect.set(topOrLeftSize, mItemViewBothSidesMargin, 0, rightOrBottomOffset);
        } else {
            outRect.set(mItemViewBothSidesMargin, topOrLeftSize, rightOrBottomOffset, 0);
        }
    } else {
        // middle column
        if (mOrientation == OrientationHelper.HORIZONTAL) {
            outRect.set(topOrLeftSize, leftOrTopOffset, 0, rightOrBottomOffset);
        } else {
            outRect.set(leftOrTopOffset, topOrLeftSize, rightOrBottomOffset, 0);
        }
    }
}
 
Example 12
Source File: FamiliarRecyclerView.java    From FamiliarRecyclerView with MIT License 4 votes vote down vote up
private void processDefDivider(boolean isLinearLayoutManager, int layoutManagerOrientation) {
    if (!isDefaultItemDecoration) return;

    if ((null == mVerticalDivider || null == mHorizontalDivider) && null != mDefAllDivider) {
        if (isLinearLayoutManager) {
            if (layoutManagerOrientation == OrientationHelper.VERTICAL && null == mHorizontalDivider) {
                mHorizontalDivider = mDefAllDivider;
            } else if (layoutManagerOrientation == OrientationHelper.HORIZONTAL && null == mVerticalDivider) {
                mVerticalDivider = mDefAllDivider;
            }
        } else {
            if (null == mVerticalDivider) {
                mVerticalDivider = mDefAllDivider;
            }

            if (null == mHorizontalDivider) {
                mHorizontalDivider = mDefAllDivider;
            }
        }
    }

    if (mVerticalDividerHeight > 0 && mHorizontalDividerHeight > 0) return;

    if (mDefAllDividerHeight > 0) {
        if (isLinearLayoutManager) {
            if (layoutManagerOrientation == OrientationHelper.VERTICAL && mHorizontalDividerHeight <= 0) {
                mHorizontalDividerHeight = mDefAllDividerHeight;
            } else if (layoutManagerOrientation == OrientationHelper.HORIZONTAL && mVerticalDividerHeight <= 0) {
                mVerticalDividerHeight = mDefAllDividerHeight;
            }
        } else {
            if (mVerticalDividerHeight <= 0) {
                mVerticalDividerHeight = mDefAllDividerHeight;
            }

            if (mHorizontalDividerHeight <= 0) {
                mHorizontalDividerHeight = mDefAllDividerHeight;
            }
        }
    } else {
        if (isLinearLayoutManager) {
            if (layoutManagerOrientation == OrientationHelper.VERTICAL && mHorizontalDividerHeight <= 0) {
                if (null != mHorizontalDivider) {
                    if (mHorizontalDivider.getIntrinsicHeight() > 0) {
                        mHorizontalDividerHeight = mHorizontalDivider.getIntrinsicHeight();
                    } else {
                        mHorizontalDividerHeight = DEF_DIVIDER_HEIGHT;
                    }
                }
            } else if (layoutManagerOrientation == OrientationHelper.HORIZONTAL && mVerticalDividerHeight <= 0) {
                if (null != mVerticalDivider) {
                    if (mVerticalDivider.getIntrinsicHeight() > 0) {
                        mVerticalDividerHeight = mVerticalDivider.getIntrinsicHeight();
                    } else {
                        mVerticalDividerHeight = DEF_DIVIDER_HEIGHT;
                    }
                }
            }
        } else {
            if (mVerticalDividerHeight <= 0 && null != mVerticalDivider) {
                if (mVerticalDivider.getIntrinsicHeight() > 0) {
                    mVerticalDividerHeight = mVerticalDivider.getIntrinsicHeight();
                } else {
                    mVerticalDividerHeight = DEF_DIVIDER_HEIGHT;
                }
            }

            if (mHorizontalDividerHeight <= 0 && null != mHorizontalDivider) {
                if (mHorizontalDivider.getIntrinsicHeight() > 0) {
                    mHorizontalDividerHeight = mHorizontalDivider.getIntrinsicHeight();
                } else {
                    mHorizontalDividerHeight = DEF_DIVIDER_HEIGHT;
                }
            }
        }
    }
}