Tuesday, 20 December 2022

Fragment transition has slight delay when replacing fragment with another, how do I prevent it?

I have a fragment A inside my activity. When I want to replace that fragment, I have a transition to move fragment A off screen and move fragment B onto the screen.

However, before fragment B is displayed, there is a slight lag/delay. I have narrowed down the problem to be my Recycler Adapter loading 5 or more items causing that 0.5 second lag (There is no lag when the Recycler has no items). It also only happens on the first loading of items.

I think it's because the adapter loading is taking about 0.5 seconds on the ui thread.

My question is, How can I design it so there is no more of that delay?

My Adapter is called inside the fragment's onViewCreated() but that doesn't seem to remove the delay.

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        viewModel = new ViewModelProvider(this).get(MainViewModel.class);

        adapter = new BackpackAdapter(viewModel.getListItems());
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        recyclerView.setAdapter(backpackAdapter);
    }

I am also using Glide to load the images on a background thread but it doesn't seem to remove the delay either.

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
        Glide.with(holder.itemView.getContext())
                .load(list.get(i).getImage())
                .apply(RequestOptions.centerCropTransform())
                .into(holder.imageView);

        ...
        ...

This is my Fragment transition to take fragment A off screen to display B

getChildFragmentManager().beginTransaction()
                .setReorderingAllowed(true)
                .setCustomAnimations(R.anim.enter_right, R.anim.exit_left, R.anim.enter_left, R.anim.exit_right)
                .replace(R.id.container_view, new FragmentB(), "FRAG_B")
                .commit();

Does anyone know what I can do to make the Fragment Transition more smoothe?



from Fragment transition has slight delay when replacing fragment with another, how do I prevent it?

No comments:

Post a Comment