2014-10-18 78 views
6

我正在使用appcompat v7庫對我的應用程序的材質設計版本進行操作,並且導航抽屜遇到了問題。打開時,材質設計工具欄中的按鈕停止工作 - 導航抽屜外的任何觸摸都會關閉抽屜。接下來的是我的意思導航抽屜打開時,工具欄按鈕不響應觸摸

the gif http://images.rymate.co.uk/images/83JXi1X.png

這裏,GIF是我使用該活動的XML佈局:

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

    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/action_toolbar" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:minHeight="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      android:elevation="4dp" 
      /> 

     <FrameLayout 
      android:id="@+id/note_list_container" 
      android:layout_marginTop="?attr/actionBarSize" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <net.rymate.notes.ui.FloatingActionButton 
      android:id="@+id/fabbutton" 
      android:layout_width="72dp" 
      android:layout_height="72dp" 
      android:layout_gravity="bottom|right" 
      android:layout_marginBottom="16dp" 
      android:layout_marginRight="16dp" /> 
    </FrameLayout> 
    <!-- The navigation drawer --> 
    <LinearLayout 
     android:id="@+id/left_drawer" 
     android:layout_width="300dp" 
     android:layout_marginTop="?attr/actionBarSize" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:orientation="vertical"> 

     <ListView 
      android:id="@+id/cat_list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" 
      android:background="?catBackColour" /> 
    </LinearLayout> 

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

這裏是onCreate代碼初始化抽屜和工具欄。

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

    ROBOTO_LIGHT = Typeface.createFromAsset(this.getAssets(), "Roboto-Light.ttf"); 
    ROBOTO_LIGHT_ITALICS = Typeface.createFromAsset(this.getAssets(), "Roboto-LightItalic.ttf"); 

    setContentView(R.layout.activity_notes); 

    if (findViewById(R.id.note_container) != null) { 
     // The detail container view will be present only in the 
     // large-screen layouts (res/values-large and 
     // res/values-sw600dp). If this view is present, then the 
     // activity should be in two-pane mode. 
     mTwoPane = true; 

     // In two-pane mode, list items should be given the 
     // 'activated' state when touched. 
     FragmentManager fm = getSupportFragmentManager(); 
     //list.setActivateOnItemClick(true); 
    } 


    if (!mTwoPane) { 
     final FloatingActionButton mFab = (FloatingActionButton) findViewById(R.id.fabbutton); 
     mFab.init(Color.parseColor("#1e90ff")); 
     mFab.setFabDrawable(getResources().getDrawable(R.drawable.ic_action_new)); 
     mFab.showFab(); 

     mFab.setOnClickListener(this); 

     list = new NotesListFragment(mFab); 
    } else { 
     list = new NotesListFragment(); 
    } 


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

    getSupportFragmentManager().beginTransaction() 
      .replace(R.id.note_list_container, list) 
      .commit(); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // the layout 
    mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer); 

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    getSupportActionBar().setHomeButtonEnabled(true); 

    mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent)); 

    mDrawerToggle = new ActionBarDrawerToggle(
      this,     /* host Activity */ 
      mDrawerLayout,   /* DrawerLayout object */ 
      toolbar, /* toolbar */ 
      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) { 
      getSupportActionBar().setTitle("Rymate Notes"); 
      supportInvalidateOptionsMenu(); 
     } 

     /** Called when a drawer has settled in a completely open state. */ 
     public void onDrawerOpened(View drawerView) { 
      getSupportActionBar().setTitle("Categories"); 
      supportInvalidateOptionsMenu(); 
     } 
    }; 

    // Set the drawer toggle as the DrawerListener 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    pref = getSharedPreferences("rymatenotesprefs", MODE_PRIVATE); 

    mDrawerList = (ListView) findViewById(R.id.cat_list); 

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

    mDbHelper = new NotesDbAdapter(this); 
    mDbHelper.open(); 

    if (mDbHelper.fetchAllNotes().getCount() == 0) { 
     IntroFragment fragment = new IntroFragment(); 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.note_list_container, fragment) 
       .commit(); 

    } 

    getCategories(); // calls a function which populates the listview 

} 

有沒有辦法解決這個問題?

+1

材料設計規範指出,導航抽屜應位於工具欄上並位於狀態欄下方,在這種情況下,該按鈕的行爲沒有響應是非常有意義的。看看這裏:http://www.google.com/design/spec/patterns/navigation-drawer.html – 2014-11-10 09:24:49

回答

1

結果Nikola Despotoski有正確的想法 - 觸摸事件被DrawerLayout竊取。然而,而不是攔截的觸摸事件我只是調整了活動佈局像這樣:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/action_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:minHeight="?attr/actionBarSize" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?attr/actionBarSize" > 

     <!-- The main content view --> 
     <FrameLayout 
      android:id="@+id/container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <FrameLayout 
       android:id="@+id/note_list_container" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 

      <net.rymate.notes.ui.FloatingActionButton 
       android:id="@+id/fabbutton" 
       android:layout_width="72dp" 
       android:layout_height="72dp" 
       android:layout_gravity="bottom|right" 
       android:layout_marginBottom="16dp" 
       android:layout_marginRight="16dp" /> 
     </FrameLayout> 
     <!-- The navigation drawer --> 
     <LinearLayout 
      android:id="@+id/left_drawer" 
      android:layout_width="300dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:orientation="vertical"> 

      <ListView 
       android:id="@+id/cat_list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="?catBackColour" 
       android:choiceMode="singleChoice" 
       android:divider="@android:color/transparent" 
       android:dividerHeight="0dp" /> 
     </LinearLayout> 

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

這樣具有允許觸摸事件由Toolbar而非DrawerLayout註冊的預期效果。

+10

,所以你寫了一個基於別人的建議,你自己的問題的答案,然後接受自己的答案?甚至沒有贊成或接受他的回答? – VipulKumar 2014-11-12 08:22:14

+0

很難理解佈局中的哪個變化使抽屜停止攔截工具欄中的觸摸事件... – Marek 2015-10-21 18:02:23

11

似乎觸摸事件由出票人的影子被盜,即DrawerLayout保持攔截觸摸事件,因爲Toolbar是內容視圖的一部分,不像ActionBar正對裝飾視圖的頂部。

可能的解決辦法是攔截觸摸事件:

如果S之間0(頂部)和Toolbar高度(底部),直接分派事件到Toolbar對象。否則保持正常行爲。

抽屜翻轉卡嗒聲同樣如此嗎?