SHARE
TWEET

RecyclerView list not getting updated until it is scrolled

allo_m Sep 11th, 2019 (edited) 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public class DiaryUserAdapter extends RecyclerView.Adapter<DiaryUserAdapter.MyViewHolder> {
  3.  
  4.     List<DiaryUser> diaryUsers;
  5.     Context context;
  6.     MainViewModel mainViewModel;
  7.  
  8.     public DiaryUserAdapter(Context context){
  9.         this.context = context;
  10.         mainViewModel = new ViewModelProvider((FragmentActivity)context).get(MainViewModel.class);
  11.     }
  12.  
  13.     @NonNull
  14.     @Override
  15.     public DiaryUserAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  16.         View view = LayoutInflater.from(context).inflate(R.layout.user_item, parent, false);
  17.         return new MyViewHolder(view);
  18.     }
  19.  
  20.     @Override
  21.     public void onBindViewHolder(@NonNull DiaryUserAdapter.MyViewHolder holder, int position) {
  22.         DiaryUser user = diaryUsers.get(position);
  23.         holder.tvName.setText(user.name);
  24.         holder.tvEmail.setText(user.email);
  25.         if(user.userId.equals(mainViewModel.getUserDetails().getUserId())){
  26.             holder.viewDelete.setVisibility(View.INVISIBLE);
  27.         }
  28.     }
  29.  
  30.     @Override
  31.     public int getItemCount() {
  32.         if(diaryUsers == null){
  33.             return 0;
  34.         }
  35.         return diaryUsers.size();
  36.     }
  37.  
  38.     public void loadList(List<DiaryUser> list){
  39.         diaryUsers = list;
  40.         notifyDataSetChanged();
  41.     }
  42.  
  43.     public void addItem(DiaryUser user){
  44.         diaryUsers.add(user);
  45.         this.notifyItemInserted(diaryUsers.size()-1);
  46. //        notifyItemRangeChanged(0, diaryUsers.size());
  47.         //this.notifyDataSetChanged();
  48.     }
  49.  
  50.     public void updateItem(String name, String email, int index){
  51.         DiaryUser user = diaryUsers.get(index);
  52.         if(name != null) user.name = name;
  53.         if(email != null) user.email = email;
  54.         this.notifyItemChanged(index);
  55. //        notifyDataSetChanged();
  56.     }
  57.  
  58.     public void deleteItem(int index){
  59.         diaryUsers.remove(index);
  60.         this.notifyItemRemoved(index);
  61.     }
  62.  
  63.     class MyViewHolder extends RecyclerView.ViewHolder {
  64.         TextView tvName, tvEmail;
  65.         View viewEdit, viewDelete;
  66.         public MyViewHolder(View itemView){
  67.             super(itemView);
  68.             tvName = itemView.findViewById(R.id.tv_manage_username);
  69.             tvEmail = itemView.findViewById(R.id.tv_manage_useremail);
  70.             viewEdit = itemView.findViewById(R.id.view_manage_edit);
  71.             viewDelete = itemView.findViewById(R.id.view_manage_delete);
  72.             viewEdit.setOnClickListener(v ->{
  73.                 int position = getAdapterPosition();
  74.                 if(position != RecyclerView.NO_POSITION){
  75.                     mainViewModel.setUserToEdit(diaryUsers.get(position));
  76.                     mainViewModel.setEditPosition(position);
  77.                 }
  78.             });
  79.             viewDelete.setOnClickListener(v -> {
  80.                 int position = getAdapterPosition();
  81.                 if(position != RecyclerView.NO_POSITION) mainViewModel.setRemovePosition(getAdapterPosition());
  82.             });
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. /************************ Fragment ****************/
  89.  
  90. import android.app.ProgressDialog;
  91. import android.os.Bundle;
  92. import android.util.Log;
  93. import android.view.LayoutInflater;
  94. import android.view.View;
  95. import android.view.ViewGroup;
  96. import android.widget.Toast;
  97.  
  98. import java.util.HashMap;
  99. import java.util.List;
  100. import java.util.Map;
  101.  
  102. import androidx.annotation.NonNull;
  103. import androidx.annotation.Nullable;
  104. import androidx.appcompat.app.AlertDialog;
  105. import androidx.fragment.app.DialogFragment;
  106. import androidx.fragment.app.Fragment;
  107. import androidx.fragment.app.FragmentTransaction;
  108. import androidx.lifecycle.ViewModelProvider;
  109. import androidx.recyclerview.widget.LinearLayoutManager;
  110. import androidx.recyclerview.widget.RecyclerView;
  111. import e.allan.mkadvocates.Adapters.DiaryUserAdapter;
  112. import e.allan.mkadvocates.Models.DiaryUser;
  113. import e.allan.mkadvocates.Models.UserDetails;
  114. import e.allan.mkadvocates.MyRetrofit;
  115. import e.allan.mkadvocates.R;
  116. import e.allan.mkadvocates.Utils.SimpleHelpers;
  117. import e.allan.mkadvocates.ViewModels.MainViewModel;
  118. import retrofit2.Call;
  119. import retrofit2.Callback;
  120. import retrofit2.Response;
  121.  
  122. import static e.allan.Utils.MyConstants.MANAGE_FRAGMENT_TAG;
  123.  
  124. public class ManageFragment extends Fragment {
  125.  
  126.     MainViewModel mainViewModel;
  127.     RecyclerView rv;
  128.     DiaryUserAdapter diaryUserAdapter;
  129.  
  130.     @Override
  131.     public void onCreate(@Nullable Bundle savedInstanceState) {
  132.         super.onCreate(savedInstanceState);
  133.         mainViewModel = new ViewModelProvider(getActivity()).get(MainViewModel.class);
  134.         diaryUserAdapter = new DiaryUserAdapter(getActivity());
  135.         if(mainViewModel.getUsersList() == null){
  136.             fetchUsers(); //diaryUserAdapter.loadList(fetchedList); is called in the fetchUsers() method once data is fetched
  137.         }else{
  138.             diaryUserAdapter.loadList(mainViewModel.getUsersList());
  139.         }
  140.         initObservers();
  141.     }
  142.  
  143.     public void initObservers(){
  144.         //on adapter item click
  145.         mainViewModel.getUserToEdit().observe(getActivity(), user ->{
  146.             if(user != null && rv != null){
  147.                 displayUserDialog(true);
  148.             }
  149.         });
  150.         //on update dialog submit
  151.         mainViewModel.getUserUpdate().observe(getActivity(), update ->{
  152.             if(update != null && rv != null){
  153.                 updateUser(update);
  154. //diaryUserAdapter.updateItem(newName, newEmail, //mainViewModel.getEditPosition()); is called in the //updateUser() method
  155.             }
  156.         });
  157.         mainViewModel.getNewUser().observe(getActivity(), details ->{
  158.             if(details != null && rv != null){
  159.                 createUser(details);
  160. //diaryUserAdapter.addItem(response.body()) is called //after user created in backend
  161.             }
  162.         });
  163.         mainViewModel.getRemovePosition().observe(getActivity(), position ->{
  164.             if(position != null && rv != null){
  165.                 displayDeleteAlert(position);
  166. // diaryUserAdapter.deleteItem(index) is called in //the displayDeleteAlert(position) method
  167.             }
  168.         });
  169.     }
  170.  
  171.     @Nullable
  172.     @Override
  173.     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  174.         View view = inflater.inflate(R.layout.frag_manage, container, false);
  175.         rv = view.findViewById(R.id.rv_manage_users);
  176.         mainViewModel.setAppTitle("M&K Advocates");
  177.         return view;
  178.     }
  179.     @Override
  180.     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  181.         super.onViewCreated(view, savedInstanceState);
  182.         rv.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
  183.         rv.setAdapter(diaryUserAdapter);
  184.         View addFab = view.findViewById(R.id.manage_fab);
  185.         addFab.setOnClickListener(v -> displayUserDialog(false));
  186.     }
  187.  
  188. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top