在可见但不重新创建时执行片段

问题描述 投票:0回答:2

我有这个问题:

目前我使用BottomNavigationView来浏览片段......

码:

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

               //Fragment fragment = null;
               Fragment currentFragment = fragmentManager.findFragmentById(R.id.fragment_container_home);
                switch (item.getItemId()){

                    case R.id.ic_home:

                        if (currentFragment instanceof MainFragment){

                            //execute code
                        }

                        if(fragmentManager.findFragmentByTag("mainMenu") != null) {
                            //if the fragment exists, show it.
                            fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit();
                            toolbar.setTitle("Flip");
                            item = menu.getItem(0);
                            item.setChecked(true);
                        } else {
                            //if the fragment does not exist, add it to fragment manager.
                            fragmentManager.beginTransaction().add(R.id.fragment_container_home, new MainFragment(), "mainMenu").commit();
                            toolbar.setTitle("Flip");
                            item = menu.getItem(0);
                            item.setChecked(true);
                        }

                        //Hide Others Fragments
                        if(fragmentManager.findFragmentByTag("categories") != null){
                            fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("categories")).commit();
                        }
                        if(fragmentManager.findFragmentByTag("feedUsers") != null){
                            fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("feedUsers")).commit();
                        }
                        if(fragmentManager.findFragmentByTag("explore") != null){
                            fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("explore")).commit();
                        }
                        if(fragmentManager.findFragmentByTag("opcoes") != null){
                            fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("opcoes")).commit();
                        }

                        break;


                }

                return false;

            }
        });

我需要的:

单击时运行一些代码,可见片段是菜单片段本身。

在这:

if (currentFragment instanceof MainFragment){

                            //execute code
                        }

只有当片段重新出现而没有重新创建时,我才能执行一些代码吗?

另一种方法是访问recyclerview或发送命令以在片段中执行

我需要访问RecyclerView Fragment,以便在BottomNavigationView中点击两次时将其发送到屏幕顶部。

android android-fragments bottomnavigationview
2个回答
0
投票

我想你的方法不止一种

第一种方式,在显示片段后调用所需的代码,非常简单

if(fragmentManager.findFragmentByTag("mainMenu") != null) {
                            //if the fragment exists, show it.
                            fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit();
                            toolbar.setTitle("Flip");
                            item = menu.getItem(0);
                            item.setChecked(true);


                            ////////////////////////////////////
                            // execute the code you want here //
                            ////////////////////////////////////
                        }

第二种方法你可以在显示/隐藏它之后设置qazxsw poi的qazxsw poi并提前检查visibility

fragment显示添加visibility fragment

setVisibility

然后VISIBLE隐藏添加fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit(); currentFragment.getView().setVisibility(View.VISIBLE); fragment

setVisibility

并检查GONE fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("categories")).commit(); currentFragment.getView().setVisibility(View.GONE); 所需的任何地方

Visibility

如果你不能检查它并且没有触发器来检查fragment你可以制作一个if(currentFragment != null){ if(currentFragment().getView().getVisibility() == View.VISIBLE){ // fragment is shown, do some action }else { // fragment is hiden, do some action } } ,每1秒检查一次visibility或任何你想要的....

第三种方式,效率很高。使用RunnableVisibility仅在onResume未运行时才被调用,除非您手动调用它。

onResume

然后

fragment

第三种方式的一些费用;

只有当你的片段在运行时没有包含值且你想保留这些值时才使用这种方式,并且不要在运行时使用这些值,除非它们是if(fragmentManager.findFragmentByTag("mainMenu") != null) { //if the fragment exists, show it. fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("mainMenu")).commit(); toolbar.setTitle("Flip"); item = menu.getItem(0); item.setChecked(true); currentFragment().onResume(); } //Hide Others Fragments if(fragmentManager.findFragmentByTag("categories") != null){ fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("categories")).commit(); currentFragment().onStop(); } EXP;如果你在运行时从@Override public void onResume() { super.onResume(); // your code } 加载static到某个imageView并且你隐藏了bitmap(正如你在代码中所做的那样)并且在你再次显示某些fragment之后你会发现fragment,因为fragmentimageView中运行,在另一个手,如果你打电话终止fragment并再次启动它,你将找不到位图或background。所以这是使用fragment方法。它停止了一切都在ImageViewonStopfragment)并保存,直到bitmap RESUME没有开始。


0
投票

你可以为此覆盖imageView

fragment
© www.soinside.com 2019 - 2024. All rights reserved.