2017-07-29 68 views
0

我已經創建了一個選項卡式活動,所以已經爲我完成了很多事情。當我點擊一個抽屜項目時,我跟隨了一個教程,轉到另一個片段。現在,我需要在初始化我的應用程序時將我的MainFragment設置爲默認值。我其實並不知道目前出現的情況。配置一個片段,當應用程序啓動時首先顯示

下面是的MainActivity這是負責處理片段更改代碼:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 
    @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); 
     toggle.syncState(); 

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

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 

     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     return id == R.id.action_settings || super.onOptionsItemSelected(item); 
    } 

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.nav_home: 
       setFragmentTransaction(new MainFragment(), "Dashboard"); 
       break; 
      case R.id.nav_accounts: 
       setFragmentTransaction(new AccountFragment(), "Accounts"); 
       break; 
      case R.id.nav_categories: 
       break; 
      case R.id.nav_transactions: 
       break; 
      case R.id.nav_reports: 
       break; 
     } 

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

     return true; 
    } 

    private void setFragmentTransaction(Fragment fragment, String title) { 
     setTitle(title); 

     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.replace(R.id.fragment_container, fragment); 
     transaction.commit(); 
    } 
} 

我試圖建立一個default,但它並沒有造成任何影響。

+1

這聽起來像你需要添加額外的'setFragmentTransaction(new MainFragment(),「Dashboard」);'到'onCreate'的底部。你嘗試過嗎? –

+0

我簡直不敢相信。可能不是最好的,但它的工作。請寫一個答案,我會標記爲正確和投票。 – mfgabriel92

+0

我很高興能幫到你! –

回答

2

setFragmentTransaction(new MainFragment(), "Dashboard");加到onCreate的底部。這應該讓你的主要片段加載。

1

你可以這樣做,你必須在onCreate本身加載你的片段之一,以便它可以在應用程序啓動時啓動。

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @BindView(R.id.toolbar) 
    Toolbar toolbar; 
    @BindView(R.id.nav_view) 
    NavigationView navigationView; 
    @BindView(R.id.drawer_layout) 
    DrawerLayout drawer; 

    // tags used to attach the fragments 
    public static final String TAG_CHAR = "characters"; 
    public static final String TAG_CO = "comics"; 
    public static final String TAG_EV = "events"; 
    public static final String TAG_AB = "about"; 
    public static String CURRENT_TAG = TAG_CHAR; 

    // index to identify current nav menu item 
    public static int navItemIndex = 0; 

    public Fragment fragment; 

    private Fragment sendFragment; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ButterKnife.bind(this); 
     setSupportActionBar(toolbar); 

     if (savedInstanceState != null) { 
      fragment = getSupportFragmentManager().getFragment(savedInstanceState, "myFragmentName"); 
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, 
        android.R.anim.fade_out); 
      fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG); 
      fragmentTransaction.commit(); 
     } else { 
      navItemIndex = 0; 
      CURRENT_TAG = TAG_CHAR; 
      loadHomeFragment(); 
     } 

     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 

     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     navigationView.setNavigationItemSelectedListener(this); 
    } 

    private void loadHomeFragment() { 
     selectNavMenu(); 

     if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) { 
      drawer.closeDrawers(); 
      return; 
     } else { 
      fragment = getHomeFragment(); 
      FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, 
        android.R.anim.fade_out); 
      fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG); 
      fragmentTransaction.commit(); 
      drawer.closeDrawers(); 
     } 
    } 

    private Fragment getHomeFragment() { 
     switch (navItemIndex) { 
      case 0: 
       sendFragment = new CharacterFragment(); 
       break; 
      case 1: 
       sendFragment = new ComicFragment(); 
       break; 
      case 2: 
       sendFragment = new EventFragment(); 
       break; 
      case 3: 
       sendFragment = new AboutFragment(); 
       break; 
      default: 
       sendFragment = new CharacterFragment(); 
     } 
     return sendFragment; 
    } 

    private void selectNavMenu() { 
     navigationView.getMenu().getItem(navItemIndex).setChecked(true); 
    } 

    @Override 
    public void onBackPressed() { 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
      return; 
     } 
     super.onBackPressed(); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem menuItem) { 

     switch (menuItem.getItemId()) { 
      case R.id.nav_char: 
       navItemIndex = 0; 
       CURRENT_TAG = TAG_CHAR; 
       break; 
      case R.id.nav_com: 
       navItemIndex = 1; 
       CURRENT_TAG = TAG_CO; 
       break; 
      case R.id.nav_ev: 
       navItemIndex = 2; 
       CURRENT_TAG = TAG_EV; 
       break; 
      case R.id.nav_about_us: 
       navItemIndex = 3; 
       CURRENT_TAG = TAG_AB; 
       break; 
      default: 
       navItemIndex = 0; 
     } 
     if (menuItem.isChecked()) { 
      menuItem.setChecked(false); 
     } else { 
      menuItem.setChecked(true); 
     } 
     menuItem.setChecked(true); 

     loadHomeFragment(); 
     return true; 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     getSupportFragmentManager().putFragment(outState, "myFragmentName", fragment); 
    } 

    @Override 
    public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) { 
     super.onRestoreInstanceState(savedInstanceState, persistentState); 
    } 
} 
相關問題