2017-01-12 3 views
1

현재 시작과 같이 fragment_empty를 사용 중입니다. fragment_empty fragment_home을 시작 페이지로 사용하고 싶습니다 (앱을 시작할 때).하지만 모든 페이지에 fragment_home을 표시하고 싶지는 않습니다.android studio 시작 페이지로 탐색 활동에서 조각을 설정하는 방법

fragment_home 페이지 :

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Welkom op home!" 
    android:padding="10dp" 
    /> 

<ImageView 
    android:layout_width="236dp" 
    android:layout_height="218dp" 
    android:src="@mipmap/ic_launcher" 
    android:id="@+id/imageView2" /> 

fragment_empty 페이지 :

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

내 MainActivity (탐색 서랍) :

public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    Fragment fragment = null; 
     if (id == R.id.nav_home) { 
      fragment = new HomeFragment(); 
     } else if (id == R.id.nav_evenementen) { 
      fragment = new EvenementenFragment(); 
     } else if (id == R.id.nav_contact) { 
      fragment = new ContactFragment(); 
     } else if (id == R.id.nav_share) { 

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

     } 


    if (fragment != null) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.frame_container, fragment).commit(); 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

App_bar_main 페이지 :

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

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    app:srcCompat="@android:drawable/ic_dialog_email" /> 

<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.design.widget.AppBarLayout> 

답변

0
your MainActivity onCreate() method 

you can directly call first time HomeFragment like 

HomeFragment mHomeFragment = new HomeFragment(); 

FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.frame_container, mHomeFragment).commit(); 
0

사용 add 대신하여 MainActivityonCreate() 방법 추가 기능에서 replace

는 :

HomeFragment fragment = new HomeFragment(); 
FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .add(R.id.frame_container, fragment).commit(); 

이것은 당신의 frame_container에 조각의 XML을 추가합니다. Fragments을 변경하려면 코드에서 이미 정의한 것과 같은 방법을 사용하십시오.

+0

덕분에 내 친구 –

+0

Np를 같이 위의 코드를 추가 할 수 있습니다. 당신의 Deveopment와 행운을 빌어 요! –

0

는이

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


     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); 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 


    //Here the Code 


HomeFragment fragment = new HomeFragment(); 
FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction() 
       .add(R.id.frame_container, fragment).commit(); 
    }