如何在每个项目的列表视图中实现简单的喜欢按钮 [英] how to implement simple like button in listview of each item

查看:101
本文介绍了如何在每个项目的列表视图中实现简单的喜欢按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表视图项中有某些条目.那里我有一个简单的喜欢按钮"(不是facebook喜欢按钮).您可以看到上面提到的SCREENSHOT;供参考. 我点击喜欢按钮的那一刻;我想更改类似按钮的颜色,并且当我再次登录时,类似按钮的颜色应保持不变(更改为类似).

I have certain entries in my list view item. There I have a simple "like button" (not facebook like button). You can see the above mentioned SCREENSHOT; for the reference. The moment I click on like button; i want the like button color to be changed and the like button color should remain same(changed on like) when I'll login again.

此外,所有条目都必须使用json在cust_id,bus_id,Offer_id中填充;我非常了解.

Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; that I know very well.

当我再次单击相同的按钮(如按钮)时,其颜色已更改.必须将其更改回默认颜色,并且必须从数据库中删除数据.

When I again click on the same button(like button), whose color has been changed. It must be changed back to the default color and data must get removed from database.

我该怎么做...? 1.如何获得点击按钮的价值. 2.如何将更改后的颜色恢复为默认值;重新单击按钮后.

How can I do this...? 1. How to get value of click button. 2. How to bring back the changed color to default; once the button has been re-clicked.

Plz建议我...

Plz suggest me...

这是按钮代码

holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clicked) {
                    holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
                } else {
                    holder.b1.setBackgroundResource(R.drawable.like_icon);
                }
                clicked = true;
            }
        });

推荐答案

您需要向按钮添加侦听器,并使用ValueAnimator更改按钮的颜色,并在再次单击时将其反转.

You need to add a listener to the button and using ValueAnimator you can change the button color and reverse it back when you click again.

这里是实现您的方案的一种简单而最佳的方法.像这样在列表项中添加按钮的onClick侦听器..我已经解释了每一行..

Here is a simple and best approach to achieve your scenario. Add the onClick listener for the button in your list item like this.. I have explained each line ..

    // set a default background color to the button
    placeHolder.likeButton.setBackgroundColor(Color.RED);
    placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
        ValueAnimator buttonColorAnim = null; // to hold the button animator

        @Override
        public void onClick(View v) {
            // first time this will be null
            if(buttonColorAnim != null){
                // reverse the color
                buttonColorAnim.reverse();
                // reset for next time click
                buttonColorAnim = null;
                // add your code here to remove from database
            }
            else {
                final Button button = (Button) v;
                // create a color value animator
                buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
                // add a update listener for the animator.
                buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        // set the background color
                        button.setBackgroundColor((Integer) animator.getAnimatedValue());
                    }
                });
                // you can also set a delay before start
                //buttonColorAnim.setStartDelay(2000); // 2 seconds
                // start the animator..
                buttonColorAnim.start();
                // add your code here to add to database
            }
        }
    });

这将在您第一次单击时更改按钮的颜色,然后在下次单击时将其恢复为原来的颜色.您还可以设置更改颜色的延迟时间.

This will change the button color on your first click and then revert the color back on the next click. You can also set a delay to change the color.

注意:您必须根据自己的逻辑设置默认按钮颜色.

Note: You have to set the default button color based on your logic.

这篇关于如何在每个项目的列表视图中实现简单的喜欢按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆