2016-04-19 183 views
0

我有幾個活動需要使用同一個導航抽屜。下面是相關的抽屜我Activity類代碼的一部分:將工具欄與抽屜式導航欄連接

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
getSupportActionBar().setDisplayShowTitleEnabled(false); 

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
drawer.addDrawerListener(toggle); 
toggle.syncState(); 
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
navigationView.setNavigationItemSelectedListener(this); 

我打算做一個DrawerActivity類,並從中所需的導航抽屜所有這些活動擴展它。因此,我將不得不將所有抽屜代碼移動到該DrawerActivity。

問題:

toolbar是的ActionBarDrawerToggle初始化重要。每個活動都有不同的工具欄,具體取決於該活動的需求。因此,工具欄佈局必須保存在各自的佈局xml文件中。我如何在DrawerActivity類中使用工具欄?

的抽屜佈局的xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 


<FrameLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/frame_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

</FrameLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    app:itemBackground="@drawable/drawer_list_selector" 
    style="@style/NavigationDrawer" 
    app:headerLayout="@layout/sidenav_header" 
    app:menu="@menu/side_navigation_menu" 
    app:theme="@style/Drawer"/> 

</android.support.v4.widget.DrawerLayout> 

這裏的側導航欄活動(這我想那些誰需要導航抽屜類來擴展):pastebin.com/hheMXku5

的主要聲明日誌說:

引起:java.lang.NullPointerException:試圖調用虛擬方法'無效android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(布爾值)」上的空對象引用

這裏的鏈接到的onCreate延伸側導航欄類的活性類中的一種:pastebin.com/iKYXVbda

回答

0

okey,所以你想用活動的抽屜。我在這裏發佈我的代碼。我希望這會幫助你。

首先你需要開發NavigationDrawer。我知道你可以靠自己做到這一點。

現在創建子例子Home.java並將其與您的抽屜擴展活動讓我們假設MainActivity.java

它會是這樣的。

public class Home extends MainActivity{ 

    Toolbar toolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 
} 

現在通常我們執行onCreatesetContentView,鑽在這裏做這樣的事情。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getLayoutInflater().inflate(R.layout.home, frameLayout); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 
} 

frameLayout將是你的容器,它必須採取MainActivity

注意:不要忘記在家裏活動另一個ToolBar。因爲它會覆蓋MainActivity的工具欄。

編輯 我張貼我的整個代碼請參考它。儘管我帶走了自定義導航抽屜,但它也可以與NavigationView一起使用。

activity_main

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" > 

<RelativeLayout 
    android:id="@+id/frame_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="false" > 

    <!-- ToolBar of MainActivity --> 

    <include 
     android:id="@+id/toolbar_headers" 
     layout="@layout/toolbar_header" /> 
</RelativeLayout> 

<!-- here will be your NavigationView --> 

<com.random.extraclasses.ScrimInsetsFrameLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:layout_marginRight="-8dp" 
    android:elevation="8dp" 
    android:fitsSystemWindows="true" 
    app:insetForeground="#4000" > 

    <include 
     android:id="@+id/drawar_layout" 
     layout="@layout/drawar_layout" /> 
</com.gujarat.property.extraclasses.ScrimInsetsFrameLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity implements OnClickListener { 

    protected RelativeLayout frameLayout; 
    protected DrawerLayout mDrawerLayout; 
    protected ListView mDrawerList; 
    protected ScrimInsetsFrameLayout mLayout; 
    protected ActionBarDrawerToggle mDrawerToggle; 
    protected static boolean isLaunch = true; 

    Toolbar toolbar; 

    private CharSequence mTitle; 

    private String[] navMenuTitles; 
    private TypedArray navMenuIcons; 

    private ArrayList<NavDrawerItem> navDrawerItems; 
    private NavDrawerListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

     navMenuIcons = getResources() 
      .obtainTypedArray(R.array.nav_drawer_icons); 

     frameLayout = (RelativeLayout) findViewById(R.id.frame_container); 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.list); 
     mLayout = (ScrimInsetsFrameLayout) findViewById(R.id.container); 

     toolbar = (Toolbar) findViewById(R.id.toolbar_headers); 

     setSupportActionBar(toolbar); 

     navDrawerItems = new ArrayList<NavDrawerItem>(); 

     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons 
      .getResourceId(0, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons 
      .getResourceId(1, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons 
      .getResourceId(2, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons 
      .getResourceId(3, -1))); 

     navMenuIcons.recycle(); 

     adapter = new NavDrawerListAdapter(getApplicationContext(), 
      navDrawerItems); 
     mDrawerList.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       selectedPos = position - 1; 
       displayView(position); 
      } 
     }); 

     mDrawerList.setAdapter(adapter); 
     mDrawerList.setFocusable(false); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.ic_drawer, R.string.app_name) { 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (isLaunch) { 
      isLaunch = false; 
      displayView(0); 
     } 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    private void displayView(int position) { 

     mDrawerLayout.closeDrawer(mLayout); 
     MainActivity.position = position; 
     switch (position) { 

     case 0: 
      if (this.getClass().getSimpleName().equals("Home")) { 
      } else { 
       Intent i = new Intent(this, Home.class); 
       startActivity(i); 
       overridePendingTransition(R.anim.fadein, R.anim.fadeout); 
      } 
      break; 
     case 1: 
      if (this.getClass().getSimpleName().equals("ForSale")) { 
      } else { 
       Intent i = new Intent(this, ForSale.class); 
       startActivity(i); 
       overridePendingTransition(R.anim.fadein, R.anim.fadeout); 
      } 
      break; 
     case 2: 
       if (this.getClass().getSimpleName().equals("ForRent")) { 
       } else { 
       Intent i = new Intent(this, ForRent.class); 
       startActivity(i); 
       overridePendingTransition(R.anim.fadein, R.anim.fadeout); 
       } 
       break; 
     case 3: 
      if (this.getClass().getSimpleName().equals("NewProjects")) { 
      } else { 
       Intent i = new Intent(this, NewProjects.class); 
       startActivity(i); 
       overridePendingTransition(R.anim.fadein, R.anim.fadeout); 
      } 
      break; 
      default: 
      break; 
     } 
    } 

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

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public void onClick(View v) { 

    } 
} 

最後Home.java

public class Home extends MainActivity implements OnClickListener { 

Toolbar toolbar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getLayoutInflater().inflate(R.layout.home, frameLayout); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setTitle(""); 
} 

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    if (mDrawerLayout.isDrawerOpen(mLayout)) { 
     isLaunch = false; 
     mDrawerLayout.closeDrawer(mLayout); 
    } else { 
     finishAffinity(); 
    } 
} 

而且僅作參考home.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/toolbar" 
     android:background="@drawable/back51" > 

     <TextView 
      android:id="@+id/etSearch" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:background="@android:color/transparent" 
      android:text="Search Property" 
      android:textColor="#666666" /> 
    </RelativeLayout> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_scrollFlags="scroll|enterAlways" 
     android:background="@color/toobar" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" > 
    </android.support.v7.widget.Toolbar> 

</RelativeLayout> 

我希望這將幫助你。

+0

因此,我在主活動xml文件中聲明瞭FrameLayout容器,並用home佈局誇大了它,但我得到了NPE。我已經更新了問題中主要活動的佈局。任何想法爲什麼會發生? –

+0

getSupportActionBar()。獲取空指針異常setDisplayHomeAsUpEnabled(true);' –

+0

你在主頁佈局中使用了另一個工具欄嗎? –