移动操作栏和导航抽屉

时间:2016-11-30 11:15:55

标签: android navigation-drawer drawerlayout android-navigation-drawer android-actionbaractivity

我已根据google guildlines实现了导航栏,但我想移动操作栏和抽屉。我甚至提到了以下How to slide the ActionBar along with the NavigationDrawer但未能实现它。

我希望在不使用第三方lib的情况下实现类似下面的内容:

enter image description here

DrawerActivity:

public  abstract class BarcodeBaseActivity extends ActionBarActivity {

 @Override
    protected void onCreate(Bundle savedInstanceState) {

 setSupportActionBar(toolbar);
//        setTypface();

        mTitle = getTitle();
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open,
                R.string.navigation_drawer_close) {
            public void onDrawerClosed(View view) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                supportInvalidateOptionsMenu();
                history.setSelected(false);
                help.setSelected(false);
                unipoints.setSelected(false);
                promotions.setSelected(false);
                notifications.setSelected(false);
                info.setSelected(false);
                contact.setSelected(false);
                signout.setSelected(false);
                drawerOpened = false;
            }

            public void onDrawerOpened(View drawerView) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                supportInvalidateOptionsMenu();
                getSupportActionBar().setTitle(mTitle);
                history.setSelected(true);
                help.setSelected(true);
                unipoints.setSelected(true);
                promotions.setSelected(true);
                notifications.setSelected(true);
                info.setSelected(true);
                contact.setSelected(true);
                signout.setSelected(true);
                drawerOpened = true;
            }

            public void onDrawerSlide(View drawerView, float slideOffset) {
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                supportInvalidateOptionsMenu();
            }
        };
        drawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        mDrawerToggle.setHomeAsUpIndicator(R.drawable.burger);
        ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float slideOffset = (Float) valueAnimator.getAnimatedValue();
                mDrawerToggle.onDrawerSlide(drawerLayout, slideOffset);
            }
        });
        anim.setInterpolator(new DecelerateInterpolator());

// You can change this duration to more closely match that of the default animation.
        anim.setDuration(500);
        anim.start();
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.LEFT);
            }
        });

        drawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
//        drawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

        drawerList = (ListView) findViewById(R.id.drawer_list);
        View list_header = getLayoutInflater().inflate(R.layout.drawerlist_header, null);
  drawerItems = getDrawerItems(pos);
        drawerList.setAdapter(new DrawerListAdapter(this, getLayoutInflater(), drawerItems));
        drawerList.setOnItemClickListener(new DrawerItemClickListener());


        if (drawerOpened) {
            history.setSelected(false);
            help.setSelected(false);
            unipoints.setSelected(false);
            promotions.setSelected(false);
            notifications.setSelected(false);
            info.setSelected(false);
            contact.setSelected(false);
            signout.setSelected(false);
        } else {
            history.setSelected(true);
            help.setSelected(true);
            unipoints.setSelected(true);
            promotions.setSelected(true);
            notifications.setSelected(true);
            info.setSelected(true);
            contact.setSelected(true);
            signout.setSelected(true);
        }

        setTitle(mTitle);
    }

  @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    }


 private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d(BarcodeBaseActivity.class.getSimpleName(), "Drawer item clicked at position " + position);

            /*for (DrawerItem drawerItem : drawerItems) {
                drawerItem.setSelected(false);
            }

            DrawerItem drawerItem = drawerItems.get(position - 1);
            drawerItem.setSelected(true);

            HeaderViewListAdapter headerViewListAdapter = (HeaderViewListAdapter) drawerList.getAdapter();
            DrawerListAdapter drawerListAdapter = (DrawerListAdapter) headerViewListAdapter.getWrappedAdapter();
            drawerListAdapter.notifyDataSetChanged();
*/
            selectItem(position);
            for (DrawerItem drawerItem : drawerItems) {
                drawerItem.setSelected(false);
            }
            if (position > 0) {
                DrawerItem drawerItem = drawerItems.get(position - 1);
                drawerItem.setSelected(true);

            }
            updateDrawer();
            SharedPreferences.Editor sharedPreferences = getSharedPreferences(AppConstants.PREFERENCE_SHARED_PREF_NAME, Context.MODE_PRIVATE).edit();
            sharedPreferences.putInt(AppConstants.PREFERENCE_LAST_DRAWERITEM_CLICKED, position - 1);
            sharedPreferences.commit();
        }
    }

请帮我一些片段。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:-1)

将此添加到您的Drawer Callbacks

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
    View container = findViewById(R.id.container);
    container.setTranslationX(slideOffset * drawerView.getWidth() * -1);
}