下面列出了androidx.recyclerview.widget.RecyclerView#findViewHolderForAdapterPosition ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
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);
}
}
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);
}
};
}
@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);
}
}
@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);
}
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;
}
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);
}
}
}
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++;
}
}
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();
}
@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);
}
@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();
}
}
}
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;
}
}
};
}
/**
* 绘制悬浮组
*
* @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();
}
}
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;
}
}
};
}