我已经成功确定要在汉堡包和箭头之间更改NavDrawer中的图标,需要切换此代码:
mDrawerToggle.syncState();
然而,当我点击后退按钮时,我确实想要返回上一个活动时仍然打开导航抽屉。
我知道我可以简单地设置一个onClickListener,但我认为Android有更原生的方式导航到上一个屏幕。这是我的代码:'
的onCreate:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
toolbar, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
isOpen = false;
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
isOpen = true;
}
};
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
ArrayList<String> drawerTitleArray = new ArrayList<>();
drawerTitleArray.add(0, "TEST");
drawerTitleArray.add(1, "TEST 1");
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, drawerTitleArray));
// TODO: Add Fragment Code to check if savedInstanceState == null; add at Activity Level?
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
//set up viewpager for current day
//mPager = (ViewPager) findViewById(R.id.home_polls_viewpager_fragment_container);
//mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
//mPager.setAdapter(mPagerAdapter);
//get current date to apply to Viewpager
mDateFormat = new SimpleDateFormat("MM-dd-yyyy");
mDate = new Date();
mCurrentDateString = mDateFormat.format(mDate);
mViewPager = (ViewPager) findViewById(R.id.poll_fragment_container);
mViewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
mTabLayout.setupWithViewPager(mViewPager);
// TODO: Checkn if AuthStateListenerNecessary
//Determine whether necessary to use an AuthStateListener here
// mUserRef.addAuthStateListener(new Firebase.AuthStateListener() {
//@Override
//public void onAuthStateChanged(AuthData authData) {
//if (authData == null) {
//Intent backToSignIn = new Intent(getApplication(), SignupActivity.class);
//startActivity(backToSignIn);
// finish();
// }
// }
// })
}
@Override
protected void onStart() {
super.onStart();
v = new ValueEventListener() {
//testing methodology of adding children
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int pollsAvailable = (int) dataSnapshot.child(mCurrentDateString).getChildrenCount();
// mPagerAdapter.setPollsAvailable(pollsAvailable);
Log.i("TAG", "There are " + String.valueOf(pollsAvailable) + " children in today's poll count.");
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
};
mPollsRef.addValueEventListener(v);
}
@Override
protected void onStop() {
super.onStop();
mPollsRef.removeEventListener(v);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId() == android.R.id.home){
super.onBackPressed();
return true;}
else
return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//TODO: Be sure to only allow one instance of each activity
//TODO:Address mDrawerToggle code from StackOverflow to make sure I am correctly implementing the return to previous activity
// Sync the toggle state after onRestoreInstanceState has occurred.
// mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
int pollIndex = mIntentFromTouch.getIntExtra("Poll_Index",0);
switch (position) {
case 0:
PollFragment pollFragment = PollFragment.newInstance(pollIndex);
return pollFragment;
case 1:
DiscussionFragment discussionFragment = DiscussionFragment.newInstance(pollIndex);
return discussionFragment;
default:
return new PollFragment();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getResources().getString(R.string.poll_text);
case 1:
return getResources().getString(R.string.discussion_text);
default:
return getResources().getString(R.string.poll_text);
}
}
}
}
答案 0 :(得分:2)
实际上,syncState()
方法可确保切换图像与抽屉处于匹配状态,并在Toolbar
/ ActionBar
上设置图像。它不会切换抽屉/后退功能。
您正在寻找setDrawerIndicatorEnabled()
方法。启用指示器后,切换操作抽屉。禁用后,如果您设置了导航OnClickListener
,则会返回导航mDrawerToggle.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
mDrawerToggle.setToolbarNavigationClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
}
);
。
要处理后退功能,请将以下内容添加到切换设置中:
mDrawerToggle.setDrawerIndicatorEnabled(false);
然后,当您想要禁用抽屉操作,并允许单击切换回去时,只需致电:
ActionBarDrawerToggle
我还要指出,由于您使用了Toolbar
参数的Activity
构造函数,因此不会调用onOptionsItemSelected()
的{{1}}方法点击切换时,那里不需要mDrawerToggle.onOptionsItemSelected(item)
来电。
注意:根据您使用的支持库版本,后箭头Drawable
资源可能会被命名为R.drawable.abc_ic_ab_back_material
。有关详细信息,请参阅this post。