Android 在不可操作时 获取Recyclerview的viewholder并动态改变view

来源:互联网 发布:淘宝双十一收货时间 编辑:程序博客网 时间:2024/04/28 17:19

Android 在不可操作时 获取Recyclerview的viewholder并动态改变view

应公司的需求,需要在没有触摸屏只有上下确定和取消的物理按键设备上操作Recyclerview并标记出当前选中栏(多一嘴,卧槽啊,搞毛啊,省预算也不是这么省的啊!不带触摸屏你用QT啊!QT效果不好?不带触摸屏加物理按键效果就好了?!),做的时候就想当然的在bindView里把holder添加到了集合里,然后根据上下键事件往下走改变view,然后,突然很傻比的发现忽略了最基本的事情,Recyclerview就只会在一开始创建当前屏幕显示数的holder,并不能获取到全部的holder,这样我在按下键的时候就只会走到当前显示的最后item上去,下面就再也走不动了。

好吧,开始找方法,大体思路就是动态的来获取holder改变布局,但是我需要在外面的物理按键回调里改变Recyclerview里面的view,嗯,一开始还就创建几个,后面的就不能再onBind里拿到了,再看看有没有什么其他方法,看见一个,getChildViewHolder(View child),嗯,看方法名很靠谱,但是参数是view。。。。

好吧,只能进去看看代码了,从上到下翻了一圈,发觉有一个方法

    /**     * 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?嗯?继续按走到最后再往上走就能获取到了?这玩意还有延迟不成?好吧。。再进去源码看吧。

for (int i = 0; i < childCount; i++) {            final ViewHolder holder = getChildViewHolderInt(mChildHelper.getUnfilteredChildAt(i));            //这里循环检查已存在的holder            if (holder != null && !holder.isRemoved() && getAdapterPositionFor(holder) == position) {                if (mChildHelper.isHidden(holder.itemView)) {                    hidden = holder;                } else {                    return holder;                }            }        }

嗯。。。。。并没有找到为啥,但是看在以上循环检查的时候debug知道以前滚过的item的holder是有的,可以,这样这个需求的难度就是不存在的了兄弟~,立马改代码,让每次多scroll一个角标,成功实现!

rvPsytopicList.scrollToPosition(index+1);RecyclerView.ViewHolder viewHolderForAdapterPosition = rvPsytopicList.findViewHolderForAdapterPosition(index);

(初来乍到,一脚踏入不知深浅,望多多指教!)

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 跑步机钥匙丢了怎么办 跑步机磁铁丢了怎么办 瑜伽拉筋拉伤了怎么办康复 大腿拉筋拉伤了怎么办 肚子饿的咕咕叫怎么办 手表折叠扣松了怎么办? 手表链扣松了怎么办? 原车挡泥板碎了怎么办 车检通不过的车怎么办 车子被轻微刮了怎么办 车龙门架弯了怎么办 材料合格工地非说不合格怎么办 汽车翼子板撞过弄好缝隙大怎么办 手表秒针摔掉了怎么办 元征升降机泄压怎么办 自拍杆的杆子往下下滑怎么办 小狗被车压了一下拉血怎么办 有鬼给你磕头让你不好怎么办 腿又粗又短怎么办 腿又短又粗怎么办 因跑步小腿变粗怎么办 健身后小腿变粗怎么办 打非洲鼓打的手疼怎么办 无塔供水压力小怎么办 家用增压水塔压力不稳定怎么办 蝴蝶耳堵比较紧怎么办 跑步机踏板坏了怎么办 遥控器电池没电了怎么办 空调遥控器电池没电了怎么办 想要自慰家里没有情趣用品怎么办 对政协提案的答复有意见怎么办 邻居在自建房养殖鳖怎么办 江桥全民健身卡怎么办 南翔全民健身卡怎么办 椭圆机皮带断了怎么办 不小心扭腰了怎么办 扭腰之后腿疼怎么办 扭腰怎么办吃什么补 湖州奥体中心怎么办卡 学车把教练投诉了怎么办 白球鞋鞋边发黄怎么办