代码之家  ›  专栏  ›  技术社区  ›  rkovtiuk

检查片段中的图像项菜单

  •  0
  • rkovtiuk  · 技术社区  · 8 年前

    我想点击项目菜单,这个项目会改变图标。

    项目选择器:

    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 回复  |  直到 8 年前
        1
  •  0
  •   Piyush    8 年前
    menu.getMenuItem(position).setIcon(drwable);
    
        2
  •  0
  •   Mohit Trivedi    8 年前
    <item android:icon="@drawable/Selector for the item"/>
    

    快乐编码!!

        3
  •  0
  •   Pynnie    8 年前

    在自定义适配器中设置自定义侦听器怎么样?类似这样:

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

    我希望这可以帮助你!