Java Code Examples for androidx.recyclerview.widget.RecyclerView#findViewHolderForAdapterPosition()

The following examples show how to use androidx.recyclerview.widget.RecyclerView#findViewHolderForAdapterPosition() . 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: GamesFragment.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 6 votes vote down vote up
public void viewGame(int gid, boolean locked) {
    if (adapter == null) return;

    if (locked && adapter.doesFilterOutLockedLobbies()) {
        Prefs.putBoolean(PK.FILTER_LOCKED_LOBBIES, false);
        adapter.setFilterOutLockedLobbies(false);
    }

    int pos = Utils.indexOf(adapter.getVisibleGames(), gid);
    if (pos != -1) {
        RecyclerView list = rmv.list();
        list.scrollToPosition(pos);
        RecyclerView.ViewHolder holder = list.findViewHolderForAdapterPosition(pos);
        if (holder instanceof GamesAdapter.ViewHolder)
            ((GamesAdapter.ViewHolder) holder).expand.performClick();
    }
}
 
Example 2
Source File: FollowingHomePageView.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void setAvatarAppearance(RecyclerView recyclerView) {
    User user = followingAdapter.getUser(avatarPosition);
    if (user == null) {
        return;
    }

    if (lastActor == null || !lastActor.username.equals(user.username)) {
        setAvatarImage(avatarPosition);
    }

    RecyclerView.ViewHolder lastHolder
            = recyclerView.findViewHolderForAdapterPosition(lastAvatarPosition);
    if (lastHolder instanceof TitleFeedHolder) {
        ((TitleFeedHolder) lastHolder).setAvatarVisibility(true);
    }

    RecyclerView.ViewHolder newHolder
            = recyclerView.findViewHolderForAdapterPosition(avatarPosition);
    if (newHolder instanceof TitleFeedHolder) {
        ((TitleFeedHolder) newHolder).setAvatarVisibility(false);
    }
}
 
Example 3
Source File: Matchers.java    From Kore with Apache License 2.0 6 votes vote down vote up
public static Matcher<View> withOnlyMatchingDataItems(final Matcher<View> dataMatcher) {
    return new TypeSafeMatcher<View>() {
        @Override
        protected boolean matchesSafely(View view) {
            if (!(view instanceof RecyclerView))
                return false;

            RecyclerView recyclerView = (RecyclerView) view;
            for (int i = 0; i < recyclerView.getChildCount(); i++) {
                RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
                if (! dataMatcher.matches(viewHolder.itemView)) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("withOnlyMatchingDataItems: ");
            dataMatcher.describeTo(description);
        }
    };
}
 
Example 4
Source File: FromRecyclerViewListener.java    From GestureViews with Apache License 2.0 6 votes vote down vote up
@Override
void scrollToPosition(RecyclerView list, int pos) {
    if (list.getLayoutManager() instanceof LinearLayoutManager) {
        // Centering item in its parent
        final LinearLayoutManager manager = (LinearLayoutManager) list.getLayoutManager();
        final boolean isHorizontal = manager.getOrientation() == LinearLayoutManager.HORIZONTAL;

        int offset = isHorizontal
                ? (list.getWidth() - list.getPaddingLeft() - list.getPaddingRight()) / 2
                : (list.getHeight() - list.getPaddingTop() - list.getPaddingBottom()) / 2;

        final RecyclerView.ViewHolder holder = list.findViewHolderForAdapterPosition(pos);
        if (holder != null) {
            final View view = holder.itemView;
            offset -= isHorizontal ? view.getWidth() / 2 : view.getHeight() / 2;
        }

        manager.scrollToPositionWithOffset(pos, offset);
    } else {
        list.scrollToPosition(pos);
    }
}
 
Example 5
Source File: HowToPlayTutorial.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private static RecyclerView.ViewHolder getFirstVisibleViewHolder(@NonNull RecyclerView list) {
    LinearLayoutManager llm = (LinearLayoutManager) list.getLayoutManager();
    if (llm == null) return null;

    int pos = llm.findFirstCompletelyVisibleItemPosition();
    if (pos == -1) return null;
    else return list.findViewHolderForAdapterPosition(pos);
}
 
Example 6
Source File: MainAdapter.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
public int getCurrentTemperatureTextHeight(RecyclerView recyclerView) {
    if (headerCurrentTemperatureTextHeight <= 0 && getItemCount() > 0) {
        AbstractMainViewHolder holder = (AbstractMainViewHolder) recyclerView.findViewHolderForAdapterPosition(0);
        if (holder instanceof HeaderViewHolder) {
            headerCurrentTemperatureTextHeight
                    = ((HeaderViewHolder) holder).getCurrentTemperatureHeight();
        }
    }
    return headerCurrentTemperatureTextHeight;
}
 
Example 7
Source File: MainAdapter.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void onScroll(RecyclerView recyclerView) {
    AbstractMainViewHolder holder;
    for (int i = 0; i < getItemCount(); i ++) {
        holder = (AbstractMainViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
        if (holder != null && holder.getTop() < recyclerView.getMeasuredHeight()) {
            holder.enterScreen(pendingAnimatorList, listAnimationEnabled);
        }
    }
}
 
Example 8
Source File: ActionableAdapter.java    From RecyclerExt with Apache License 2.0 5 votes vote down vote up
public void updateVisibleViewHolders() {
    RecyclerView recyclerView = boundRecyclerView;
    if (recyclerView == null) {
        Log.d(TAG, "Ignoring updateVisibleViewHolders() when no RecyclerView is bound");
        return;
    }

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager == null) {
        Log.d(TAG, "Ignoring updateVisibleViewHolders() when no LayoutManager is bound");
        return;
    }

    int startPosition = 0;
    if (layoutManager instanceof LinearLayoutManager) {
        startPosition = ((LinearLayoutManager)layoutManager).findFirstVisibleItemPosition();
    } else {
        Log.e(TAG, "updateVisibleViewHolders() currently only supports LinearLayoutManager and it's subclasses");
        return;
    }

    int i = startPosition;
    RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(startPosition);

    while (holder != null && i < getItemCount()) {
        holder = recyclerView.findViewHolderForAdapterPosition(i);
        if (holder != null && holder instanceof ActionableView) {
            ((ActionableView) holder).onActionModeChange(inActionMode);
        }
        i++;
    }
}
 
Example 9
Source File: ItemAdapter.java    From nextcloud-notes with GNU General Public License v3.0 5 votes vote down vote up
public void clearSelection(@NonNull RecyclerView recyclerView) {
    for (Integer i : getSelected()) {
        RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(i);
        if (viewHolder != null) {
            viewHolder.itemView.setSelected(false);
        } else {
            Log.w(TAG, "Could not found viewholder to remove selection");
        }
    }
    selected.clear();
}
 
Example 10
Source File: RecyclerViewActions.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public void perform(UiController uiController, View view) {
  RecyclerView recyclerView = (RecyclerView) view;

  new ScrollToPositionViewAction(position).perform(uiController, view);
  uiController.loopMainThreadUntilIdle();

  @SuppressWarnings("unchecked")
  VH viewHolderForPosition = (VH) recyclerView.findViewHolderForAdapterPosition(position);
  if (null == viewHolderForPosition) {
    throw new PerformException.Builder()
        .withActionDescription(this.toString())
        .withViewDescription(HumanReadables.describe(view))
        .withCause(new IllegalStateException("No view holder at position: " + position))
        .build();
  }

  View viewAtPosition = viewHolderForPosition.itemView;
  if (null == viewAtPosition) {
    throw new PerformException.Builder()
        .withActionDescription(this.toString())
        .withViewDescription(HumanReadables.describe(viewAtPosition))
        .withCause(new IllegalStateException("No view at position: " + position))
        .build();
  }

  viewAction.perform(uiController, viewAtPosition);
}
 
Example 11
Source File: AdapterView.java    From android-player-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
    super.onDetachedFromRecyclerView(recyclerView);
    int childCount = recyclerView.getChildCount();
    //We need to stop the player to avoid a potential memory leak.
    for (int i = 0; i < childCount; i++) {
        ViewHolder holder = (ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
        if (holder != null && holder.videoView != null) {
            holder.videoView.stopPlayback();
        }
    }
}
 
Example 12
Source File: RecyclerViewMatcher.java    From adamant-android with GNU General Public License v3.0 4 votes vote down vote up
public Matcher<View> atPositionOnView(final int position, final int targetViewId) {

        return new TypeSafeMatcher<View>() {
            Resources resources = null;
            View childView;

            public void describeTo(Description description) {
                String idDescription = Integer.toString(recyclerViewId);
                if (this.resources != null) {
                    try {
                        idDescription = this.resources.getResourceName(recyclerViewId);
                    } catch (Resources.NotFoundException var4) {
                        idDescription = String.format("%s (resource name not found)",
                                                      new Object[] { Integer.valueOf
                                                          (recyclerViewId) });
                    }
                }

                description.appendText("with id: " + idDescription);
            }

            public boolean matchesSafely(View view) {

                this.resources = view.getResources();

                if (childView == null) {
                    RecyclerView recyclerView =
                        (RecyclerView) view.getRootView().findViewById(recyclerViewId);
                    if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
                        childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
                    }
                    else {
                        return false;
                    }
                }

                if (targetViewId == -1) {
                    return view == childView;
                } else {
                    View targetView = childView.findViewById(targetViewId);
                    return view == targetView;
                }

            }
        };
    }
 
Example 13
Source File: GroupItemDecoration.java    From CalendarView with Apache License 2.0 4 votes vote down vote up
/**
 * 绘制悬浮组
 *
 * @param c      Canvas
 * @param parent RecyclerView
 */
protected void onDrawOverGroup(Canvas c, RecyclerView parent) {
    int firstVisiblePosition = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
    if (firstVisiblePosition == RecyclerView.NO_POSITION) {
        return;
    }
    Group group = getCroup(firstVisiblePosition);
    if (group == null)
        return;
    String groupTitle = group.toString();
    if (TextUtils.isEmpty(groupTitle)) {
        return;
    }
    boolean isRestore = false;
    Group nextGroup = getCroup(firstVisiblePosition + 1);
    if (nextGroup != null && !group.equals(nextGroup)) {
        //说明是当前组最后一个元素,但不一定碰撞了
        View child = parent.findViewHolderForAdapterPosition(firstVisiblePosition).itemView;
        if (child.getTop() + child.getMeasuredHeight() < mGroupHeight) {
            //进一步检测碰撞
            c.save();//保存画布当前的状态
            isRestore = true;
            c.translate(0, child.getTop() + child.getMeasuredHeight() - mGroupHeight);
        }
    }
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();
    int top = parent.getPaddingTop();
    int bottom = top + mGroupHeight;
    c.drawRect(left, top, right, bottom, mBackgroundPaint);
    float x;
    float y = top + mTextBaseLine;
    if (isCenter) {
        x = parent.getMeasuredWidth() / 2 - getTextX(groupTitle);
    } else {
        x = mPaddingLeft;
    }
    c.drawText(groupTitle, x, y, mTextPaint);
    if (isRestore) {
        //还原画布为初始状态
        c.restore();
    }
}
 
Example 14
Source File: RecyclerViewMatcher.java    From Barricade with Apache License 2.0 4 votes vote down vote up
private Matcher<View> atPositionOnView(final int position, final int targetViewId) {

    return new TypeSafeMatcher<View>() {
      Resources resources = null;
      View childView;

      public void describeTo(Description description) {
        String idDescription = Integer.toString(recyclerViewId);
        if (this.resources != null) {
          try {
            idDescription = this.resources.getResourceName(recyclerViewId);
          } catch (Resources.NotFoundException var4) {
            idDescription = String.format("%s (resource name not found)", recyclerViewId);
          }
        }

        description.appendText("with id: " + idDescription);
      }

      public boolean matchesSafely(View view) {

        this.resources = view.getResources();

        if (childView == null) {
          RecyclerView recyclerView = (RecyclerView) view.getRootView().findViewById(recyclerViewId);
          if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
            childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
          } else {
            return false;
          }
        }

        if (targetViewId == -1) {
          return view == childView;
        } else {
          View targetView = childView.findViewById(targetViewId);
          return view == targetView;
        }
      }
    };
  }