如何在每个项目的列表视图中实现简单的按钮

Ama*_*jay 7 android listview android-intent android-button

在此输入图像描述

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

此外,所有条目必须使用json,使用cust_id,bus_id,Offer_id填充数据库; 我非常清楚.

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

我怎样才能做到这一点...?1.如何获得点击按钮的价值.2.如何将更改的颜色恢复为默认值; 一旦按钮被重新点击.

Plz建议我......

这是按钮代码

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;
            }
        });
Run Code Online (Sandbox Code Playgroud)

Lib*_*bin 2

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

这是实现您的场景的简单且最佳的方法。像这样为列表项中的按钮添加 onClick 侦听器。我已经解释了每一行..

    // 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
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

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

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