检查片段中的图像项目菜单

时间:2016-09-30 09:49:01

标签: java android android-menu

我想点击项目菜单,此项目将更改图标。

项目的选择器:

button_add_to_wishlist.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_checked="false"
    android:drawable="@drawable/ic_add_fav" />
<item
    android:state_checked="true"
    android:drawable="@drawable/ic_add_fav_fill" />

菜单

<item
    android:id="@+id/add_to_wishlist"
    android:title="@string/book_btn_text_add_to_wishlist"
    android:icon="@drawable/button_add_to_wishlist"
    android:checkable="true"
    app:showAsAction="always"/>

3 个答案:

答案 0 :(得分:0)

menu.getMenuItem(position).setIcon(drwable);

答案 1 :(得分:0)

<item android:icon="@drawable/Selector for the item"/>

快乐编码!!

答案 2 :(得分:0)

如何在自定义适配器中设置自定义侦听器?像这样:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final Rowholder;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new Rowholder();
        holder.checkbox= (CheckBox) row.findViewById(R.id.myCheckbox);
        holder.icon= (ImageView) row.findViewById(R.id.myIcon);

        row.setTag(holder);
    }
    else
    {
        holder = (Rowholder)row.getTag();
    }
    holder.checkbox.setText("A Box");

    //here is the important part where we set the imageview's source
    //dependig on the checkbox state
    //a checkChangeListener would also do the deal
    holder.checkbox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(holder.checkBox.isChecked()) {
               holder.icon.setImageResource(R.drawable.ic_add_fav_fill);
            }else{
               holder.icon.setImageResource(R.drawable.ic_add_fav);
    });


    return row;
}

//holder class for your UI-elements
static class RowHolder  {
    CheckBox checkbox;
    ImageView icon;
}

我希望这可以帮到你!