处理ActionBarDrawerToggle和ActionBar的click事件

时间:2015-06-07 09:06:59

标签: android onclick android-actionbar drawerlayout

我为ActionBarDrawerToggle实施了NavigationDrawer,并使用了ActionBar 如何处理这两个点击事件,因为两者都需要onOptionsItemSelected(MenuItem item)方法? 还有其他方法吗?

public class A1 extends AppCompatActivity implements OnItemClickListener {
    String[] menu;
    DrawerLayout dLayout;
    ListView dList;
    ArrayAdapter < String > adapter;
    ActionBarDrawerToggle drawListener;

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

        menu = new String[] {
            "Home", "Android", "Windows", "Linux", "Raspberry Pi", "WordPress", "Videos", "Contact Us"
        };
        dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        dList = (ListView) findViewById(R.id.left_drawer);

        adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, menu);

        dList.setAdapter(adapter);
        dList.setOnItemClickListener(this);
        dList.setSelector(android.R.color.holo_blue_dark);
        drawListener = new ActionBarDrawerToggle(this, dLayout, R.drawable.ic_draw, R.string.dopen) {

            @Override
            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerOpened(drawerView);
                getSupportActionBar().setTitle("Menu");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(drawerView);
            }
        };
        dLayout.setDrawerListener(drawListener);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    public boolean onCreateOptionsMenu(Menu menu) //for actionbar  
    {
        getMenuInflater().inflate(R.menu.actionbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.copy:
                return showToast("Copy");
            default:
                return false;
        }
        //  if(drawListener.onOptionsItemSelected(item))
        //  {
        //  return true;
        //  }
        //      return super.onOptionsItemSelected(item);
    }

    public boolean showToast(String message) {
        Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public void onConfigurationChanged(android.content.res.Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawListener.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawListener.syncState();
    }

    @Override
    public void onItemClick(AdapterView <? > arg0, View v, int position, long id) {
        selectitem(position);
        dLayout.closeDrawers();
        Bundle args = new Bundle();
        args.putString("Menu", menu[position]);
        Fragment detail = new DetailFragment();
        detail.setArguments(args);
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();
    }


    public void selectitem(int position) {
        dList.setItemChecked(position, true);
        setTitle(menu[position]);
    }

    public void setTitle(String title) {
        getSupportActionBar().setTitle(title);
    }

}

1 个答案:

答案 0 :(得分:0)

我看到你正在使用最新的AppCompat和东西。那为什么抽屉复杂化? 请完成this。你可以非常快速,轻松地制作抽屉,非常整齐地处理它的打开和关闭。你甚至不需要ActionBarDrawerToggle。

干杯。