单击菜单项时无法显示片段

时间:2013-01-28 03:55:25

标签: android android-fragments actionbarsherlock

单击菜单项时,我很难在容器中显示片段。这是我的代码。

我尝试在登录菜单项的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);
    }
}
}

2 个答案:

答案 0 :(得分:0)

而不是将碎片添加到一个容器中并隐藏&显示它们,您可以在单击菜单按钮时尝试更换碎片。

例如,当您点击登录按钮

ft.replace(R.id.fragment_container, loginFragment).commit();
试试希望它会起作用。

答案 1 :(得分:0)

您应该再次开始并提交交易。尝试:

@Override
public boolean onMenuItemClick(MenuItem item) {
    ft = getSupportFragmentManager().beginTransaction();
    ft.show(loginFragment);
    ft.commit();
    return false;
}