提问者:小点点

Android从导航栏中选择元素冻结负载


我有一个底部导航栏,有多个元素,把我送到不同的碎片。

 bottomNavigationView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.navigation_pokemon_description:
                            selectedFragment = PokemonDescriptionFragment.newInstance(MainActivity.getSelectedPokemon().get_id());
                            break;
                        case R.id.navigation_pokemon_evolutions:
                            selectedFragment = PokemonEvolutionsFragment.newInstance();
                            break;
                        case R.id.navigation_pokemon_moves:
                            selectedFragment = PokemonMovesFragment.newInstance();
                            break;
                        case R.id.navigation_pokemon_breeding:
                            selectedFragment = PokemonBreedingTrainingFragment.newInstance();
                            break;
                        case R.id.navigation_pokemon_location:
                            selectedFragment = PokemonLocationFragment.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;
                }
            });

除了Moves片段之外,所有的片段都可以正常工作,当我按下Moves片段时,它需要大约3秒来加载数据并显示片段。

我想先显示片段,然后加载移动,在加载数据时显示进度条或类似的东西。

这是我的片段:

public class PokemonMovesFragment extends Fragment {


   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    databaseAccess = DatabaseAccess.getInstance(getContext());
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_pokemon_moves, container, false);
    this.view=view;
    initalizeAllComponents();
    initializePokemonData();
    initializePostDataPopulationComponents();
    getActivity().setTitle(getResources().getText(R.string.moves)+ " (" +selectedPokemon.getName() + ")");
    return view;
}
//The rest of the methods

共1个答案

匿名用户

只需将所有逻辑移动到onViewCreated中,然后在逻辑之前显示Progressbar,在逻辑之后隐藏Progressbar

@Override
public View onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState)
        progressBar.visibility = View.VISIBLE; // Show Progress bar here
        initalizeAllComponents();
        initializePokemonData();
        initializePostDataPopulationComponents();
        getActivity().setTitle(getResources().getText(R.string.moves)+ " (" +selectedPokemon.getName() + ")");
        progressBar.visibility = View.INVISIBLE; // Hide Progress bar here
}