how to implement simple like button in listview of each item

做~自己de王妃 提交于 2019-12-05 04:54:13

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.

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.

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

                }
            }

you can use custom adapter for your listview(it has own layout.xml),and you can set your clicklistener in it.

You can change color or what you want. Actually I did have project like you want.I put some link if you can t do it.

Try this link:

ListView elements with multiple clickable buttons,

Using lists in Android (ListView) - Tutorial

The solution to this is actually easier than I thought. You can simply add in your custom adapter's getView() method a setOnClickListener() for the buttons you're using.

Try following:

  1. use setOnClickListener() on the button.

eg.

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. Assuming you are fetching the data from db when you login, you can check the likestatus and set the color of button accordingly.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!