iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android listview点赞问题分析
  • 770
分享到

Android listview点赞问题分析

listviewAndroid 2022-06-06 06:06:10 770人浏览 薄情痞子
摘要

最近这段时间一直在看Android,利用Listview去实现点赞功能 基本思路: 进入界面–》获取数据–》 在Listview中显示–》 通过map集合(positi

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

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()+"");
 }
 }
 @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")+"");
  
 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>

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

参考文章://www.jb51.net/article/97335.htm

您可能感兴趣的文章:Android自定义FloatingText仿点赞+1特效Android使用SurfaceView实现飘赞动画Android仿直播特效之点赞飘心效果Android贝塞尔曲线实现直播点赞效果Android实现朋友圈点赞列表Android自定义ViewGroup实现堆叠头像的点赞LayoutAndroid中使用PopupWindow 仿微信点赞和评论弹出Android实现点赞动画(27)Android PraiseTextView实现朋友圈点赞功能Android中Listview点赞功能的实现简单实用的Android UI微博动态点赞效果Android项目开发 教你实现Periscope点赞效果Android控件实现直播App特效之点赞飘心动画


--结束END--

本文标题: Android listview点赞问题分析

本文链接: https://www.lsjlt.com/news/23286.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android anr问题分析
    前言 本文主要介绍anr问题一手分析、分类判断,归类后提case给平台处理。 不是针对应用开发的anr分析和优化处理。 anr问题分类 anr问题主要分为 1、input 无焦点anr Reason:...
    99+
    2023-09-01
    android
  • Android 中ScrollView与ListView冲突问题的解决办法
    Android 中ScrollView与ListView冲突问题的解决办法自定义MyListViewpublic class MyListView extends ListView { public MyListView(Context...
    99+
    2023-05-30
    android scrollview listview
  • Android 黑屏问题分析处理总结
    介绍 黑屏问题是显示相关的综合性问题,涉及Android应用层、框架层和底层SurfaceFlinger、屏显等多个领域。下面有一些基础的判断来定位黑屏问题的归属: (1) 屏幕没有亮屏、背光为0则需先从power、屏显角度分析 (2) 屏...
    99+
    2023-08-16
    android
  • 解析Android ANR问题
    目录一、ANR介绍1.1、ANR类型1.1.1、KeyDispatchTimeout1.1.2、BroadcastTimeout1.1.3、ServiceTimeout&nb...
    99+
    2024-04-02
  • Android Navigation重建Fragment问题分析及解决
    目录前言Navigation源码分析1. NavHostFragment#onInflate2. NavHostFragment#onAttach3. NavHostFragment...
    99+
    2024-04-02
  • Android中怎么解决 ListView与getView调用卡顿问题
    本篇文章给大家分享的是有关Android中怎么解决 ListView与getView调用卡顿问题,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Android ListView与...
    99+
    2023-05-30
    android listview getview
  • Android性能优化之ANR问题定位分析
    目录前言1 ANR原因总结1.1 KeyDispatchTimeout1.2 BroadCastTimeout1.3 ServiceTimeout1.4 ContentProvide...
    99+
    2024-04-02
  • Python伪代码分析点赞器实现原理及代码
    目录前言一、简介1.适用场景2.核心逻辑二、代码实现1.模拟登录2.点赞接口分析3.点赞器伪代码实现三、总结前言 许多社区类平台都具备点赞功能,应运而生的就是自动点赞器,今天用Pyt...
    99+
    2024-04-02
  • 如何解决Android ScrollView下嵌套ListView或GridView出现的问题
    这篇文章主要为大家展示了“如何解决Android ScrollView下嵌套ListView或GridView出现的问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何解决Android Sc...
    99+
    2023-05-30
    android scrollview
  • android如何实现仿即刻点赞文字部分的自定义View
    小编给大家分享一下android如何实现仿即刻点赞文字部分的自定义View,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!概述:在学习HenCoder的过程中,有一...
    99+
    2023-05-30
    android
  • 分析web缓冲问题
    这篇文章主要介绍“分析web缓冲问题”,在日常操作中,相信很多人在分析web缓冲问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”分析web缓冲问题”的疑惑有所帮助!接下来,...
    99+
    2024-04-02
  • expdpnf导出问题分析
    这篇文章主要讲解了“expdpnf导出问题分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“expdpnf导出问题分析”吧!中止僵死的expdp任务--db...
    99+
    2024-04-02
  • SpringCloud问题实例分析
    这篇“SpringCloud问题实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringCloud问题实例分析”文...
    99+
    2023-06-29
  • Hibernate3.1问题举例分析
    本篇内容主要讲解“Hibernate3.1问题举例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Hibernate3.1问题举例分析”吧!今天在运行一个很简单的save()方法报:Excep...
    99+
    2023-06-17
  • 多面分析HarmonyOS与Android的特点
    目录一、前言二、HarmonyOS与Android的对比2.1 HarmonyOS并不是Android的替代品2.2 系统定位2.3 内核对比2.4 运行速度三、方舟编译器一、前言 ...
    99+
    2024-04-02
  • PHP浮点数运算问题分析与解决方案
    PHP是一种广泛应用于网站开发的脚本语言,其强大的功能和灵活性使得它成为许多开发者的首选工具。然而,在处理浮点数运算时,PHP也存在一些问题,特别是在涉及到精度和精确性要求较高的情况下...
    99+
    2024-02-27
    php 运算 浮点数 格式化输出
  • Android集成GoogleCast异常问题解析
    目录GoogleCast 异常问题需求Fragment/Activity 启动时查询Available Cast DevicesFragment/Activity 处于前台时,动态获...
    99+
    2023-02-10
    Android集成Google Cast异常 Android 集成异常
  • 解决vue-seamless-scroll滚动加点赞衔接处数据不同步问题
    VUE使用vue-seamless-scroll自动滚动加点赞,因为有两个overhidden导致点击不同同步dom,在代码中会出现两处vue-seamless-scroll上下悬接...
    99+
    2024-04-02
  • mysqldump问题的示例分析
    mysqldump问题的示例分析,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。导出:mysqldump数据库[表]>/t...
    99+
    2024-04-02
  • java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver报错问题分析
    java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver 这个错误通常意味着应用程序服务器在尝试在只读模式下访问数据库时出现了问题,该错误与事务处理有关。通常出现在JDBC连...
    99+
    2023-08-19
    mysql java 数据库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作