【发布时间】:2020-10-24 05:51:30
【问题描述】:
我有一个底部导航栏,其中包含多个元素,可以将我带到不同的片段。
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 片段外,所有片段都可以正常工作,当我按下它时,加载数据并显示片段大约需要 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
【问题讨论】:
-
将您的逻辑移到
onViewCreated(...)中,这可能会有所帮助。此外,在后台加载数据并在可用时绑定到 UI -
工作就像一个魅力,谢谢!
标签: android performance android-studio android-fragments android-progressbar