メニュー項目をクリックしたときにコンテナ内のフラグメントを表示するのに問題があります。以下は私のコードです。
Login Menu ItemのonMenuItemClick内で実行しようとしましft.show();
たが、機能せず、エラーが発生せず、アプリがクラッシュしないため、何が間違っているのかわかりません。タブに接続されているフラグメントは機能しており、正常に表示されています。メニュー項目がクリックされたときにコンテナにフラグメントをロードしようとすると、何も起こりません。正しい方向に向けてください。私が何を間違っているのかを理解してください。
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);
}
}
}