save state of an item in recyclerview when scrolled out of the view

夙愿已清 提交于 2021-02-19 03:40:13

问题


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


回答1:


Your RecyclerView list items get their state from the objects in the List/Array they are based on the objects used by to your adapter class.

If you want to save a checked state you can add a boolean field to your object class to set and hold the value so that when your adapter, update this on click and set the visibility of the button accordingly.

Instead of Using Strings in your ArrayList use a simple object class to hold the String and an additional boolean value for the state of the button visibility.

Such a class could look like this

        class Items {
        String myString;
        boolean visibility;

        public String getMyString(){
            return myString;
        }

        public boolean isVisible(){
            return visibility;
        }

        public void setVisibility(boolean visibility){
            this.visibility = visibility;
        }
    }

In your RecyclerView you will have a method to set the value of the list items, I've never used it but I believe it is called bindView? Here where you set the text value of your list item also set the visibility according to the value returned by item.getVisibility() and set it when you call the onClick method of your button.

Hope this is helpful.




回答2:


You can use shared preference for this purpose

onRecyclerViewItemClick insert key value in share preference where key should be position and value should be true/false

and in your adapter implement if condition where item view created like

if(sharedpreferences.contains(String.valueof(position)) && sharedpreferences.getString(String.valueof(position), "").equals("true")){

//button visible

}

else{
 // button gone/invisible
}

Hope this would help you




回答3:


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 holder, int position)

by passing the position

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


来源:https://stackoverflow.com/questions/28796328/save-state-of-an-item-in-recyclerview-when-scrolled-out-of-the-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!