Java Code Examples for android.support.v7.widget.RecyclerView#NO_POSITION

The following examples show how to use android.support.v7.widget.RecyclerView#NO_POSITION . 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: DividerItemDecoration.java    From AndroidSchool with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    if (mDivider == null) {
        return;
    }

    int position = parent.getChildAdapterPosition(view);
    if (position == RecyclerView.NO_POSITION || (position == 0)) {
        return;
    }

    if (mOrientation == -1) {
        getOrientation(parent);
    }

    if (mOrientation == LinearLayoutManager.VERTICAL) {
        outRect.top = mDivider.getIntrinsicHeight();
    } else {
        outRect.left = mDivider.getIntrinsicWidth();
    }
}
 
Example 2
Source File: PagerSnapHelper.java    From Dagger2-Sample with MIT License 5 votes vote down vote up
@Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {

    View centerView = findSnapView(layoutManager);
    if (centerView == null)
        return RecyclerView.NO_POSITION;

    int position = layoutManager.getPosition(centerView);
    int targetPosition = -1;
    if (layoutManager.canScrollHorizontally()) {
        if (velocityX < 0) {
            targetPosition = position - 1;
        } else {
            targetPosition = position + 1;
        }
    }

    if (layoutManager.canScrollVertically()) {
        if (velocityY < 0) {
            targetPosition = position - 1;
        } else {
            targetPosition = position + 1;
        }
    }

    final int firstItem = 0;
    final int lastItem = layoutManager.getItemCount() - 1;
    targetPosition = Math.min(lastItem, Math.max(targetPosition, firstItem));
    if(targetPosition >= 0) recyclerSnapItemListener.onItemSnap(targetPosition);
    return targetPosition;
}
 
Example 3
Source File: MainActivity.java    From android-ui-toolkit-demos with Apache License 2.0 5 votes vote down vote up
private void changeItem(View view) {
    int position = mRecyclerView.getChildAdapterPosition(view);
    if (position != RecyclerView.NO_POSITION) {
        int color = generateColor();
        mColors.set(position, color);
        notifyItemChanged(position);
    }
}
 
Example 4
Source File: TodoAdapter.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void onClick(final View view) {
  if (getAdapterPosition() == RecyclerView.NO_POSITION) {
    return;
  }
  final TodoItem item = todoItems.get(getAdapterPosition());
  itemUpdater.updateChecked(item.getId(), taskCheckbox.isChecked());
}
 
Example 5
Source File: CommentViewHolder.java    From Hews with MIT License 5 votes vote down vote up
@Override
public boolean onLongClick(View v) {
    if (mListener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {
        mListener.onLongClick(getAdapterPosition());
    }
    return true;
}
 
Example 6
Source File: NewTabPageRecyclerView.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the above the fold view.
 * @return The view for above the fold or null, if it is not present.
 */
public NewTabPageLayout findAboveTheFoldView() {
    int position = getNewTabPageAdapter().getAboveTheFoldPosition();
    if (position == RecyclerView.NO_POSITION) return null;

    ViewHolder viewHolder = findViewHolderForAdapterPosition(position);
    if (viewHolder == null) return null;

    View view = viewHolder.itemView;
    if (!(view instanceof NewTabPageLayout)) return null;

    return (NewTabPageLayout) view;
}
 
Example 7
Source File: DesignerNewsStory.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
private void collapseExpandedComment() {
    if (!isCommentReplyExpanded()) return;
    notifyItemChanged(expandedCommentPosition, CommentAnimator.COLLAPSE_COMMENT);
    notifyItemRemoved(expandedCommentPosition + 1);
    replyToCommentFocused = false;
    expandedCommentPosition = RecyclerView.NO_POSITION;
    updateFabVisibility();
}
 
Example 8
Source File: NewTabPageRecyclerView.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the view holder for the first header.
 * @return The {@code ViewHolder} of the header, or null if it is not present.
 */
private SectionHeaderViewHolder findFirstHeader() {
    int position = getNewTabPageAdapter().getFirstHeaderPosition();
    if (position == RecyclerView.NO_POSITION) return null;

    ViewHolder viewHolder = findViewHolderForAdapterPosition(position);
    if (!(viewHolder instanceof SectionHeaderViewHolder)) return null;

    return (SectionHeaderViewHolder) viewHolder;
}
 
Example 9
Source File: RecyclerViewTV.java    From Android-tv-widget with Apache License 2.0 5 votes vote down vote up
/**
 * 滑动到底部.
 */
public int findLastCompletelyVisibleItemPosition() {
    LayoutManager layoutManager = getLayoutManager();
    if (layoutManager != null) {
        if (layoutManager instanceof LinearLayoutManager) {
            return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
        }
        if (layoutManager instanceof GridLayoutManager) {
            return ((GridLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
        }
    }
    return RecyclerView.NO_POSITION;
}
 
Example 10
Source File: StickyNormalItemLine.java    From YCRefreshView with Apache License 2.0 5 votes vote down vote up
private int getHeaderTop(RecyclerView parent, View child, View header,
                         int adapterPos, int layoutPos) {
    int headerHeight = getHeaderHeightForLayout(header);
    int top = ((int) child.getY()) - headerHeight;
    if (layoutPos == 0) {
        final int count = parent.getChildCount();
        final long currentId = mAdapter.getHeaderId(adapterPos);
        // find next view with header and compute the offscreen push if needed
        for (int i = 1; i < count; i++) {
            int adapterPosHere = parent.getChildAdapterPosition(parent.getChildAt(i));
            if (adapterPosHere != RecyclerView.NO_POSITION) {
                long nextId = mAdapter.getHeaderId(adapterPosHere);
                if (nextId != currentId) {
                    final View next = parent.getChildAt(i);
                    final int offset = ((int) next.getY()) - (headerHeight +
                            getHeader(parent, adapterPosHere).itemView.getHeight());
                    if (offset < 0) {
                        return offset;
                    } else {
                        break;
                    }
                }
            }
        }
        top = Math.max(0, top);
    }
    return top;
}
 
Example 11
Source File: CardAdapter.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
/**
 * onViewRecycled
 * @param holder
 */
@Override
public void onViewRecycled(CardViewHolder holder)
{
    detachClickListeners(holder);
    if (holder.position >= 0 && (holder.position < TODAY_POSITION - 1 || holder.position > TODAY_POSITION + 2)) {
        data.remove(holder.position);
    }
    holder.position = RecyclerView.NO_POSITION;
}
 
Example 12
Source File: StickyRecyclerHeadersDecoration.java    From sticky-headers-recyclerview with Apache License 2.0 5 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
  super.getItemOffsets(outRect, view, parent, state);
  int itemPosition = parent.getChildAdapterPosition(view);
  if (itemPosition == RecyclerView.NO_POSITION) {
      return;
  }
  if (mHeaderPositionCalculator.hasNewHeader(itemPosition, mOrientationProvider.isReverseLayout(parent))) {
    View header = getHeaderView(parent, itemPosition);
    setItemOffsetsForHeader(outRect, header, mOrientationProvider.getOrientation(parent));
  }
}
 
Example 13
Source File: RecyclerViewTV.java    From AndroidTVWidget with Apache License 2.0 5 votes vote down vote up
/**
 * 滑动到底部.
 */
public int findLastCompletelyVisibleItemPosition() {
    LayoutManager layoutManager = getLayoutManager();
    if (layoutManager != null) {
        if (layoutManager instanceof LinearLayoutManager) {
            return ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
        }
        if (layoutManager instanceof GridLayoutManager) {
            return ((GridLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
        }
    }
    return RecyclerView.NO_POSITION;
}
 
Example 14
Source File: ExpandableRecyclerView.java    From Expandable-RecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public final void onBindViewHolder(VH holder, int groupPosition) {
	if (DEBUG) Log.d(LOG_TAG,  this+" onBindViewHolder(pos="+groupPosition+") expanded="+expandedPosition+" count="+expandedChildCount);

	holder.setExpandHandler(this);

	if (expandedPosition == RecyclerView.NO_POSITION || groupPosition <= expandedPosition) {
		holder.isSelected = groupPosition == recyclerView.selectedGroup;
		onBindGroupView(holder, groupPosition);
		setExpandedViewHolder(holder, groupPosition == expandedPosition, true);
	} else if (groupPosition <= expandedPosition + expandedChildCount) {
		holder.isSelected = expandedPosition == recyclerView.selectedGroup;
		if (!BuildConfig.DEBUG) {
			try {
				onBindChildView(holder, expandedPosition, groupPosition - expandedPosition - 1);
			} catch (ClassCastException e) {
				Log.e(LOG_TAG, this + " failed onBindViewHolder(pos=" + groupPosition + ") expanded=" + expandedPosition + " count=" + expandedChildCount, e);
			}
		} else {
			onBindChildView(holder, expandedPosition, groupPosition - expandedPosition - 1);
		}
	} else {
		holder.isSelected = groupPosition == recyclerView.selectedGroup;
		onBindGroupView(holder, groupPosition - expandedChildCount);
		setExpandedViewHolder(holder, false, true);
	}
}
 
Example 15
Source File: BookmarksFragment.java    From JumpGo with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public boolean onLongClick(View v) {
    int index = getAdapterPosition();
    return index != RecyclerView.NO_POSITION && onItemLongClickListener != null &&
        onItemLongClickListener.onItemLongClick(adapter.itemAt(index));
}
 
Example 16
Source File: MeasurableOnScrollListener.java    From tenor-android-core with Apache License 2.0 4 votes vote down vote up
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    initScriptDirection(recyclerView);

    switch (newState) {
        case RecyclerView.SCROLL_STATE_DRAGGING:
            // distinct drag to improve accuracy, init mDraggingStart and mDraggingEnd here
            mDragging = true;
            final int[] range = AbstractLayoutManagerUtils.getVisibleRange(recyclerView);
            mDraggingStart = range[0];
            mDraggingEnd = range[1];
            break;
        case RecyclerView.SCROLL_STATE_SETTLING:
            mDragging = false;
            break;
        case RecyclerView.SCROLL_STATE_IDLE:
            mDragging = false;

            // perform final update on the range we have ever visited
            updateVisibleRange(recyclerView);
            AbstractLogUtils.e(this, "==>  visible range: [" + mDraggingStart + ", " + mDraggingEnd + "]");
            MeasurableRecyclerViewHelper.notifyMeasurableViewHolderDataRangeChanged(recyclerView, mDraggingStart, mDraggingEnd);

            // reset mDraggingStart and mDraggingEnd here
            mDraggingStart = RecyclerView.NO_POSITION;
            mDraggingEnd = RecyclerView.NO_POSITION;

            final int state = ScriptDirectionChecker.checkSelfScriptDirection(recyclerView.getContext());
            if (mScriptDirectionState != ScriptDirectionChecker.UNSPECIFIED
                    && state != ScriptDirectionChecker.UNSPECIFIED
                    && mScriptDirectionState != state) {
                /*
                 * [ANDROID-1778]
                 *
                 * immediately flush the statistics if user change between a ltr and rtl language
                 * on the fly; do the check in here for better performance
                 */
                final List<IMeasurableViewHolder> holders =
                        MeasurableRecyclerViewHelper.getViewHolders(recyclerView, IMeasurableViewHolder.class);
                for (IMeasurableViewHolder holder : holders) {
                    holder.flush();
                }

                if (AbstractLayoutManagerUtils.getOrientation(recyclerView.getLayoutManager())
                        == OrientationHelper.HORIZONTAL) {
                    AbstractLayoutManagerUtils.setReverseLayout(recyclerView.getLayoutManager(),
                            state == ScriptDirectionChecker.RIGHT_TO_LEFT);
                }
                mScriptDirectionState = state;
            }
            break;
        default:
            break;
    }
}
 
Example 17
Source File: Square.java    From ChipsLayoutManager with Apache License 2.0 4 votes vote down vote up
/**
 * find highest & lowest views among visible attached views
 */
@Override
public void findBorderViews() {
    topView = null;
    bottomView = null;
    leftView = null;
    rightView = null;
    minPositionOnScreen = RecyclerView.NO_POSITION;
    maxPositionOnScreen = RecyclerView.NO_POSITION;

    isFirstItemAdded = false;

    if (lm.getChildCount() > 0) {
        View initView = lm.getChildAt(0);
        topView = initView;
        bottomView = initView;
        leftView = initView;
        rightView = initView;

        for (View view : childViews) {
            int position = lm.getPosition(view);

            if (!isInside(view)) continue;

            if (lm.getDecoratedTop(view) < lm.getDecoratedTop(topView)) {
                topView = view;
            }

            if (lm.getDecoratedBottom(view) > lm.getDecoratedBottom(bottomView)) {
                bottomView = view;
            }

            if (lm.getDecoratedLeft(view) < lm.getDecoratedLeft(leftView)) {
                leftView = view;
            }

            if (lm.getDecoratedRight(view) > lm.getDecoratedRight(rightView)) {
                rightView = view;
            }

            if (minPositionOnScreen == RecyclerView.NO_POSITION || position < minPositionOnScreen) {
                minPositionOnScreen = position;
            }

            if (maxPositionOnScreen == RecyclerView.NO_POSITION || position > maxPositionOnScreen) {
                maxPositionOnScreen = position;
            }

            if (position == 0) {
                isFirstItemAdded = true;
            }
        }
    }
}
 
Example 18
Source File: DebateListAdapter.java    From kaif-android with Apache License 2.0 4 votes vote down vote up
public void clearSelection() {
  int prev = selectedPosition;
  selectedPosition = RecyclerView.NO_POSITION;
  notifyItemChanged(prev);
}
 
Example 19
Source File: FocusableLinearLayoutManager.java    From ticdesign with Apache License 2.0 3 votes vote down vote up
public FocusableLinearLayoutManager(Context context) {
    super(context, VERTICAL, false);

    mContext = context;
    mUiHandler = new Handler();

    mOnCentralPositionChangedListeners = new ArrayList<>();
    mPreviousCentral = RecyclerView.NO_POSITION;

    mScrollOffset = INVALID_SCROLL_OFFSET;

    mAppBarScrollController = new AppBarScrollController(mTicklableRecyclerView);

    setInFocusState(false);
}
 
Example 20
Source File: RecyclerViewPositionHelper.java    From Ticket-Analysis with MIT License 2 votes vote down vote up
/**
 * Returns the adapter position of the last visible view. This position does not include
 * adapter changes that were dispatched after the last layout pass.
 *
 * @return The adapter position of the last visible view or {@link RecyclerView#NO_POSITION} if
 * there aren't any visible items
 */
public int findLastVisibleItemPosition() {
    final View child = findOneVisibleChild(layoutManager.getChildCount() - 1, -1, false, true);
    return child == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(child);
}