yavski / fab-speed-dial

FAB flinging out actionable menu items using the Speed dial metaphor
Apache License 2.0
477 stars 122 forks source link

How to hide activity's fab-speed-dial in child fragment? #53

Open lucasjinreal opened 7 years ago

lucasjinreal commented 7 years ago

I have a Main Activity which contains a fab-speed-dial, and I am using NavigationDrawer swich fragments. When I replace to another fragment, I want hide fab-speed-dial, I tried implements this in MainActivity:

 FabSpeedDial mainFab;

...
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_news_detail);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Hot");
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HotFragment()).commit();
        }

        //get the fab in main
        FabSpeedDial mainFab = (FabSpeedDial) findViewById(R.id.fab_main);
        if (mainFab == null){
            System.out.println("If main fab is null, this will show.");
        }

        //实现drawer布局
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //设置drawer里面的圆形图片,这貌似是添加原型图片最简单的办法
                ImageView imageViewPortrait = (ImageView) findViewById(R.id.navImageView);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.portrait);
                RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
                dr.setCircular(true);
                imageViewPortrait.setImageDrawable(dr);
            }
            @Override
            public void onDrawerOpened(View drawerView) {
            }
            @Override
            public void onDrawerClosed(View drawerView) {
            }
            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        //设置默认的选中nav item
        navigationView.setCheckedItem(R.id.nav_hot);
        navigationView.setNavigationItemSelectedListener(this);

    }
...
public void showFabMain(){
        mainFab.show();
    }
public void hideFabMain(){
        mainFab.hide();
    }

and in fragment I called hideFabMain( ) method:

//get father activity floating button set it too invisible
((MainActivity) getActivity()).hideFabMain();

However, I got a NULL pointer error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void io.github.yavski.fabspeeddial.FabSpeedDial.hide()' on a null object reference
                                                                          at com.ouman.luoliluoli.MainActivity.hideFabMain(MainActivity.java:163)
                                                                          at com.ouman.luoliluoli.AroundFragment.onCreateView(AroundFragment.java:48)
                                                                          at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                                                                          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                                                                          at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
                                                                          at android.os.Handler.handleCallback(Handler.java:751)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

So, I want get some help in my question, this really struggled me out, the great author can give me some advise?