Android中Listview点赞功能的实现

时间:2021-05-20

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

基本思路:

进入界面–》获取数据–》
在Listview中显示–》
通过map集合(position,boolean)保存每一行是否被点击–》
利用实体类去保存相应的对象–》
get/set方法进行相应值得改变–》
点击一次,相应的数量加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;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() {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()+"");}}@Overridepublic int getCount() {return listItems.size() ;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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")+"");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;}@Overridepublic void onClick(final View v) {Log.i("position",position+"");if(bean.getPraise()==0){if(isExist.get(position) == false){final Handler handler = new Handler() {@Overridepublic 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(){@Overridepublic 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;@Overrideprotected 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() {@Overridepublic 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 {@Overrideprotected 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"?><RelativeLayoutxmlns: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"><ListViewandroid: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"><FrameLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" ><ImageViewandroid:id="@+id/has_exame"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="top|left"/><LinearLayoutandroid: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"><TextViewandroid:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="14sp"android:text="商用多联机"android:textColor="#000"/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><TextViewandroid: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"/><TextViewandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:gravity="center|right"><ImageViewandroid:id="@+id/tv_praises_img"android:layout_width="30dip"android:layout_height="30dip"android:background="@drawable/good_no"android:layout_marginRight="5dip"/><TextViewandroid: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"/><ImageViewandroid:id="@+id/tv_step_img"android:layout_width="30dip"android:layout_height="30dip"android:background="@drawable/bad_no"android:layout_marginRight="5dip"/><TextViewandroid: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"?><LinearLayoutxmlns: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="111"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="111"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="111"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="111"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="111"/></LinearLayout>

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

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

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章