Android 在不可操作時 獲取Recyclerview的viewholder并動態(tài)改變view

應(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);

(初來乍到,一腳踏入不知深淺,望多多指教!)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,115評論 25 709
  • 這篇文章分三個部分,簡單跟大家講一下 RecyclerView 的常用方法與奇葩用法;工作原理與ListView比...
    LucasAdam閱讀 4,705評論 0 27
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,163評論 22 665
  • 今天朋友問我 你喜歡了那么久的他現(xiàn)在還有感覺嗎 我想了很久 沒有說那句差點脫口而出的沒有 我說 我舉個例子吧 你有...
    很怪閱讀 439評論 0 0
  • 清明假日,我和小伙伴驅(qū)車前往,奔向梨木臺。 梨木臺,被專家學(xué)者稱為“天津的神農(nóng)架”、“北方的熱帶雨林”。它是國家地...
    安之騰閱讀 1,168評論 16 13

友情鏈接更多精彩內(nèi)容