欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android listview点赞问题分析

程序员文章站 2024-03-31 15:39:16
最近这段时间一直在看android,利用listview去实现点赞功能 基本思路: 进入界面–》获取数据–》 在listview中显示–》 通过map集合...

最近这段时间一直在看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;
 /*
 根据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>


代码中是我对程序的理解,有的地方可能不是很明白,还得完善完善
这篇文章给了我很大帮助,动画效果就是搬过来的,哈哈,很不错,学习了。

参考文章:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。