我正在创建一个小应用,允许用户查看不同类别的大学图书。
到目前为止,在活动主抽屉中,我有几个不同的类别,应该使用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 :(得分:0)
您的displaySelectedScreen()函数正在使用FragmentTransaction而不是FragmentManager。尝试使用FragmentManager。