應(yīng)公司的需求,需要在沒有觸摸屏只有上下確定和取消的物理按鍵設(shè)備上操作Recyclerview并標(biāo)記出當(dāng)前選中欄(多一嘴,臥槽啊,搞毛啊,省預(yù)算也不是這么省的??!不帶觸摸屏你用QT??!QT效果不好?不帶觸摸屏加物理按鍵效果就好了?!),做的時候就想當(dāng)然的在bindView里把holder添加到了集合里,然后根據(jù)上下鍵事件往下走改變view,然后,突然很傻比的發(fā)現(xiàn)忽略了最基本的事情,Recyclerview就只會在一開始創(chuàng)建當(dāng)前屏幕顯示數(shù)的holder,并不能獲取到全部的holder,這樣我在按下鍵的時候就只會走到當(dāng)前顯示的最后item上去,下面就再也走不動了。
好吧,開始找方法,大體思路就是動態(tài)的來獲取holder改變布局,但是我需要在外面的物理按鍵回調(diào)里改變Recyclerview里面的view,嗯,一開始還就創(chuàng)建幾個,后面的就不能再onBind里拿到了,再看看有沒有什么其他方法,看見一個,getChildViewHolder(View child),嗯,看方法名很靠譜,但是參數(shù)是view。。。。
好吧,只能進去看看代碼了,從上到下翻了一圈,發(fā)覺有一個方法
/**
* Return the ViewHolder for the item in the given position of the data set. Unlike
* {@link #findViewHolderForLayoutPosition(int)} this method takes into account any pending
* adapter changes that may not be reflected to the layout yet. On the other hand, if
* {@link Adapter#notifyDataSetChanged()} has been called but the new layout has not been
* calculated yet, this method will return <code>null</code> since the new positions of views
* are unknown until the layout is calculated.
* <p>
* This method checks only the children of RecyclerView. If the item at the given
* <code>position</code> is not laid out, it <em>will not</em> create a new one.
* <p>
* When the ItemAnimator is running a change animation, there might be 2 ViewHolders
* representing the same Item. In this case, the updated ViewHolder will be returned.
*
* @param position The position of the item in the data set of the adapter
* @return The ViewHolder at <code>position</code> or null if there is no such item
*/
public ViewHolder findViewHolderForAdapterPosition(int position) {
if (mDataSetHasChangedAfterLayout) {
return null;
}
final int childCount = mChildHelper.getUnfilteredChildCount();
// hidden VHs are not preferred but if that is the only one we find, we rather return it
ViewHolder hidden = null;
for (int i = 0; i < childCount; i++) {
final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
if (holder != null && !holder.isRemoved() && getAdapterPositionFor(holder) == position) {
if (mChildHelper.isHidden(holder.itemView)) {
hidden = holder;
} else {
return holder;
}
}
}
return hidden;
}
嗯,除了mDataSetHasChangedAfterLayout這個變量看起來有點辣眼,后面明顯就可以獲取到我所需要的holder啊,可以可以,來邊走邊試
rvPsytopicList.scrollToPosition(index);
RecyclerView.ViewHolder viewHolderForAdapterPosition = rvPsytopicList.findViewHolderForAdapterPosition(index);
先讓它滑動到我所需要改變的item,更新holder,然后我獲取holder該干嘛干嘛,嗯,簡直天才。嗯?走到超出屏幕的下一條返回了個null?嗯?繼續(xù)按走到最后再往上走就能獲取到了?這玩意還有延遲不成?好吧。。再進去源碼看吧。
for (int i = 0; i < childCount; i++) {
final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));
//這里循環(huán)檢查已存在的holder
if (holder != null && !holder.isRemoved() && getAdapterPositionFor(holder) == position) {
if (mChildHelper.isHidden(holder.itemView)) {
hidden = holder;
} else {
return holder;
}
}
}
嗯。。。。。并沒有找到為啥,但是看在以上循環(huán)檢查的時候debug知道以前滾過的item的holder是有的,可以,這樣這個需求的難度就是不存在的了兄弟~,立馬改代碼,讓每次多scroll一個角標(biāo),成功實現(xiàn)!
rvPsytopicList.scrollToPosition(index+1);
RecyclerView.ViewHolder viewHolderForAdapterPosition = rvPsytopicList.findViewHolderForAdapterPosition(index);
(初來乍到,一腳踏入不知深淺,望多多指教!)