简体   繁体   English

如何通过导航抽屉的打开和关闭来更改按钮图像

[英]How do I change my button image with navigation drawer opening and closing

I have a navigation button for my navigation fragment which turns active and opens a navigation drawer menu upon click: 我的导航片段有一个导航按钮,该按钮将变为活动状态,并在单击时打开导航抽屉菜单: 在此处输入图片说明

Now when I click it, it turns active as follows: 现在,当我单击它时,它变为活动状态,如下所示: 在此处输入图片说明

However, I want to associate it with a navigation drawer such a way that, even if I do not click the button and slide open the navigation drawer, the button turns active when the navigation drawer menu is open and when closed by sliding back in from right to left, the button turns red/inactive. 但是,我希望将其与导航抽屉相关联,即使我不单击按钮并滑动打开导航抽屉,当打开导航抽屉菜单以及通过从中向后滑入而关闭时,按钮也会变为活动状态。从右到左,按钮变为红色/无效。 The code which I am trying to work with is as follows: 我尝试使用的代码如下:

private boolean mIsNavigationOpen = false;
    private DrawerLayout drawerLayout;
    private NavigationPanelFragment dlDrawer;
    private ActionBarDrawerToggle actionBarDrawerToggle;
public boolean isNavigationOpen() {

        return mIsNavigationOpen;

    }
//----------Code for Navigation open logo button active/inactive instances
    @SuppressWarnings("deprecation")
    public void setNavigationOpen(final boolean isNavigationOpen) {
        this.mIsNavigationOpen = isNavigationOpen;
        final ImageButton mainButton = (ImageButton) findViewById(R.id.button_main);
        if(isNavigationOpen) {
            mainButton.setBackgroundResource(R.drawable.bg_helios_active);
        } else {
            mainButton.setBackgroundDrawable(null);
        }
@Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        //----------Code for Navigation Drawer setup
        // 2. App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        // 2.1 create ActionBarDrawerToggle
                 actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
                            R.drawable.arrow_up, R.string.drawer_open, R.string.drawer_close){

                        /** Called when a drawer has settled in a completely closed state. */
                        public void onDrawerClosed(View view) {
                           // getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
                           // invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                        }

                        /** Called when a drawer has settled in a completely open state. */
                        public void onDrawerOpened(View drawerView) {
                         //   getActionBar().setTitle(NavigationPanelFragment.activeFragmentTitle);
                          //  invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                        }
                    };


                // 2.2 Set actionBarDrawerToggle as the DrawerListener
                drawerLayout.setDrawerListener(actionBarDrawerToggle);
          }
@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }
private void setupOnClickListenerForMainButton() {
        final ImageButton mainButton = (ImageButton) findViewById(R.id.button_main);
        mainButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {
                toggleNavigationPanel();


            }
        });
    }
@Override
    public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                toggleNavigationPanel();
                break;

            default:
                break;
        }
        return super.onMenuItemSelected(featureId, item);
    }
public void onNewsClicked(final View view) {
        if(mIsNavigationOpen) {
            toggleNavigationPanel();
        }

        if (isFragmentVisible(NewsFragment.TAG_NEWS_FRAGMENT)) {
            return;
        }

        FragmentStackManager.getInstance().clearBackStack(getSupportFragmentManager());
        mActiveFragment = NewsFragment.newInstance(getSupportFragmentManager());
        updateActionBarTitle();
        //For swipe action close drawer on button click
         drawerLayout.closeDrawer(R.id.drawer);
    }
public void onListsClicked(final View view) {
        if(mIsNavigationOpen) {
            toggleNavigationPanel();
        }

        if (isFragmentVisible(ListsContainerFragment.TAG_LIST_CONTAINER_FRAGMENT)) {
            return;
        }

        FragmentStackManager.getInstance().clearBackStack(getSupportFragmentManager());
        mActiveFragment = ListsContainerFragment.newInstance(getSupportFragmentManager());
        updateActionBarTitle();
        //For swipe action close drawer on button click
        drawerLayout.closeDrawer(R.id.drawer);
    }
private void toggleNavigationPanel() {
        //final FragmentStackManager manager = FragmentStackManager.getInstance();
        if (mIsNavigationOpen) {
            //NavigationPanelFragment.removeInstance(getSupportFragmentManager());
            updateActionBarTitle();
            drawerLayout.closeDrawer(R.id.drawer);
        } else {
            drawerLayout.openDrawer(R.id.drawer);
            final TextView title = (TextView) findViewById(R.id.main_title);
            title.setText(getString(R.string.title_applications));
            //NavigationPanelFragment.newInstance(getSupportFragmentManager(), manager.getTopTitle());
        }


        setNavigationOpen(!mIsNavigationOpen);
    }

You might want to concentrate on the main_button and togglenavigationpanel. 您可能需要专注于main_button和togglenavigationpanel。 I added the condition if(drawerlayout.isdraweropen(R.id.drawer)){closedrawerlayout...} but it didn't do the trick. 我添加了条件if(drawerlayout.isdraweropen(R.id.drawer)){closedrawerlayout ...},但没有成功。 I was wondering if anyone has any idea regarding the same? 我想知道是否有人对此有任何想法?

Thanks! 谢谢!

When you've set up the drawer a listener has been added which has callbacks for when the drawer is in either final state, so what you need to do is reset the drawable inside these callbacks. 设置抽屉后,将添加一个侦听器,该监听器具有抽屉处于两种最终状态时的回调,因此您需要做的是在这些回调中重置drawable。 I've shown what I think your final code should look like, minus some cleaning up you need to do, hope it helps 我已经展示了我认为您的最终代码应该是什么样子,减去了您需要做的一些整理工作,希望对您有所帮助

private DrawerLayout drawerLayout;
private NavigationPanelFragment dlDrawer;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ImageButton mDrawerButton; //Store it! - findViewById is *Expensive*
private TextView mTitle;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    mTitle = (TextView) findViewById(R.id.main_title);
    mDrawerButton = (ImageButton) findViewById(R.id.button_main);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.arrow_up, R.string.drawer_open, R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            mDrawerButton.setImageResource(R.drawable.bg_helios_inactive);
        }

        public void onDrawerOpened(View drawerView) {
            mDrawerButton.setImageResource(R.drawable.bg_helios_active);
        }

    };
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    setupOnClickListenerForMainButton(;
}

@Override //Note: I'd imagine this should be in onResume ...
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarDrawerToggle.syncState();
}

private void setupOnClickListenerForMainButton() {
    mDrawerButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            toggleNavigationPanel();
        }
    });
}

...

private void toggleNavigationPanel() {
    //if ( drawerLayout.isOpen() ) { //I dont know what methods these objects have
    if ( dlDrawer.isOpen() ) { //but one of these must be sensibly storing its own state
        updateActionBarTitle();
        drawerLayout.closeDrawer(R.id.drawer);
    } else {
        drawerLayout.openDrawer(R.id.drawer);
        mTitle.setText(getString(R.string.title_applications));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM