【问题标题】:Android selecting element from navigation bar freezes loadAndroid 从导航栏中选择元素会冻结加载
【发布时间】: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


【解决方案1】:

只需将所有逻辑移入onViewCreated,然后在逻辑之前显示进度条,在逻辑之后隐藏进度条

@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
}

【讨论】:

    猜你喜欢
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多