滚动到视图之外时,将项目的状态保存在recyclerview中 [英] save state of an item in recyclerview when scrolled out of the view

查看:92
本文介绍了滚动到视图之外时,将项目的状态保存在recyclerview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个recyclerView,其中最多包含20个项目.在行模板中,我有一个文本视图和一个最初隐藏的按钮,在recyclerview项上单击该按钮的可见性.问题是显示第一行中的按钮时,如果将其从视图中滚动出来,然后再向后滚动,则该按钮将再次不可见,而不会调用toogle可见性.滚动时如何在回收器视图中保存对象的状态看不见

In my project i have a recyclerView which contains at most 20 items. In the row template i have a text view and a button which is hidden initially and on the recyclerview item click the visibility of the button is toogled. The problem is when the button in the first row is shown and if it is scrolled out of the view and then scrolled back the button is invisible again without calling toogle visibility.How can i save the sate of objects in recycler view when it is scrolled out of the view

推荐答案

使用哈希表存储状态.单击按钮时,通过调用 getPosition()和可见性

used a hash map to store states. when the button is clicked the hashmap is updated with the position by calling getPosition() and the visibility

 public void onClick(View v) {
                Btn.setVisibility(Btn.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                statesMap.put(getPosition(),Btn.getVisibility() == View.VISIBLE );
            }

,然后从哈希映射中读取值

and then read the values from the hash map in

onBindViewHolder(MyViewHolder持有人,int位置)

通过位置

holder.Btn.setVisibility(statesMap.get(position) != null && statesMap.get(position) != false ? View.VISIBLE : View.GONE);

这篇关于滚动到视图之外时,将项目的状态保存在recyclerview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆