单击菜单项时,我很难在容器中显示片段。下面是我的代码。
我试图ft.show();
在登录菜单项的 onMenuItemClick 内部进行操作,但它不起作用,我不知道我做错了什么,因为我既没有收到任何错误,也没有应用程序崩溃。附加到选项卡的片段可以正常工作和显示。只是当我尝试在单击菜单项时将片段加载到容器中时,没有任何反应。请指出正确的方向,让我知道我做错了什么。
public class HomeActivity extends SherlockFragmentActivity {
public SherlockFragment fragment;
FragmentTransaction ft;
public static Context appContext;
ActionBar actionbar;
String mCurFilter;
LoginScreenFragment loginFragment;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
appContext = getApplicationContext();
actionbar = getSupportActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab HomeTab = actionbar.newTab().setText("Home");
ActionBar.Tab CartTab = actionbar.newTab().setText("Cart");
ActionBar.Tab myTab = actionbar.newTab().setText("MyFragment");
ActionBar.Tab moreTab = actionbar.newTab().setText("More...");
HomeFragment homeFragment = new HomeFragment();
CartFragment cartFragment = new CartFragment();
MyFragment myFragment = new MyFragment();
MoreFragment moreFragment = new MoreFragment();
loginFragment = new LoginScreenFragment();
HomeTab.setTabListener(new MyTabsListener(homeFragment));
CartTab.setTabListener(new MyTabsListener(cartFragment));
myTab.setTabListener(new MyTabsListener(myFragment));
moreTab.setTabListener(new MyTabsListener(moreFragment));
actionbar.addTab(HomeTab);
actionbar.addTab(CartTab);
actionbar.addTab(myTab);
actionbar.addTab(moreTab);
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_container, homeFragment);
ft.show(homeFragment);
ft.add(R.id.fragment_container, cartFragment);
ft.hide(cartFragment);
ft.add(R.id.fragment_container, myFragment);
ft.hide(myFragment);
ft.add(R.id.fragment_container, moreFragment);
ft.hide(moreFragment);
ft.add(R.id.fragment_container, loginFragment);
ft.hide(loginFragment);
ft.commit();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar()
.getSelectedNavigationIndex());
System.out.println("onSaveInstanceState");
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
MenuItem item = menu.add("Search");
item.setIcon(android.R.drawable.ic_menu_search);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
View searchView = SearchViewCompat.newSearchView(this);
if (searchView != null) {
SearchViewCompat.setOnQueryTextListener(searchView,
new OnQueryTextListenerCompat() {
@Override
public boolean onQueryTextChange(String newText) {
mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
return true;
}
});
item.setActionView(searchView);
}
MenuItem loginItem = menu.add("Login");
loginItem.setTitle("Login");
loginItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
loginItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item) {
//I wanted to show "loginFragment" when the Login button is clicked.But unfortunately its not showing up.Also the application is not giving any errors or crashing
ft.show(loginFragment);
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.menuitem_home) {
Toast.makeText(appContext, "Home", Toast.LENGTH_SHORT).show();
return true;
} else if (itemId == R.id.menuitem_shop) {
Toast.makeText(appContext, "Shop", Toast.LENGTH_SHORT).show();
return true;
} else if (itemId == R.id.menuitem_cart) {
Toast.makeText(appContext, "Cart", Toast.LENGTH_SHORT).show();
return true;
} else if (itemId == R.id.menuitem_my) {
Toast.makeText(appContext, "My", Toast.LENGTH_SHORT).show();
return true;
} else if (itemId == R.id.menuitem_more) {
Toast.makeText(appContext, "More...", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
static class MyTabsListener implements ActionBar.TabListener {
public SherlockFragment fragment;
public MyTabsListener(SherlockFragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
System.out.println("TabReselected " + tab.getPosition());
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
System.out.println("TabSelected " + tab.getPosition());
ft.show(this.fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
System.out.println("TabUnselected " + tab.getPosition());
ft.hide(this.fragment);
}
}
}