Search code examples
sharedpreferencesandroid-recyclerviewobserversandroid-sharedpreferences

Update RecyclerView adapter after SharedPrefereces changed


I have RecyclerView with custom RecyclerView adapter.

Each row of RecyclerView "opens"(when user clicked on it) using CardFlipAnimation(AnimatorSet).

In each row there is a button that change int value in SharedPreferences , and TextView that shows this value.

If i have opened more than one row i need dynamically change all TextViews in all rows which are opened.

NotifyDataSetChanged and etc updates data in RecyclerView great but the is a problem that they close opened rows(because for standart thay are closed and then open with AnimatorSet)

My main mission now is to understand how to update only one TextView in all opened rows and to keep them opened(dont touch AnimatorSet). I think i can use an Observer,but i didn't know how correctly implement him.

If you any ideas please help me.

Thanks for answers


Solution

  • Ok, finaly i got a solution , it's a little bit ugly but it works exactly as I need.

    First: I pass my RecyclerView to Adapter

            ...
            public CardBaseAdapter(RecyclerView recyclerView,...) {
            ...
            this.mRecyclerView = recyclerView;
            ....
    

    Second: Create a method in my Adapter :

        public void updateViews() {
    
            for(int i =0;i<getItemCount();i++){
    
                HeaderViewHolder holder = (HeaderViewHolder)recyclerView.findViewHolderForAdapterPosition(i);
                if(holder!=null){
    
                 //Update views at holders
                }
    
            }
    
    }
    

    Third: Call this function when i need to update view

    P.S. I'm not sure that this is the correct way to update view's , but it works, if you know another, good solution , post it pls