使用新工具栏组件,隐藏和显示导航抽屉图标,Home Up图标单击事件片段中的问题

时间:2014-12-03 11:33:52

标签: android android-fragments navigation-drawer android-navigation android-toolbar

我最近开始更新我的应用,以使用带有导航抽屉的Android 5.0中引入的新工具栏组件。应用程序的流程是:MainActivity有一个工具栏,带菜单的导航抽屉1.主页2.购物车。

主菜单通过其中的按钮导航到“主页详细信息”。

“MainActivity”的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <FrameLayout android:id="@+id/activity_frame" 
         android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:listSelector="@android:color/transparent" />
</android.support.v4.widget.DrawerLayout>

我在从抽屉中选择的菜单上的“MainActivity”中添加两个片段HomeFragment,CartFragment,默认选择是位置0,即“Home”

class“MainActivity.java”::

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTitle = mDrawerTitle = getTitle();     
    drawer = (DrawerLayout) findViewById(R.id.drawer);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    tvTitle = (TextView) mToolbar.findViewById(R.id.toolbar_title);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);          
    }       
    mDrawerToggle = new ActionBarDrawerToggle(this, drawer,mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
        }
    };
    drawer.setDrawerListener(mDrawerToggle);        
    mDrawerList = (ListView) findViewById(R.id.left_drawer);    
    dataList = new ArrayList<DrawerItem>();
    dataList.add(new DrawerItem(Constants.V_HOME, R.drawable.ic_launcher));
    dataList.add(new DrawerItem(Constants.V_MY_CART, R.drawable.ic_launcher));  
    mDrawerList.setAdapter(adapterDrawer);      
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());      
    // to show first "Home fragment" on start up of application
    if(savedInstanceState==null)
        SelectItem(0);
}
public void SelectItem(int position) {
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        replaceFragment(fragment, Constants.V_TAG_HOME,false,"Home");
        break;
    case 1:
        fragment = new CartFragment();
        replaceFragment(fragment, Constants.V_TAG_MY_CART,false,"My Cart");
        break;
    }           
    drawer.closeDrawer(mDrawerList);
}
public void replaceFragment(Fragment fragment,String fragment_tag,boolean showHome,String title){   
    frgManager = this.getSupportFragmentManager();
    FragmentTransaction ft  = frgManager.beginTransaction();
    ft.replace(R.id.activity_frame,fragment,fragment_tag);      
    if(showHome)
        ft.addToBackStack(null);
    ft.commit();            
    shouldDisplayHomeUp(showHome);
}
public void shouldDisplayHomeUp(boolean showHome){
    if(showHome){
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        getSupportActionBar().setHomeButtonEnabled(true);   
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }else{
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {         
    getMenuInflater().inflate(R.menu.main, menu);
      return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {   
    switch (item.getItemId()) 
       {        
          case android.R.id.home:            
               Toast.makeText(getApplicationContext(),"main act  clicked", Toast.LENGTH_SHORT).show();      
             return false;        
          default:            
             return super.onOptionsItemSelected(item);    
       }
}
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub      
    if(frgManager.getBackStackEntryCount()>0){      
           getSupportFragmentManager().popBackStack();      
           // show the drawer icon when on moving back 
           shouldDisplayHomeUp(false);         
        }else{
            super.onBackPressed();
        }
    mDrawerToggle.syncState();
}

我的主页片段点击按钮有“详细信息”按钮,替换为其他片段:“HomeDetailFragment”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
    android:id="@+id/btnDetail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Home detail" />
<FrameLayout
    android:id="@+id/home_frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

class“HomeFragment.java”btnDetail click事件替换为其他片段“HomeDetailFragment”::

btnDetail.setOnClickListener(new View.OnClickListener() {           
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Fragment fragment = new HomeDetailFragment();
            ((MainActivity)getActivity()).showDrawerIndicator(false);
            FragmentManager frManager = getActivity().getSupportFragmentManager();
            FragmentTransaction ft  = frManager.beginTransaction();
            ft.addToBackStack(null);
            ft.replace(R.id.activity_frame, fragment).commit();
        }
});

class“HomeDetailFragment.java”::

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);    
    // update the actionbar to show the up carat/affordance 
    ((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {       
    switch (item.getItemId()) {
    case android.R.id.home:    
            // your code for order here
        Toast.makeText(getActivity(), "home detail", Toast.LENGTH_LONG).show(); 
        ((MainActivity)getActivity()).onBackPressed();          
     return true;
    }
    return true;
}

我的问题::

1。在“HomeDetailFragment”中,我可以看到“UP主页图标”但无法获取主页图标onOptionsItemSelected的点击事件未被调用,因此无法导航回HomeFragment

2。按下电话后退按钮,再次导航到“主页详细信息”时,它没有显示“向上主页图标”

请指导我。

3 个答案:

答案 0 :(得分:3)

  
      
  1. 在&#34; HomeDetailFragment&#34;中,我可以看到&#34; UP主页图标&#34;但是无法调用onOptionsItemSelected主页图标的click事件,因此无法导航回HomeFragment
  2.   

实现此目的的一种方法是使用Toolbar中的MainActivity,如下所示:

Toolbar mToolbar= ((MainActivity)getActivity()).mToolbar;
mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_nav_back));
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("MrE", "home selected");
            }
        });

  
      
  1. 按下电话后退按钮,然后再次导航到&#34; Home Detail&#34;它没有显示&#34; UP主页图标&#34;
  2.   

您的代码未被调用的原因是因为它位于片段的onCreate中。将其移至onCreateOptionsMenu而不是更新。

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Put toolbar stuff from above here
    super.onCreateOptionsMenu(menu, inflater);
}

答案 1 :(得分:2)

我有确切的问题。

使用 setToolbarNavigationClickListener 方法。

在MainActivity.java中,

actionBarToggle.setToolbarNavigationClickListener(new View.OnClickListener({
        @Override
        public void onClick(View v) {
            MainActivity.this.onSupportNavigateUp();
        }
    });

避免在Fragments中单独实现Up Navigation。

答案 2 :(得分:0)

  1. 您调用getSupportActionBar().setDisplayHomeAsUpEnabled(true),显示工具栏中的按钮但未激活它。 另外调用getSupportActionBar().setHomeButtonEnabled(true);应该可以胜任。

  2. 您可以在片段中显示工具栏中的后退按钮,这是我要避免的。当你执行FragmentTransaction时,尝试将此部分移动到Activity。