Recently this period of time has been looking at Android, using ListView to achieve the point of praise function, below to introduce the basic ideas.
Basic ideas:
Access to the interface – "Getting Data" –
Show in ListView-"
Save each row by clicking on the Map Collection (Position,boolean)-
Use entity classes to save objects-"
The Get/set method is worth changing accordingly-"
Click once, the corresponding quantity plus 1
Only achieved a point of praise function, tread and praise basic similar.
Specifically implemented as follows:
Inherit from 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{//used to get the elements of the interface private Viewholder holder; The Bean here is a test of the internal class, the point of praise and the number of steps to save, you can define a class,//direct access to data is good, need to improve the private list<bean> Praise_step_num = new arraylist<
Bean> ();
Private Layoutinflater Inflater; Save the current point of praise List<map<string,object>> ListItems; /* According to position, save the click of which line of data, the default is False, click to 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 () {/* is assigned a value for the corresponding data, all data is stored in listitems position one by one corresponds to each row of data * * for (int i = 0;i<listitems.size (); i++) {IsE
Xist.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 P
Osition; @Override public long getitemid (int position) {return POsition; @Override Public View getview (final int position, View Convertview, ViewGroup parent) {//Get praise, Step on value final bean bean = Prai
Se_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") + ""); /* Every time the Convertview, Convertview is not assigned value, will be used after the assignment of data, such as: Click on the first row of data Praise the button, the corresponding first line of praise picture into red when sliding down the page, there will beA change in the data, reused here for each reuse assignment */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,b Ean bean) {this.position = position; This.bean = bean;} @Override public void OnClick (final View v) {log.i ("position", PO
sition+ ""); 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.get Id () = = 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, "failure", Toast.length_long). Show ();
Break
}
}
}; New Thread () {@Override public void run () {msg = new message (); Msg.what=1 handler.sendmessage (msg);}}.
Start ();
}else{Toast.maketext (context, "already clicked", Toast.length_long). Show ();} private static class viewholder{public ImageView img_praise public ImageView getimg_praise () {return img_praise;} PU Blic 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;
int Getstep () {return step;} public void SetStep (int step) {this.step = step;} private int praise; private INT StEp }
}
Mainactivity
Assign a value to a ListView value, and then bind to and 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 = {"Commercial", "Home", "Test", "multi-Union", "Air can
"," commercial "," Home "," Test "," multi-Union "," Air energy "};
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,detailactivi
Ty.class);
StartActivity (Intent);
}
}); }
}
Jump interface, test with
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) { C10/>super.oncreate (savedinstancestate);
Setcontentview (R.layout.detail_info);
}
The realization of simple animation effect
Just use it right now.
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 ();
V.startanimation (ANIM);
}
Interface
<?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 Child Items
<?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= "commercial ManyOnline "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= "Time 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=" 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= "ten" android:textsize= "14dip" android:layout_gravity= "center"/> </linearlayout > </LinearLayout> </FrameLayout> </LinearLayout>
Enter the interface after clicking 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= "a"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "a"/> < TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "a"/> < TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "a"/> < TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "111"/> </LinearLayout>
In the code is my understanding of the program, some places may not be very clear, but also perfect
This article gave me a lot of help, animation effect is moved over, haha, very good, learning
Http://www.jb51.net/article/97335.htm
The above is a small set to introduce the Android ListView point of the realization of the function, hope to help everyone, if you have any questions welcome to my message, small series will promptly reply to everyone!