自定义的一个菜单组件,想对里面的item设置click监听。
public class ResideMenuItem extends LinearLayout{}//自定义菜单项目
private ResideMenuItem itemMusic;
itemMusic = new ResideMenuItem(this,R.drawable.icon_home,"Music");
itemMusic.setOnClickListener(this);
因为项目比较多,所以想用switch()case来匹配,但一时间想不起来应该怎么设置匹配方式。
对于button这类有绑定view的可以使用view.getId()来匹配,但这种自定义的应该怎么设置呢。
demo里用的是if (view == itemMusic){}
,但switch好像不行呢。
高洛峰2017-04-17 17:22:24
View has setId(int id) method, such as
private ResideMenuItem itemMusic;
itemMusic = new ResideMenuItem(this,R.drawable.icon_home,"Music");
itemMusic.setId(0xff01);
itemMusic.setOnClickListener(this);
This way you can use view.getId() in switch to judge.
天蓬老师2017-04-17 17:22:24
Menu click event Override this method onOptionsItemSelected(MenuItem item) You can judge based on the itemId in the method.