繁体   English   中英

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

[英]Check Image item menu in fragment

我要单击项目菜单,该项目将更改图标。

该项目的选择器:

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"/>
menu.getMenuItem(position).setIcon(drwable);
<item android:icon="@drawable/Selector for the item"/>

快乐编码!

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

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

希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM