Android listview点赞问题分析
最近这段时间一直在看Android,利用Listview去实现点赞功能
基本思路:
进入界面–》获取数据–》
在Listview中显示–》
通过map集合(position,boolean)保存每一行是否被点击–》
利用实体类去保存相应的对象–》
get/set方法进行相应值得改变–》
点击一次,相应的数量加1
只实现了点赞功能,踩和赞基本类似。
具体实现如下:
继承自BaseAdapter
packagecom.gz.test_listview; importandroid.content.Context; importandroid.content.DialogInterface; importandroid.content.Intent; importandroid.os.Handler; importandroid.os.Message; importandroid.util.Log; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.AdapterView; importandroid.widget.BaseAdapter; importandroid.widget.Button; importandroid.widget.ImageView; importandroid.widget.ListView; importandroid.widget.TextView; importandroid.widget.Toast; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; /** *Createdbygzon2016/11/9. */ publicclassMainActivityAdapterextendsBaseAdapter{ //用来获取界面上的元素 privateViewHolderholder; privateContextcontext; //这里的Bean是个测试用的内部类,将点赞数和踩得数存起来,可以定义一个类, //直接获取数据就好,需要改进下 privateList<Bean>praise_step_num=newArrayList<Bean>(); privateLayoutInflaterinflater; //保存当前点赞数 List<Map<String,Object>>listItems; /* 根据position,保存点击的是哪一行数据,默认为false,点击后改为true */ privateMap<Integer,Boolean>isExist=newHashMap<Integer,Boolean>(); publicMainActivityAdapter(Contextcontext,List<Map<String,Object>>listItems){ this.context=context; this.listItems=listItems; inflater=LayoutInflater.from(context); Log.i("listItem",listItems.toString()); init(); } privatevoidinit(){ /* 为相应的数据赋值,所有的数据存储在listItems中 position一一对应每一行数据 */ for(inti=0;i<listItems.size();i++){ isExist.put(i,false); Beanb=newBean(); Integerpraise=Integer.parseInt(listItems.get(i).get("praise").toString()); b.setPraise(praise); Integerstep=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 publicintgetCount(){ returnlistItems.size(); } @Override publicObjectgetItem(intposition){ returnposition; } @Override publiclonggetItemId(intposition){ returnposition; } @Override publicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){ //获取赞、踩得值 finalBeanbean=praise_step_num.get(position); if(convertView==null){ holder=newViewHolder(); 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(newimgClick(position,bean)); returnconvertView; } classimgClickimplementsView.OnClickListener{ privateintposition; privateBeanbean; publicimgClick(intposition,Beanbean){ this.position=position; this.bean=bean; } @Override publicvoidonClick(finalViewv){ Log.i("position",position+""); if(bean.getPraise()==0){ if(isExist.get(position)==false){ finalHandlerhandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ super.handleMessage(msg); switch(msg.what){ case1: ImageViewbtn=(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; } case2: Toast.makeText(context,"失败",Toast.LENGTH_LONG).show(); break; } } }; newThread(){ @Override publicvoidrun(){ Messagemsg=newMessage(); msg.what=1; handler.sendMessage(msg); } }.start(); } }else{ Toast.makeText(context,"已经点击过",Toast.LENGTH_LONG).show(); } } } privatestaticclassViewHolder{ publicImageViewimg_praise; publicImageViewgetImg_praise(){ returnimg_praise; } publicImageViewgetImg_step(){ returnimg_step; } publicTextViewgetPraise(){ returnpraise; } publicTextViewgetStep(){ returnstep; } publicTextViewgetTitle(){ returntitle; } privateImageViewimg_step; publicTextViewpraise; privateTextViewstep; privateTextViewtitle; } classBean{ publicintgetPraise(){ returnpraise; } publicvoidsetPraise(intpraise){ this.praise=praise; } publicintgetStep(){ returnstep; } publicvoidsetStep(intstep){ this.step=step; } privateintpraise; privateintstep; } }
MainActivity
进行Listview值赋值,然后进行和Adapter的绑定
packagecom.gz.test_listview; importandroid.app.Activity; importandroid.content.Intent; importandroid.support.v7.app.AppCompatActivity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.AdapterView; importandroid.widget.ListView; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importjava.util.Objects; publicclassMainActivityextendsActivity{ privateListViewlistview; privateString[]title={"商用","家用","测试","多联","空气能","商用","家用","测试","多联","空气能"}; privateString[]praise={"3","1","0","0","0","3","1","0","0","0"}; privateString[]step={"3","0","0","0","0","3","0","0","0","0"}; privateArrayList<Map<String,Object>>map=newArrayList<Map<String,Object>>(); privateListViewlistView; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview=(ListView)findViewById(R.id.listview); for(inti=0;i<title.length;i++){ Map<String,Object>item=newHashMap<String,Object>(); item.put("title",title[i]); item.put("praise",praise[i]); item.put("step",step[i]); map.add(item); } MainActivityAdapteradapter=newMainActivityAdapter(this,map); listview.setAdapter(adapter); listview.setOnItemClickListener(newAdapterView.OnItemClickListener(){ @Override publicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,longid){ Intentintent=newIntent(MainActivity.this,DetailActivity.class); startActivity(intent); } }); } }
跳转界面,测试用
packagecom.gz.test_listview; importandroid.app.Activity; importandroid.os.Bundle; importandroid.widget.ListView; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.Map; publicclassDetailActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.detail_info); } }
简单的动画效果的实现
直接用就好了
packagecom.gz.test_listview; importandroid.view.View; importandroid.view.animation.Animation; importandroid.view.animation.ScaleAnimation; publicclassAnimationTools{ publicstaticvoidscale(Viewv){ ScaleAnimationanim=newScaleAnimation(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); } }
界面
<?xmlversion="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子项
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns: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后进入的界面
<?xmlversion="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>
代码中是我对程序的理解,有的地方可能不是很明白,还得完善完善
这篇文章给了我很大帮助,动画效果就是搬过来的,哈哈,很不错,学习了。
参考文章:https://www.nhooo.com/article/97335.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。