2016-09-30 11 views
0

アイテムメニューをクリックしたいと思っています。このアイテムはアイコンを変更します。項目のフラグメントの画像アイテムメニューを確認

セレクター:

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"/> 

答えて

0
menu.getMenuItem(position).setIcon(drwable); 
0
<item android:icon="@drawable/Selector for the item"/> 

ハッピーコーディング!

+0

これは以前使用していましたが、機能しません。 "button_add_to_wishlist"はセレクタで、meni itemのアイコンです。 – rkovtiuk

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; 
} 

私はこれがあなたを助けてくれることを願っています!

関連する問題