0
我爲NavigationDrawer
實施了ActionBarDrawerToggle
並且還使用了ActionBar
。
如何處理兩個點擊事件,因爲兩者都需要onOptionsItemSelected(MenuItem item)
方法?
有沒有其他解決方法?處理ActionBarDrawerToggle和ActionBar的點擊事件
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);
}
}
你想處理哪些點擊事件? – Suleiman19
我需要同時處理右上角的動作欄事件和左側的actionbartoggle,這裏是屏幕截圖:http://imgur.com/TI681pX – Kaijju