Android中Listview点赞功能的实现

最近这段时间一直在看Android,利用Listview去实现点赞功能,下面给大家介绍下基本思路。

基本思路:

进入界面C》获取数据C》
在Listview中显示C》
通过map集合(position,boolean)保存每一行是否被点击C》
利用实体类去保存相应的对象C》
get/set方法进行相应值得改变C》
点击一次,相应的数量加1

只实现了点赞功能,踩和赞基本类似。

具体实现如下:

继承自BaseAdapter

package com.gz.test_listview;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by gz on 2016/11/9.
*/
public class MainActivityAdapter extends BaseAdapter{
//用来获取界面上的元素
private ViewHolder holder;
private Context context;
//这里的Bean是个测试用的内部类,将点赞数和踩得数存起来,可以定义一个类,
//直接获取数据就好,需要改进下
private List<Bean> praise_step_num = new ArrayList<Bean>();
private LayoutInflater inflater;
//保存当前点赞数
List<Map<String,Object>> listItems;
/*
根据position,保存点击的是哪一行数据,默认为false,点击后改为true
*/
private Map<Integer, Boolean> isExist = new HashMap<Integer, Boolean>();
public MainActivityAdapter(Context context,List<Map<String,Object>> listItems){
this.context = context;
this.listItems = listItems;
inflater = LayoutInflater.from(context);
Log.i("listItem",listItems.toString());
init();
}
private void init() {
/*
为相应的数据赋值,所有的数据存储在listItems中
position一一对应每一行数据
*/
for (int i = 0;i<listItems.size();i++){
isExist.put(i,false);
Bean b = new Bean();
Integer praise = Integer.parseInt(listItems.get(i).get("praise").toString());
b.setPraise(praise);
Integer step = Integer.parseInt(listItems.get(i).get("step").toString());
b.setStep(step);
praise_step_num.add(i,b);
Log.i("praise_step",praise_step_num.get(i).getPraise()+"");
}
}
@Override
public int getCount() {
return listItems.size() ;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//获取赞、踩得值
final Bean bean = praise_step_num.get(position);
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.item_praise,null);
holder.img_praise = (ImageView) convertView.findViewById(R.id.tv_praises_img);
holder.img_step = (ImageView) convertView.findViewById(R.id.tv_step_img);
holder.praise = (TextView) convertView.findViewById(R.id.tv_praises);
holder.step = (TextView) convertView.findViewById(R.id.tv_step);
holder.title = (TextView) convertView.findViewById(R.id.tv_name);
//holder.img_step.setImageResource(R.drawable.bad);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.praise.setText(bean.getPraise()+"");
holder.step.setText(bean.getStep()+"");
holder.title.setText(listItems.get(position).get("title")+"");
/*
每次会复用convertView,
convertView没有被赋值,就会使用之后赋值的数据,
比如:点击了第一行数据赞的按钮,相应的第一行点赞图片变为红色
当向下滑动页面时,下方的也会有一项数据发生变化,复用
在这里为每次重用赋值
*/
if(bean.getPraise()!=0){
holder.img_praise.setImageResource(R.drawable.good);
}else{
holder.img_praise.setImageResource(R.drawable.good_no);
}
if(bean.getStep()!=0){
holder.img_step.setImageResource(R.drawable.bad);
}else{
holder.img_step.setImageResource(R.drawable.bad_no);
}
holder.img_praise.setOnClickListener(new imgClick(position,bean));
return convertView;
}
class imgClick implements View.OnClickListener {
private int position;
private Bean bean;
public imgClick(int position,Bean bean){
this.position = position;
this.bean = bean;
}
@Override
public void onClick(final View v) {
Log.i("position",position+"");
if(bean.getPraise()==0){
if(isExist.get(position) == false){
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
ImageView btn = (ImageView) v;
if(v.getId() == btn.getId()) {
isExist.put(position, true);
bean.setPraise(bean.getPraise()+1);
btn.setImageResource(R.drawable.good);
AnimationTools.scale(btn);
notifyDataSetChanged();
break;
}
case 2:
Toast.makeText(context, "失败", Toast.LENGTH_LONG).show();
break;
}
}
};
new Thread(){
@Override
public void run() {
Message msg = new Message();
msg.what=1;
handler.sendMessage(msg);
}
}.start();
}
}else{
Toast.makeText(context, "已经点击过", Toast.LENGTH_LONG).show();
}
}
}
private static class ViewHolder{
public ImageView img_praise;
public ImageView getImg_praise() {
return img_praise;
}
public ImageView getImg_step() {
return img_step;
}
public TextView getPraise() {
return praise;
}
public TextView getStep() {
return step;
}
public TextView getTitle() {
return title;
}
private ImageView img_step;
public TextView praise;
private TextView step;
private TextView title;
}
class Bean{
public int getPraise() {
return praise;
}
public void setPraise(int praise) {
this.praise = praise;
}
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
private int praise;
private int step;
}
}

MainActivity

进行Listview值赋值,然后进行和Adapter的绑定

package com.gz.test_listview;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class MainActivity extends Activity {
private ListView listview;
private String[] title = {"商用","家用","测试","多联","空气能","商用","家用","测试","多联","空气能"};
private String[] praise = {"3","1","0","0","0","3","1","0","0","0"};
private String[] step = {"3","0","0","0","0","3","0","0","0","0"};
private ArrayList<Map<String,Object>> map = new ArrayList<Map<String, Object>>();
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
for(int i=0;i<title.length;i++){
Map<String,Object> item = new HashMap<String,Object>();
item.put("title",title[i]);
item.put("praise",praise[i]);
item.put("step",step[i]);
map.add(item);
}
MainActivityAdapter adapter = new MainActivityAdapter(this,map);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,DetailActivity.class);
startActivity(intent);
}
});
}
}

跳转界面,测试用

package com.gz.test_listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class DetailActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_info);
}
}

简单的动画效果的实现

直接用就好了

package com.gz.test_listview;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
public class AnimationTools {
public static void scale(View v) {
ScaleAnimation anim = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setDuration(300);
v.startAnimation(anim);
}
}

界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.gz.test_listview.MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>

Listview子项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lyt_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
android:orientation="vertical"
android:descendantFocusability= "blocksDescendants"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/has_exame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
/>
<LinearLayout
android:layout_margin="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/corners"
android:layout_weight="1.0"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="商用多联机"
android:textColor="#000"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_date"
android:layout_marginTop="10dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="时间 2016-10-10"
android:textColor="#000"
android:visibility="visible"
/>
<TextView
android:id="@+id/tv_downloads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:textColor="#000"
android:textSize="14sp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center|right"
>
<ImageView
android:id="@+id/tv_praises_img"
android:layout_width="30dip"
android:layout_height="30dip"
android:background="@drawable/good_no"
android:layout_marginRight="5dip"
/>
<TextView
android:id="@+id/tv_praises"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="10"
android:textSize="14dip"
android:layout_gravity="center"
/>
<ImageView
android:id="@+id/tv_step_img"
android:layout_width="30dip"
android:layout_height="30dip"
android:background="@drawable/bad_no"
android:layout_marginRight="5dip"
/>
<TextView
android:id="@+id/tv_step"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="10"
android:textSize="14dip"
android:layout_gravity="center"
/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>

点击item后进入的界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111"
/>
</LinearLayout>

代码中是我对程序的理解,有的地方可能不是很明白,还得完善完善

这篇文章给了我很大帮助,动画效果就是搬过来的,哈哈,很不错,学习了

http://www.jb51.net/article/97335.htm

以上所述是小编给大家介绍的Android中Listview点赞功能的实现,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

代码技巧

转载请关注公众号:代码技巧 回复:授权

本文链接地址:https://www.oudahe.com/p/49576/