我尝试在ActionBar
上的图标上单击时执行某些操作,但没有反应。我还想在单击操作栏中的图标时打开drawer_layout。如何使其可点击并处理其按下事件?
public class MainActivity extends AppCompatActivity {
ViewPager viewPager,viewPager01;
CustomSwipAdapter swip_adapter;
CustomSwipAdapter01 swip_adapter01;
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
getSupportActionBar().setLogo(R.drawable.sample_01);
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("");
}
catch (Exception e) {
}
viewPager = (ViewPager)findViewById(R.id.viewpager);
swip_adapter =new CustomSwipAdapter(this);
viewPager.setAdapter(swip_adapter);
/* viewPager01 = (ViewPager)findViewById(R.id.viewpager01);
swip_adapter01 =new CustomSwipAdapter01(this);
viewPager01.setAdapter(swip_adapter01);*/
menu = new String[]{"Home","E-Gift Voucher"};
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.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
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();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
/* getMenuInflater().inflate(R.menu.menu_main, menu);
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.main_activity_action,menu);
return super.onCreateOptionsMenu(menu);*/
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.main_activity_action,menu);
return true;//super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
switch (item.getItemId()){
case android.R.id.home:
Toast.makeText(MainActivity.this,"Click on Logo",Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}