2017-01-01 13 views
-3

Androidには非常に新しいので、Androidでレイアウトを使用する標準的な方法についていくつかの混乱があります。基本的に、私はiOS開発者です。 NavigationDrawerViewテンプレートを使用して新しいプロジェクトを作成しました。今度は、ナビゲーションビューでツールバーと引き出しを使って別のアクティビティを作成する必要があります。基本的には、ツールバー、引き出し、ナビゲーションビューを持つレイアウトを設計したいだけです。これはアクティビティに含まれるだけで、コンテンツは新しく作成されたアクティビティで設計できます。誰もがこれを行うための最良の方法を提案することができます。私は、すべての人がナビゲーション・ドロワー・ビューのデフォルト・テンプレート設計を知っていることを願っています。だから私はここにコードを含めていない。誰かがコードを見たいと思ったら、私に知らせてください。あなたは次のXMLファイルを使用する必要がありそのために引き出しとナビゲーションビューでツールバーのレイアウトを再利用

+0

をitemselectedとき、あなたの破片を呼び出します要件を満たすために単一のアクティビティにあり、フラグメント間を切り替える必要があります。すべてのビューに簡単にアクセスできます –

答えて

1

: content_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.android.MainActivity"//you main activity 
    tools:showIn="@layout/app_bar_main"> 

</RelativeLayout> 

app_bar_main.xml:

<RelativeLayout 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="wrap_content" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <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="@drawable/actionbar" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

    <include layout="@layout/content_main" /> 

</RelativeLayout> 

activity_main:

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

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <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:background="@color/navDrawerbg" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

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

そしてメインのjavaを作りますファイルie mainactivity extends AppcompatActivity and implements NavigationView.OnNavigationItemSelectedListener

グローバルvarible宣言:

private Context mContext; 
     private static FragmentManager mManager; 
       Fragment fragment = null; 

はOnCreateの後に方法の下に作成し

プライベートボイドinitUI(){

if (fragment != null) { 

     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.content_main, fragment).commit(); 

    } 

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

    getSupportActionBar().setTitle("Home"); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

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

}

fragmentviewをIntiate

private void initiateFragmentView() { 
     if (fragment != null) { 
      FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_main, fragment).commit(); 
     } 
    } 

そして(setcontentview後のOnCreateメソッドでメソッドの上に呼び出す)

mManager = getSupportFragmentManager(); 
       fragment = new Home(); 

       mContext = this; 
       initUI(); 

引き出しは、あなたが、フラグメントを使用する主な活動でフレームを使用しての断片を切り替えることができます

@Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      // Handle navigation view item clicks here. 
      int id = item.getItemId(); 

      if (id == R.id.nav_home) { 

       //Your fragment 
       fragment = new Home(); 
       initiateFragmentView(); 

      } else if (id == R.id.nav_xyz) { 

       fragment = new xyz(); 
       initiateFragmentView(); 


      } else if (id == R.id.nav_abc) { 

       fragment = new abc(); 
       initiateFragmentView(); 

      } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      drawer.closeDrawer(GravityCompat.START); 
      return true; 
     } 
関連する問題