繁体   English   中英

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

[英]how to implement simple like button in listview of each item

在此输入图像描述

我的列表视图项中有一些条目。 在那里,我有一个简单的“喜欢按钮”(不像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;
            }
        });

您需要为按钮添加一个监听器,并使用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
            }
        }
    });

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

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

            @Override
            public void onClick(View view) {
                if(!check)
                {

                    personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup_blue);
                    check = true;
                }
                else
                {
                    personViewHolder.img_like_job.setImageResource(R.drawable.ic_thumbsup);
                    check = false;

                }
            }

您可以使用自定义适配器作为列表视图(它有自己的layout.xml),您可以在其中设置clicklistener。

你可以改变颜色或你想要的。 实际上我确实有你想要的项目。如果你不能这样做,我会提供一些链接。

试试这个链接:

具有多个可单击按钮的ListView元素

使用Android中的列表(ListView) - 教程

对此的解决方案实际上比我想象的要容易。 您可以在自定义适配器的getView()方法中添加一个setOnClickListener(),用于您正在使用的按钮。

试试以下:

  1. button上使用setOnClickListener()

例如。

viewHolder.imgVwFbLike.setOnClickListener(new View.OnClickListener() { 
   @Override public void onClick(View v) {
         // TODO :
         // 1. make webservice call to update like status (Assuming a web service call)
         // 2. Implement a callback for webservice call, to get the status of request.
            if(success) 
             a) change the colour of like btn. and insert the data in Db.
             b) Also maintain a column in db for likestatus(by default set it false).                 
            } 
        }
   );
  1. 假设您在登录时从数据库中获取数据,您可以检查likestatus并相应地设置按钮的颜色。

暂无
暂无

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

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