简体   繁体   English

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

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

在此输入图像描述

I have certain entries in my list view item. 我的列表视图项中有一些条目。 There I have a simple "like button" (not facebook like button). 在那里,我有一个简单的“喜欢按钮”(不像Facebook按钮)。 You can see the above mentioned SCREENSHOT; 你可以看到上面提到的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. 当我再次登录时,我希望更改类似按钮的颜色,并且类似按钮的颜色应该保持不变( 更改类似 )。

Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; 此外,所有条目必须使用json,使用cust_id,bus_id,Offer_id填充数据库; 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. 必须将其更改回默认颜色,并且必须从数据库中删除数据。

How can I do this...? 我怎样才能做到这一点...? 1. How to get value of click button. 1.如何获得点击按钮的价值。 2. How to bring back the changed color to default; 2.如何将更改的颜色恢复为默认值; once the button has been re-clicked. 一旦按钮被重新点击。

Plz suggest me... Plz建议我......

this is button code 这是按钮代码

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;
            }
        });

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. 您需要为按钮添加一个监听器,并使用ValueAnimator,您可以更改按钮颜色,并在再次单击时将其反转。

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 .. 为列表项中的按钮添加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
            }
        }
    });

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

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 , 具有多个可单击按钮的ListView元素

Using lists in Android (ListView) - Tutorial 使用Android中的列表(ListView) - 教程

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. 您可以在自定义适配器的getView()方法中添加一个setOnClickListener(),用于您正在使用的按钮。

Try following: 试试以下:

  1. use setOnClickListener() on the button . button上使用setOnClickListener()

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. 假设您在登录时从数据库中获取数据,您可以检查likestatus并相应地设置按钮的颜色。

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

相关问题 如何实现Android多项目列表视图(如iOS UITableView) - How to implement Android Multi-item listview like iOS UITableView 如何在每个列表视图的项目中更新按钮的文本?(Android) - How to update a button's text in each listview's item ?(Android) 如何在列表视图的每个项目上分别保存切换按钮状态 - How to save toggle button state separately on each item of listview 如何通过单击每个按钮将多个项目添加到ListView - How to add multiple item to a ListView with each Button click 替换ListView项目,每按一个按钮 - Replace ListView Item each press a Button 在自定义适配器中为listView中的每个项目添加按钮 - Adding button in custom adapter for each item in listView 如何在 ListView 中实现呼叫按钮? - How to to implement a call button in the ListView? 在Android中的每个列表项中单击删除按钮时如何删除列表视图项 - How to delete listview item when click delete button in each List item in android 如何实现自动滚动水平listView(或视图)在每个项目(或视图)上支持onClick()(或onItemClick()) - How to implement autoscroll horizontal listView (or view) supports onClick() (or onItemClick()) on each item (or view) 如何使用listview实现一个简单的待办事项列表? - How to implement a simple to do list using a listview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM