2016-10-11 14 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)が、アイブ氏は、同様に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

しかし、私は特定のクラスで私の引き出しのコンテンツをセットアップし、カップルの活動からコードをreapeatingしないように呼び出す各アクティビティで – Expiredmind

+0

あなたはこのアクティビティに属する別のクラスのcuzから引き出しを開くことはできません。ただし、このメソッドをpublic staticにすることもできます。 Btwこれは実際には良い練習ではなく、すべてあなたができることです。 –

+0

私は別のクラスにSettingsDrawerを実装しているので、別のアクティビティに行くたびに引き出しを設定したいと思っています。問題は、自分のアクティビティクラスから引き出しを呼び出すことができないということです。 – Expiredmind

0

mDrawerLayout.openDrawer(リストビュー)。

+0

これは私には良いアイデアだとは思いますが、classCastExceptionを投げます。 – 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

Okey私は問題が何であるか知っていると思います。アクティビティには、setting_drawer_layoutよりも異なるレイアウトxmlがあります。引き出しを開くと、それは異なる視点でトリガーされ、何も起こりません。アクティビティビューでトリガする方法を知っていますか? – Expiredmind

+0

あなたは、あなたのAcivity内でSettingsDrawerの断片を拡張して実装する方がよいと思います – user3068659

関連する問題