片段行动酒吧象和导航抽屉

时间:2014-03-22 12:04:20

标签: android android-fragments actionbarsherlock

我的应用使用导航抽屉和actionbarsherlock。我想在片段中使用行为项,如下所示:

public class StartFragment extends SherlockFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_start, container,
            false);

    setHasOptionsMenu(true);
    return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.start, menu);
}

// Handle context menu events
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case R.id.backup:
        Log.i(TAG, "Backup clicked");
        break;
    case R.id.settings:
        Log.i(TAG, "Settings clicked");
    }
    return (super.onOptionsItemSelected(menuItem));
}

}

问题是,单击图标时不会触发日志输出,但仅当我打开导航抽屉时。

以下是主要活动:

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import xyz.StartFragment;

public class MainActivity extends SherlockFragmentActivity {

// Variables
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] title;
String[] subtitle;
int[] icon;
Fragment fragment1 = new StartFragment();
private CharSequence mDrawerTitle;
private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_main);

    // Get the title
    mTitle = mDrawerTitle = getTitle();

    // Generate title
    title = getResources().getStringArray(R.array.actionbar_menu);

    // Generate icon
    icon = new int[] { R.drawable.home, R.drawable.mozart,
            R.drawable.action_settings, R.drawable.action_settings,
            R.drawable.action_settings, R.drawable.collections_cloud };

    // Locate DrawerLayout in drawer_main.xml
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Locate ListView in drawer_main.xml
    mDrawerList = (ListView) findViewById(R.id.listview_drawer);

    // Set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    // Pass string arrays to MenuListAdapter
    mMenuAdapter = new MenuListAdapter(MainActivity.this, title, icon);

    // Set the MenuListAdapter to the ListView
    mDrawerList.setAdapter(mMenuAdapter);

    // Capture listview menu item click
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // Enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            // TODO Auto-generated method stub
            super.onDrawerClosed(view);
        }

        public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
            // Set the title on the action when drawer open
            getSupportActionBar().setTitle(mDrawerTitle);
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);

    }

}



@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getItemId() == android.R.id.home) {

        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }


    return super.onOptionsItemSelected(item);
}

// ListView click listener in the navigation drawer
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // Locate Position
    switch (position) {
    case 0:
        ft.replace(R.id.content_frame, fragment1);
        break;
    case 1:
        ft.replace(R.id.content_frame, fragment2);
        break;
    case 2:
        ft.replace(R.id.content_frame, fragment3);
        break;
    }
    ft.commit();
    mDrawerList.setItemChecked(position, true);

    // Get the title followed by the position
    setTitle(title[position]);
    // Close drawer
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

}

任何线索?

3 个答案:

答案 0 :(得分:3)

根据documentation,您需要在片段setHasOptionsMenu()中调用onCreate,因此请尝试将其添加到您的片段代码中:

public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

请注意,该文档告诉您在setHasOptionsMenu中使用onCreate。您在onCreateView

中呼叫它

修改
也来自documentation hereonCreateOptionsMenu不会调用超级方法,它只是创建菜单并返回true:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

所以你可能不得不在调用super-method之前给它充气,或者根本不调用它。

<强> EDIT2

我想这应该是actionbarsherlock的以下内容:

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
    inflater.inflate(R.menu.start, menu);
    return true;
}

但是,这些图标根本不会出现

<强> EDIT3
对不起,我在编辑中不够清楚。您在此处看到的代码来自“活动”。对于片段,它看起来有点不同。因此,通过一些更多的研究,我发现this answer给出了工作代码。这支持我的想法来自编辑:你必须在给布局膨胀后调用超级方法。在您的情况下,Fragment中的代码必须如下所示:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.start, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

答案 1 :(得分:1)

我相信&#39;备份&#39;按钮属于活动,而不是片段,这就是你在片段的onOptionsItemSelected方法中没有得到回调的原因。

如果你想在按下这个按钮时从片段中执行代码,你可以简单地为你的片段添加一个方法,然后从活动的onOptionsItemSelected中调用它:

的活动:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        fragment1.onUp();
        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

片段:

public void onUp() {
    Log.d("backup clicked");
}

答案 2 :(得分:0)

我自己解决了这个问题。活动的onOptionsItemSelected方法不正确。我在文档中找到了有关处理操作项点击的部分中的解决方案:http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

我根据文档实现了该方法,一切正常。