應(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);
(初來乍到,一腳踏入不知深淺摊溶,望多多指教爬骤!)