2017-06-12 11 views
0

各アクティビティーでツールバーに表示されるナビゲーション・ドロワー・メニューを実装しようとしています。私は共通のメニューアクティビティクラスを作成することに決めました。他のすべてのアクティビティはそれから拡張されています。これは、navのツールバーの下にアクティビティを置くことを除いて、うまく動作します。メニュー。ナビゲーション・ドロワー・メニューのツールバーでアクティビティーの内容を取得できません

Intent intent = new Intent(getApplicationContext(), TrainingActivity.class); 
startActivity(intent); 

そしてTrainingActivityで:私はアイテムのタッチを管理するのと同じクラス(メニュー項目選択)で

MenuActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 
... 
super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_menu); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { 
      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       changeActivity(); 
      } 
     }; 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 

     navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     navigationView.setItemIconTintList(null); 

     navigationView.getMenu().getItem(currentMenuItem).setChecked(true); 
... 

public class TrainingActivity extends MenuActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.setStartingState(MenuItemNames.TRAINING); 
     super.onCreate(savedInstanceState); 
     FrameLayout contentLayout = (FrameLayout) findViewById(R.id.content_frame); 
     View contentView = getLayoutInflater().inflate(R.layout.activity_training, contentLayout, false); 
     drawer.addView(contentView, 0); 

//  setContentView(R.layout.activity_training); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

...

ここ

がactivity_menuです.xml:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <include 
      layout="@layout/app_bar_menu" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/content_frame"/> 
    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 

     app:menu="@menu/activity_menu_drawer" /> 

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

app_bar_menu.xml:

<android.support.design.widget.CoordinatorLayout 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/app_bar_menu_coord_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.magnifi.pennantrace.MenuActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

      <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/relativelayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:textStyle="bold" 
        android:layout_height="wrap_content" 
        android:id="@+id/toolbar_div_rank" 
        android:text="4th Place" 
        app:layout_constraintTop_toTopOf="parent" 
        app:layout_constraintLeft_toLeftOf="parent" /> 

       ... 

      </android.support.constraint.ConstraintLayout> 
     </android.support.v7.widget.Toolbar> 

    </android.support.design.widget.AppBarLayout> 

</android.support.design.widget.CoordinatorLayout> 

だから私が実装しようとしているロジックは、下でframeLayout(content_frameに項目をメニューで選択されるたびに、私は(それを膨らませる)適切な活動をロードしていますということですactivity_menu.xmlのツールバー)。だから、それはツールバーの下になければならないようだが、そうではない。私を助けて、私のミスはどこにあるのか教えてください。

+0

'drawer.addView(contentView、0);' - あなたがいない 'drawer' –

+0

@、contentLayout''に 'contentView'を追加しますMikeM。私がcontentLayout.addView(contentView、0)を行うと、メニュー/ツールバーを除いて何も表示されません。 – maximus

+0

うん、ちょうど気づいたように、あなたは 'content_frame'を持っている垂直' LinearLayout'の中に2つのものがあり、どちらも 'match_parent'の高さを持っていますので、' content_frame'が一番下に押し出されています。 'content_frame'を' app_bar_menu'に移動して、 'DrawerLayout'の' LinearLayout'を取り除きたいと思うかもしれません。 –

答えて

0

私は、アクティビティをフラグメントで置き換えることでこの問題を解決しました。 ので、代わりとして活動を宣言する:選択したメニュー項目が、私は次のことをやった変更

public class TrainingActivity extends Fragment { 

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

    public static TrainingActivity newInstance(String param1, String param2) { 
     TrainingActivity fragment = new TrainingActivity(); 
     Bundle args = new Bundle(); 
     fragment.setArguments(args); 
     return fragment; 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.activity_training, container, false); 
     imageButtonsSetup(view); 
     return view; 
    } 

    private void imageButtonsSetup(View view) { 
     setImageButtonWithResourceId(view, R.id.imageButtonSelectTraining); 
     setImageButtonWithResourceId(view, R.id.imageButtonSelectPlayers); 
    } 

    private void setImageButtonWithResourceId(View view, int resourceId) { 
     ((ImageButton)view.findViewById(resourceId)).setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: { 
         ImageButton view = (ImageButton) v; 
         view.getBackground().setColorFilter(0x77777700, PorterDuff.Mode.SRC_ATOP); 
         v.invalidate(); 
         break; 
        } 
        case MotionEvent.ACTION_UP: 

         // Your action here on button click 

        case MotionEvent.ACTION_CANCEL: { 
         ImageButton view = (ImageButton) v; 
         view.getBackground().clearColorFilter(); 
         view.invalidate(); 
         break; 
        } 
       } 
       return true; 
      } 
     }); 
    } 
} 

とIF:フラグメントに変更アクティビティの public class TrainingActivity extends Fragment {

例: public class TrainingActivity extends MenuActivity {

私はでそれを置き換えます:

try { 
       fragment = (Fragment) fragmentClass.newInstance(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 

だから、 drawer addViewなどはもう必要ありません。私は現在content_frameを置き換えています。

最終app_bar_menu.xmlは、次のように変更されます。

<LinearLayout 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/app_bar_menu_coord_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:fitsSystemWindows="true" 
    tools:context="com...."> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

      ..... 
     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.AppBarLayout> 

    <FrameLayout 
     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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:id="@+id/content_frame"/> 
</LinearLayout> 
関連する問題