2016-06-07 42 views
0

我目前正在開發一個使用工具欄和DrawerLayout的android應用程序。主要內容是一個自定義的SurfaceView,我可以在其中繪製不同的形狀,文本......左側菜單是一個NavigationView並用作工具箱(我選擇我想從左側繪製的圖形,並在SurfaceView上繪製它)。除了一件事情之外,一切工作都正常:當我第一次嘗試打開左側菜單時(通過單擊工具欄或從屏幕左側滑動),項目不可見。雖然我沒有點擊任何物品(不可見),但它們保持隱形狀態。只有當我點擊一個不可見的項目時,問題才得以解決,之後菜單正常工作。我使用的是自定義主題,以隱藏狀態欄,並刪除默認的動作條:Android - DrawerLayout和工具欄在第一次打開時不可見

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar"></style> 

<style name="ColladiaTheme" parent="AppBaseTheme"> 

    <!-- Remove action bar --> 
    <item name="android:windowActionBar">false</item> 
    <item name="android:windowNoTitle">true</item> 

    <!-- Remove status bar --> 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowContentOverlay">@null</item> 

    <!-- Material theme --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 

</style> 

下面是一些截圖:

Menu open but not visible

Menu visible after clicking an item

<?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" > 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <com.ia04nf28.colladia.DrawColladiaView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/draw_view" /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_height="?attr/actionBarSize" 
      android:layout_width="?attr/actionBarSize" 
      android:minHeight="?attr/actionBarSize" 
      android:background="?attr/colorPrimary"> 
     </android.support.v7.widget.Toolbar> 

    </FrameLayout> 

    <!-- Left navigation menu --> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="?attr/actionBarSize" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?attr/actionBarSize" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="false" 
     app:menu="@menu/nav_view_items" > 
    </android.support.design.widget.NavigationView> 

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

這裏是活動代碼:

public class DrawActivity extends AppCompatActivity { 
private static final String TAG = "DrawActivity"; 

private DrawerLayout drawer; 
ActionBarDrawerToggle drawerToggle; 
private NavigationView nav; 
private DrawColladiaView colladiaView; 

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

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

    colladiaView = (DrawColladiaView) findViewById(R.id.draw_view); 
    colladiaView.setApplicationCtx(getApplicationContext()); 

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    // Add burger button 
    drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.addDrawerListener(drawerToggle); 
    // Removes overlay 
    drawer.setScrimColor(Color.TRANSPARENT); 
    drawer.closeDrawers(); 

    nav = (NavigationView) findViewById(R.id.nav_view); 

    setUpDrawerContent(nav); 
} 

public void setUpDrawerContent(NavigationView nav) 
{ 
    nav.setNavigationItemSelectedListener(
      new NavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(MenuItem item) { 
        selectDrawerItem(item); 
        return true; 
       } 
      } 
    ); 
} 

public void selectDrawerItem(MenuItem item) 
{ 
    switch(item.getItemId()) 
    { 
     case R.id.nav_home: 
      Intent intent = new Intent(getApplicationContext(), LoginActivity.class); 
      startActivity(intent); 
      break; 

     default: 
      Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString()); 

      if (newElement != null) colladiaView.insertNewElement(newElement); 

      drawer.closeDrawers(); 
      break; 
    } 
} 

@Override 
public void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    drawerToggle.syncState(); 
} 

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

@Override 
public void onBackPressed() { 
    Log.d(TAG, "onBackPressed"); 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

}

+0

沒有跳出以我爲你的XML不正確,但也可能是跟你的活動代碼。 – Bryan

+0

@Bryan,我編輯了我的問題並添加了我的活動代碼 – Bash

回答

0

看起來你應該重寫onOptionsItemSelected,而不是onBackPressed

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    // This will toggle the drawer if android.R.id.home is clicked 
    if(drawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 

    // Handle any other menu item selections... 

    return super.onOptionsItemSelected; 
} 

說實話,我沒有看到你的抽屜是不斷被打開的位置。它應該仍然響應從左邊的滑動。當點擊android.R.id.home後退/菜單按鈕)時,此代碼會將其設置爲切換。

+0

不幸的是,這也不起作用。我已經嘗試了一些其他設備和Android模擬器,它看起來好像在它們上工作得很好。我想這與我的手機有關... – Bash

+0

@Bash啊,他們是否運行不同版本的Android?舊版本可能存在錯誤。 – Bryan

+0

我的手機在Android 5.0(Lollipop)和Android 4.1.1的模擬器中運行。它的工作在模擬器上而不是我的手機上 – Bash

0

好的,問題出在setUpDrawerContent和selectDrawerItem中。 所以,你基本上想要設置按鈕,但是你需要添加一個navigationItemSelectedListener。 發生這種情況: 您準備好所有內容,然後添加偵聽器。直到您單擊導航器抽屜中的項目纔會發生任何事情。當你點擊某個東西時,點擊偵聽器會觸發,並且抽屜項目被創建。

您嘗試創建動態元素,但只有當您按下不可見按鈕時纔會觸發此創建。

嘗試移動從交換機的默認情況下,代碼selectDrawerItem到setUpDrawerContent,並將其放置在你面前的幾行設置navigationItemSelectedListener。您還需要在那裏創建所有按鈕,使用for循環或其他內容來預先創建菜單項。

像這樣:

public void setUpDrawerContent(NavigationView nav) 
{ 
    /* 
    //for loop here to add your menu items 
    Element newElement = ElementFactory.createElement(getApplicationContext(), item.getTitle().toString()); 

    if (newElement != null) 
     colladiaView.insertNewElement(newElement); 
    */ 
    nav.setNavigationItemSelectedListener(
      new NavigationView.OnNavigationItemSelectedListener() { 
       @Override 
       public boolean onNavigationItemSelected(MenuItem item) { 
        selectDrawerItem(item); 
        return true; 
       } 
      } 
    ); 
} 

public void selectDrawerItem(MenuItem item) 
{ 
    switch(item.getItemId()) 
    { 
     case R.id.nav_home: 
      Intent intent = new Intent(getApplicationContext(), LoginActivity.class); 
      startActivity(intent); 
      break; 
     // more cases goes here to handle each menu item clicks 
     default: 
      //pop a Toast message or something about not existing function 
      drawer.closeDrawers(); 
      break; 
    } 
} 
+0

的ElementFactory部分不與在菜單中的項目。navigationView中的可用項目是通過以下佈局設置的:app:menu =「@ menu/nav_view_items」 – Bash

相關問題