2017-04-25 57 views
0

遇到問題我正在創建一個小應用程序,該應用程序允許用戶查看不同類別的大學圖書。使用onNavigationItemSelected和beginTransaction()

到目前爲止,在活動主抽屜中,我有幾個不同的類別,應該使用listView加載一個佈局,並在選中時從arrayAdapter填充此列表。我已經在我的一個佈局中設置了半工作,但是當我更改爲與抽屜不同的類別時,出現問題,ListView仍然顯示在屏幕上,我似乎無法弄清楚如何讓它停止。

我認爲我需要使用endTransaction(),但如果我使用正確的東西,我還不確定。

我是Java和Android Studio的初學者,所以我希望我對我的要求很有意義。

如果需要更多的代碼或信息,我會很樂意提供。

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.view.ViewGroup; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.Fragment; 

import java.util.ArrayList; 
import java.util.List; 

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); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

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

    FragmentManager fm = getSupportFragmentManager(); 
    fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit(); 

    //loads the home screen when app launches 
    displaySelectedScreen(R.id.nav_home); 

} 

@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) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

private void displaySelectedScreen(int id) { 

    Fragment fragment = null; 

    //loads each layout inflator depending on what menu item has been clicked 
    switch(id) { 
     case R.id.nav_home: 
      fragment = new Home(); 
      break; 
     case R.id.nav_book: 
      fragment = new All(); 
      break; 
     case R.id.nav_sci_fi: 
      fragment = new SciFi(); 
      break; 
     case R.id.nav_fantasy: 
      fragment = new Fantasy(); 
      break; 
     case R.id.nav_dystopian: 
      fragment = new Dystopian(); 
      break; 
     case R.id.nav_about: 
      fragment = new About(); 
      break; 
     case R.id.nav_contact: 
      fragment = new ContactUs(); 
      break; 
    } 

    if(fragment != null) { 

     FragmentTransaction ft = getSupportFragmentManager(). beginTransaction(); 
     ft.replace(R.id.content_main, fragment); 
     ft.commit(); 
    } 

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

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 

    FragmentManager fm=getSupportFragmentManager(); 

    int id = item.getItemId(); 

    if (id == R.id.nav_book) { 
     // Handle the camera action 
    } else if (id == R.id.nav_sci_fi) { 
     fm .beginTransaction().replace(R.id.content_frame, new BookFragment()).commit(); setTitle("Sci-Fi"); 
     //TODO: endTransaction 

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

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

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

    displaySelectedScreen(id); 

    return true; 
} 
} 
+0

你有'FragmentTransaction's發生在三個不同的地方;其中兩人在'content_frame'中添加'Fragment',另一個在'content_main'中添加。一個'replace()'事務只會在你傳遞它的'ViewGroup'中刪除以前的'Fragment's。另外,你在啓動時加載了一個'Home'和'MainFragment',並且可能在'onNavigationItemSelected()'中執行了兩個事務,因爲它也調用了'displaySelectedScreen()'。 –

+0

謝謝,這讓我對自己的工作更加清楚。 – Brandan

回答

0

您的displaySelectedScreen()函數使用FragmentTransaction而不是FragmentManager。嘗試使用FragmentManager。

+0

當我將其更改爲FragmentManager時,該行的其餘部分出現錯誤,並在下面進行了替換和提交。我不確定一旦發生了什麼改變,以補救它。 – Brandan

相關問題