2016-10-11 188 views
0

我試圖從我的活動中調用導航抽屜,但沒有發生任何事情。我應該使用ActionBarDrawerToggle嗎?打開導航按鈕的抽屜

public class SettingsDrawer { 
    Context mContext; 
    ArrayList<SettingsDrawerItem> items; 
     ListView listView; 
     ImageView logo; 
     DrawerLayout mDrawerLayout; 
     public SettingsDrawer(Context context) { 
      this.mContext = context; 
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view = inflater.inflate(R.layout.settings_drawer_layout, null); 
      mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawerLayout); 
      listView = (ListView) view.findViewById(R.id.settingsList); 
      logo = (ImageView) view.findViewById(R.id.logo); 
      DrawListAdapter adapter = new DrawListAdapter(mContext, items); 
      listView.setAdapter(adapter); 
       mDrawerLayout.openDrawer(Gravity.LEFT); 
     } 

而且在我的活動

public class Activity extends AppCompatActivity{ 

Button settingsDrawerButton =(Button)findViewById(R.id.settingsDrawer); 
     settingsDrawerButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i(TAG,"settings drawer click!"); 
       SettingsDrawer s = new SettingsDrawer(Activity.this); 

      } 
     }); 
} 

UPDATE:XML我試圖建立這樣的:mDrawerLayout.openDrawer(drawerPane),但是Ive得到ClassCastException異常也是如此。我認爲這是非常可靠的,因爲DrawerLayout不能轉換爲RelativeLayout。

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <RelativeLayout 
     android:layout_width="280dp" 
     android:layout_height="match_parent" 
     android:id="@+id/drawerPane" 
     android:layout_gravity="start"> 


     <RelativeLayout 
      android:id="@+id/logoBox" 
      android:layout_width="match_parent" 
      android:layout_height="100dp" 
      android:background="@color/material_blue_grey_800" 
      android:padding="8dp" > 

      <ImageView 
       android:id="@+id/logo" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_marginTop="15dp" 
       android:layout_centerInParent="true"/> 

     </RelativeLayout> 

     <ListView 
      android:id="@+id/settingsList" 
      android:layout_width="280dp" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/logoBox" 
      android:choiceMode="singleChoice" 
      android:background="#ffffffff" /> 
    </RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 
+0

添加此行mDrawerLayout.openDrawer(listView); – user3068659

回答

1

找到抽屜佈局是這樣的:

myDrawerLayout = (DrawerLayout) getView().findViewById(R.id.yourdrawer); 

變量myDrawerLayout應該在你的活動領域:

DrawerLayout myDrawerLayout; 

然後它能夠​​更好地做出這樣的方法在你的活動:

public void openDrawer(){ 
myDrawerLayout.openDrawer(mDrawerLayout); 

}

whever要關閉抽屜

然後輕鬆地調用此方法;)

+0

是的,但我想在specyfic類中設置我的抽屜內容,然後從這對夫婦的活動中調用它來不重複每個活動中的代碼 – Expiredmind

+0

您無法打開其他類的抽屜,因爲它屬於此活動。儘管如此,您可以使此方法爲公共靜態。順便說一句,這實際上不是一個好的做法,但你可以做的一切。 –

+0

我知道,所以我想每次當我去不同的活動,我想設置我的抽屜,這就是爲什麼我在不同的類中實現SettingsDrawer。問題是,我無法從我的活動類中調用抽屜 – Expiredmind

0

mDrawerLayout.openDrawer(ListView控件);

+0

這拋出了classCastException,儘管我不認爲它是一個好主意,因爲在我的DrawerLayou中,我獲得了幾個項目不僅僅是一個列表。 – Expiredmind

+0

你可以發佈你的XML部分嗎? – user3068659

+0

我更新了我的問題 – Expiredmind

0
the below code is my xml 

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 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" 
     tools:context="mActivity"> 

     <android.support.v4.widget.DrawerLayout 
      android:id="@+id/mDrawerLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 
      <RelativeLayout 
       android:id="@+id/mDrawerMainLayout" 
       android:layout_width="340dp" 
       android:layout_gravity="start" 
       android:layout_height="match_parent"> 

       <ListView 
        android:id="@+id/mListView" 
        android:layout_width="340dp" 
        android:layout_height="match_parent" 
        android:background="@color/white" 
        android:choiceMode="singleChoice" 
        android:dividerHeight="1dp" /> 
      </RelativeLayout> 
     </android.support.v4.widget.DrawerLayout> 

    </RelativeLayout> `from that I fixed private void` 

openAndColseDrawerList() { 
     if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { 
      mDrawerLayout.closeDrawer(mDrawerMainLayout); 
     } else 
      mDrawerLayout.openDrawer(mDrawerMainLayout); 
    } 
+0

奧基我想我知道什麼是問題。該Activity具有不同於setting_drawer_layout的佈局xml。當我打開抽屜時,它會以不同的視角觸發並且什麼也沒有發生。你有什麼想法如何在活動視圖中觸發它? – Expiredmind

+0

我認爲你最好使用SettingsDrawer擴展片段並在你的Acivity中實現 – user3068659