1

I have created the bottom navigation with five fragments, which won't be recreated again and again while clicking the tabs. This has been done to reduce the number of API calls. In all the fragments recycler view is present and a pull to refresh is also implemented. The issue comes when I do pull to refresh, am getting data from the server, but recycler view is not updating. Please shed some light on what am doing wrong.

BottomNavigation.class

  @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_std_bottom_bar);
    studentSchoolDataBaseController = Application.getInstance().getStudentSchoolDataBaseController();
    studentApiController = new StudentApiController(this);
    studentAbsenceApiController = new StudentAbsenceApiController(this);
    studentCoursesApiController = new StudentCoursesApiController(this);
    studentBottomNavView = (BottomNavigationView) findViewById(R.id.navigation1);
    CustomFont.cairo_font(getApplicationContext(), studentBottomNavView);
    if (getIntent().getExtras() != null) {
        notificationType = getIntent().getExtras().getString("type");
        comesFromNotification = true;
        msgFrom = getIntent().getExtras().getString(StaticConfig.CHAT_MSG_USER_TYPE);
    }
    fragmentManager.beginTransaction().add(R.id.main_container, studCurrCourseFragment, "1").hide(studCurrCourseFragment).commit();
    fragmentManager.beginTransaction().add(R.id.main_container, stdPointsFragment, "2").hide(stdPointsFragment).commit();
    fragmentManager.beginTransaction().add(R.id.main_container, stdHome, "3").commit();
    fragmentManager.beginTransaction().add(R.id.main_container, stdTaskAndHw, "4").hide(stdTaskAndHw).commit();
     fragmentManager.beginTransaction().add(R.id.main_container, stdRealtimeChat, "5").hide(stdRealtimeChat).commit();
    showLoadingDialog();
    Handler handler = new Handler(Looper.getMainLooper());


    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            //hide Progressbar here or close your activity.
            if(progressDialog!=null && progressDialog.isShowing())
            {
                progressDialog.dismiss();
            }

        }
    },10*1000);

    BottomNavigationViewHelper.removeNavigationShiftMode(studentBottomNavView);
    studentBottomNavView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                Fragment fragment = null;

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;

                    switch (item.getItemId()) {
                        case R.id.navigation_std_current_course:
                            
fragmentManager.beginTransaction().hide(active).show(studCurrCourseFragment).commit();
                            active = studCurrCourseFragment;

                            isHome = false;
                            return true;
                        case R.id.navigation_std_points:

                            
fragmentManager.beginTransaction().hide(active).show(stdPointsFragment).commit();
                            active = stdPointsFragment;
                            isHome = false;
                            return true;
                        case R.id.navigation_std_home:

                            
fragmentManager.beginTransaction().hide(active).show(stdHome).commit();
                            active = stdHome;
                            isHome = true;
                            return true;
                        case R.id.navigation_std_task_hw:

                            
 fragmentManager.beginTransaction().hide(active).show(stdTaskAndHw).commit();
                            active = stdTaskAndHw;
                            isHome = false;
                            return true;
                        case R.id.navigation_chat
                            
  fragmentManager.beginTransaction().hide(active).show(stdRealtimeChat).commit();
                            active = stdRealtimeChat;
                            isHome = false;
                            return true;
                    }
                    return false;
                }
            });
    studentBottomNavView.setOnNavigationItemReselectedListener(new 
 BottomNavigationView.OnNavigationItemReselectedListener() {
        @Override
        public void onNavigationItemReselected(@NonNull MenuItem item) {
            //Toast.makeText(BottomStdNavigation.this, "Reselected", 
  Toast.LENGTH_SHORT).show();

        }
    });
   
    setNavigationTypeface();

}

Adding one of my fragment class below

Stdpoints.java

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new ActionBarCustomization(getActivity(), "", R.layout.include_navbar_add_points);
    initializeView();


}

@Override
public void onStart() {
    super.onStart();
}


public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_std_points, container, false);
    findViews(rootView);
    pointsReplaceBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), StudentPointsReplaceActivity.class);
            startActivity(intent);
        }
    });

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initializeView();
            swipeRefreshLayout.setRefreshing(false);
        }
    });
    return rootView;
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
}

private void initializeView() {
  //  new ActionBarCustomization(getActivity(), "", R.layout.include_toolbar_back);
    studentPointsApiController = new StudentPointsApiController(getActivity());
    getPointsDetails();
    getReplacePointsRequests();


}
private void findViews(View view) {
    recyclerView = (RecyclerView)view. findViewById(R.id.student_points_details_recycler_view);
    pointsReplacerecyclerView = (RecyclerView) view.findViewById(R.id.student_points_replace_sent_requests_recycler_view);
    pointsReplacerecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, true));
    swipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.std_pts_refresh_id);
    studentPointsDetailsTotal = (CustomTextView)view.findViewById(R.id.student_points_details_total);
    studentPointsDetailsReplaced = (CustomTextView)view. findViewById(R.id.student_points_details_replaced);
    studentPointsDetailsRemain = (CustomTextView)view. findViewById(R.id.student_points_details_remain);
    pointsReplaceBtn = (CustomButton)view.findViewById(R.id.addStdPointBtnID);
}

private void initializeRecyclerAdapter() {
    studentPointsDetailsRecyclerAdapter = new StudentPointsDetailsRecyclerAdapter(getActivity(), studentBalance.getBalanceDetails());
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(studentPointsDetailsRecyclerAdapter);
    //setData();
}

public void getPointsDetails() {
    studentPointsApiController.getPointsDetails(new ObjectCallback() {
        @Override
        public void onSuccess(Object o) {
            studentBalance = ((StudentBalance) o);
            setData();
            initializeRecyclerAdapter();
        }

        @Override
        public void onFailed() {

        }
    });
}

private void getReplacePointsRequests() {
    studentPointsApiController.getStudentPointsReplaceRequests(new ListCallback() {
        @Override
        public void onSuccess(List<Object> objects) {
            replacedPoints.clear();
            replacedPoints.addAll((List<ReplacedPoints>) (List<?>) objects);
            pointsReplaceRequestsRecyclerAdapter = new 
 PointsReplaceRequestsRecyclerAdapter(getContext(),replacedPoints, StdPoints.this);
 //              RecyclerView.LayoutManager mLayoutManager1 = new 
            LinearLayoutManager(getContext());
  //              recyclerView.setLayoutManager(mLayoutManager1);
            pointsReplacerecyclerView.setItemAnimator(new DefaultItemAnimator());
            pointsReplacerecyclerView.setAdapter(pointsReplaceRequestsRecyclerAdapter);
        }

        @Override
        public void onFail() {

        }
    });
}

Adapter Class

StudentPointsDetailsReclyerAdaper.java

 private Context context;
private List<BalanceDetails> balanceDetails = new ArrayList<>();

public StudentPointsDetailsRecyclerAdapter(Context context, List<BalanceDetails> 
 balanceDetails) {
    this.context = context;
    this.balanceDetails = balanceDetails;
}

@NonNull
@Override
public StudentPointsDetailsHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_points_details, parent, false);
    return new StudentPointsDetailsHolder(view, context);
}

@Override
public void onBindViewHolder(@NonNull StudentPointsDetailsHolder holder, int position) {
    holder.setData(balanceDetails.get(position));
}

@Override
public int getItemCount() {
    return balanceDetails.size();
}
Mac_Play
  • 302
  • 4
  • 21
  • 1. What exactly do you mean the RecyclerView is not updating, have you checked that you get different data on refresh? Maybe API is returning the same data and nothing changes on UI which leads you to believe that `RecyclerView` is not updating. 2. You need to give more details, you're using two RecyclerViews, which one is not showing the data? 3. Have you checked that `onSuccess` is called? There is a method that can't be concluded what its purpose is, like `setData()`. More details you give more helpful we can be – Farid Mar 23 '21 at 07:57
  • I agree with @Farid, do you have any pagination in the api because it seems that every time api returns same data. – Rahul Chauhan Mar 23 '21 at 08:11
  • @Farid Thanks for the reply. 1. Yes, I have confirmed, I am getting different data from the server, but it always shows the very first data. 2. None of the recyclerviews are updating. 3. Yes, OnSuccess is called and am getting different data. setData() - fill the data for Textview in the adapter class. If I kill the app and open it again, then am getting the updated data. FYI, while doing pull to refresh, even getCount method is not calling in Adapter class. I doubt, this issue link with Fragments which are not recreating? – Mac_Play Mar 23 '21 at 08:16
  • @Farid Sorry, setData fills the data in the Fragment class, not in the adapter. – Mac_Play Mar 23 '21 at 08:24

0 Answers0