我已经成功确定,在NavDrawer中更改汉堡和箭头图标,需要切换此代码:
mDrawerToggle.syncState();
然而,当我点击返回按钮时,它仍然打开导航抽屉菜单,实际上我想返回到之前的活动。我知道我可以简单地设置一个onClick监听器,但我认为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);
}
}
}
}
R.drawable.abc_ic_ab_back_material
。请参考这篇帖子。我马上会在这里修改我的答案。 - Mike M.ActionBarDrawerToggle
,因为您立即禁用了指示器。这就是我在之前的评论中询问的原因。ActionBarDrawerToggle
的特定用途是控制抽屉。如果您只想通过拖动来使用抽屉,则不需要切换开关。您可以直接设置该按钮返回。 - Mike M.