[英]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: 试试以下:
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).
}
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.