【问题标题】:Puting the heart in the listView and changing heart's color by onclick将心脏放在 listView 中并通过 onclick 更改心脏的颜色
【发布时间】:2016-03-15 09:24:29
【问题描述】:

我想要一个没有颜色的心,当我点击它时,它的颜色变成红色并在退出应用时保存。

StructAnatomy 类:

public class StructAnatomy {

    public String title;

    //public ImageView heart_empty;

    //public boolean   done;
}

AdapterAnatomy 类:

heart_impty 是没有颜色的图像,而 heart_fill 是有颜色的。

public class AdapterAnatomy extends ArrayAdapter<StructAnatomy> {

    public AdapterAnatomy(ArrayList<StructAnatomy> array) {

        super(G.context, R.layout.adapter_anatomy, array);
    }
    public static class ViewHolder {

        public ViewGroup layoutRoot;
        public TextView  txtTitle;

        //public ImageView heart_empty;

         public ViewHolder(View view) {
            layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot);
            txtTitle = (TextView) view.findViewById(R.id.txtTitle);
            //heart_empty.setImageResource(R.drawable.heart_fill);
        }


        public void fill(ArrayAdapter<StructAnatomy> adapter, StructAnatomy item, final int position) {
            txtTitle.setText(item.title);
            //heart_empty.setEnabled(item.done);
            layoutRoot.setOnClickListener(new OnClickListener() {

             //******************************************************************************************
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(G.currentActivity, Anatomy1_2.class);
                    intent.putExtra("POSITION", position);
                    G.currentActivity.startActivity(intent);
                 }

            });
        }
     }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        StructAnatomy item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.adapter_anatomy, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.fill(this, item, position);
        return convertView;
    }
}

【问题讨论】:

  • 你能分享你所做的代码吗?一种方法可以放在用于每个行视图的 row.xml
  • 你有没有尝试过这样做?
  • 是的 // 我解释我的尝试
  • 你是否使用任何api来获取喜欢或不喜欢的数据?
  • 基本上,您需要一个自定义 CheckBox 或 RadioButton。谷歌。

标签: android xml android-layout listview android-studio


【解决方案1】:

要在点击时改变你的心的颜色,写

heartViww.setBackgroundColor(Color.parseColor("#ff0000"));

如果您想保存所选心脏的颜色,即使退出应用程序,您可以在 sharedpreference 中保存一个布尔值,然后在您的适配器构造函数返回应用程序时再次检查共享首选项中的布尔值。

【讨论】:

  • 我需要通过 onClick 改变心的颜色。
  • 所以在我的回答中我提到了为心设置颜色。
【解决方案2】:
  • 当您点击 listview getPosition(getChildAt(position)) 和 从中您可以设置颜色
  • 现在退出时,您需要将其保存到 sharedPreferences

前:

if(Your condition){    //if it is true

 img.setImageResource(R.drawable.my_first_image); //replace image

}else{                 

 img.setImageResource(R.drawable.my_second_image);

}

要更改列表视图项的颜色,您可以使用

view.setBackgroundColor(Color.parseColor("#08A3F5"));

详情请查看this link

【讨论】:

  • 我只需要通过点击 heart_empty 图像来改变心的颜色。
  • 我有点困惑,我应该先检查一下。我稍后再检查。我有两张心脏的照片,可以粘贴在一起。
  • 您希望它现在如何工作?当您单击它时,它会改变颜色,然后在退出时将其保存到 sharedpreference 中,这样当您打开应用程序时,它的状态就会恢复。它将与您在退出时所做的相同。
  • 我不想改变颜色,我想改变图像(我有两个心脏图像,第一个心脏没有颜色(灰色),第二个心脏有红色)。如上图所示。我刚开始编程,我复制了一些代码,但我并不流利。
  • 您需要检查条件,使用 if else 并根据需要替换图像。
【解决方案3】:

我使用这些代码并且我的目标很满意:

public void heart(int heartId, int listViewId) {
    final String NameId = listViewId + "";
    final ImageView heart = (ImageView) findViewById(heartId);

    if (AppPrefrances.getInstance(getApplicationContext()).getClicked(NameId).equals("1")) {
        heart.setImageResource(R.drawable.heart_fill);
    } else {
        heart.setImageResource(R.drawable.heart_empty);
    }

    heart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View V) {

            if (AppPrefrances.getInstance(getApplicationContext()).getClicked(NameId).equals("1")) {
                heart.setImageResource(R.drawable.heart_empty);
                AppPrefrances.getInstance(getApplicationContext()).setClicked(NameId, "0");
            } else {
                heart.setImageResource(R.drawable.heart_fill);
                AppPrefrances.getInstance(getApplicationContext()).setClicked(NameId, "1");
            }

        }
    });
}

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多