将操作栏与导航抽屉一起移动
Move action bar along with Navigation Drawer
我已经按照 google 指南实施了导航栏,但我想将操作栏与抽屉一起移动。我什至提到了 How to slide the ActionBar along with the NavigationDrawer 但无法实现。
我想在不使用第三方库的情况下实现如下所示的功能:
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();
}
}
请帮我写一些片段。
将此添加到您的 Drawer
Callbacks
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
View container = findViewById(R.id.container);
container.setTranslationX(slideOffset * drawerView.getWidth() * -1);
}
使用 Android 提供的 SlidingPanelayout,而不是使用 Navigation Drawer。
https://developer.android.com/reference/android/support/v4/widget/SlidingPaneLayout.html
http://www.survivingwithandroid.com/2013/10/android-slidingpanelayout-tutorial-2.html
我已经按照 google 指南实施了导航栏,但我想将操作栏与抽屉一起移动。我什至提到了 How to slide the ActionBar along with the NavigationDrawer 但无法实现。
我想在不使用第三方库的情况下实现如下所示的功能:
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();
}
}
请帮我写一些片段。
将此添加到您的 Drawer
Callbacks
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
View container = findViewById(R.id.container);
container.setTranslationX(slideOffset * drawerView.getWidth() * -1);
}
使用 Android 提供的 SlidingPanelayout,而不是使用 Navigation Drawer。
https://developer.android.com/reference/android/support/v4/widget/SlidingPaneLayout.html
http://www.survivingwithandroid.com/2013/10/android-slidingpanelayout-tutorial-2.html