网上有很多点赞效果的单独实现,那么问题来了, 我们一般都是列表展示一个物品,我们点赞button一般就是在列表里面,所以需要在Adapter里面的getView里面写功能实现。
废话不说,直接代码:
listview的布局文件中,赞点赞的button下面隐藏着+1:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/relativeLayout1" >
<Button
android:id="@+id/bt_one"
android:layout_width="40dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="赞"
android:textColor="#000000"
android:textSize="18dp"
/>
<TextView
android:id="@+id/tv_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+1"
android:layout_centerInParent="true"
android:visibility="gone"
android:gravity="center"
android:textSize="18dp"
android:textColor="#000000"
/>
</RelativeLayout>
下面是Adapter文件:其余的省略,主要是getView片段:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// animation=AnimationUtils.loadAnimation(context,R.anim.nn);
final ObjectEntity objentity=hotobjList.get(position);
ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.listitem_hotobj, null);
holder.starn= (TextView) convertView.findViewById(R.id.starname);
holder.objt = (TextView) convertView.findViewById(R.id.objecttype);
holder.objpopty=(TextView) convertView.findViewById(R.id.objectpopty);
holder.objpic=(ImageView)convertView.findViewById(R.id.objpic);
holder.zanbt=(Button)convertView.findViewById(R.id.bt_one);
holder.plus1=(TextView)convertView.findViewById(R.id.tv_one);
convertView.setTag(holder);
}else
{
holder = (ViewHolder)convertView.getTag();
}
holder.starn.setText(objentity.getStar_Name());
holder.objt.setText(objentity.getObject_Type());
int i=objentity.getObject_Popty();
holder.objpopty.setText(Integer.toString(i));
holder.objpic.setImageDrawable(objentity.getObject_pic());
holder.zanbt.setTag(position);
//给Button添加单击事件 添加Button之后ListView将失去焦点 需要的直接把Button的焦点去掉
//holder.zanbt.setOnClickListener( myListener);
final TextView tv=holder.plus1;
final TextView tv2=holder.objpopty;
holder.zanbt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
/* holder.plus1.setVisibility(View.VISIBLE);
ObjectAnimator oa=ObjectAnimator.ofFloat(holder.plus1, "translate", 1.0f, 2f);
oa.setDuration(3000);
oa.start();
holder.plus1.setVisibility(View.GONE);*/
tv.setVisibility(View.VISIBLE);
//tv2.setText("100");
System.out.println("__animation1");
tv.startAnimation(animation);
System.out.println("__animation2");
new Handler().postDelayed(new Runnable(){
public void run() {
//holder.plus1.clearAnimation();
tv.setVisibility(View.GONE);
tv2.setText("100");
objentity.setObject_Popty(100);
}
}, 1000);
}
});
return convertView;
}
}
主要注意OnClicListener事件
最后附上动画的nn文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--
<scale
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="0%"
android:duration="1000" />
-->
<translate
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="-50"
android:duration="1000"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.3"
android:duration="1000"/>
</set>