Recently, I have been watching Android and using Listview to implement the like function
Basic ideas:
Enter the interface –》Get data –》
Show in Listview –》
Save whether each line is clicked through map collection (position,boolean)-。
Use entity classes to save corresponding objects –》
The get/set method is worth changing accordingly –
Click once, add 1 for the corresponding number
Only implementedLike function,Step onandpraiseBasically similar.
The specific implementation is as follows:
Inherited from BaseAdapter
package .test_listview; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by gz on 2016/11/9. */ public class MainActivityAdapter extends BaseAdapter{ // Used to obtain elements on the interface private ViewHolder holder; private Context context; //The bean here is an internal class for testing. Save the likes and the number of steps. You can define a class. //Just just get the data directly, need to be improved private List<Bean> praise_step_num = new ArrayList<Bean>(); private LayoutInflater inflater; //Save the current number of likes List<Map<String,Object>> listItems; /* According to position, which row of data is saved, the default is false, and change it to true after clicking */ private Map<Integer, Boolean> isExist = new HashMap<Integer, Boolean>(); public MainActivityAdapter(Context context,List<Map<String,Object>> listItems){ = context; = listItems; inflater = (context); ("listItem",()); init(); } private void init() { /* Assign values to the corresponding data, all data is stored in listItems position corresponds to each row of data one by one */ for (int i = 0;i<();i++){ (i,false); Bean b = new Bean(); Integer praise = ((i).get("praise").toString()); (praise); Integer step = ((i).get("step").toString()); (step); praise_step_num.add(i,b); ("praise_step",praise_step_num.get(i).getPraise()+""); } } @Override public int getCount() { return () ; } @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) { //Get likes, step on worth final Bean bean = praise_step_num.get(position); if(convertView == null){ holder = new ViewHolder(); convertView = (.item_praise,null); holder.img_praise = (ImageView) (.tv_praises_img); holder.img_step = (ImageView) (.tv_step_img); = (TextView) (.tv_praises); = (TextView) (.tv_step); = (TextView) (.tv_name); //holder.img_step.setImageResource(); (holder); }else{ holder = (ViewHolder) (); } (()+""); (()+""); ((position).get("title")+""); /* convertView will be reused every time, The convertView is not assigned, and the data assigned later will be used. For example: Click the button for the first line of data like, and the corresponding first line of like picture becomes red When sliding down the page, there will also be a data below to change. Assign values to each reuse here */ if(()!=0){ holder.img_praise.setImageResource(); }else{ holder.img_praise.setImageResource(.good_no); } if(()!=0){ holder.img_step.setImageResource(); }else{ holder.img_step.setImageResource(.bad_no); } holder.img_praise.setOnClickListener(new imgClick(position,bean)); return convertView; } class imgClick implements { private int position; private Bean bean; public imgClick(int position,Bean bean){ = position; = bean; } @Override public void onClick(final View v) { ("position",position+""); if(()==0){ if((position) == false){ final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { (msg); switch (){ case 1: ImageView btn = (ImageView) v; if(() == ()) { (position, true); (()+1); (); (btn); notifyDataSetChanged(); break; } case 2: (context, "fail", Toast.LENGTH_LONG).show(); break; } } }; new Thread(){ @Override public void run() { Message msg = new Message(); =1; (msg); } }.start(); } }else{ (context, "Clicked", 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) { = praise; } public int getStep() { return step; } public void setStep(int step) { = step; } private int praise; private int step; } }
MainActivity
Assign Listview value, and then bind to Adapter
package .test_listview; import ; import ; import .; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private ListView listview; private String[] title = {"Commercial","Home use","test","Multiple connections","Air Energy","Commercial","Home use","test","Multiple connections","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) { (savedInstanceState); setContentView(.activity_main); listview = (ListView) findViewById(); for(int i=0;i<;i++){ Map<String,Object> item = new HashMap<String,Object>(); ("title",title[i]); ("praise",praise[i]); ("step",step[i]); (item); } MainActivityAdapter adapter = new MainActivityAdapter(this,map); (adapter); (new () { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(,); startActivity(intent); } }); } }
Jump interface, for testing
package .test_listview; import ; import ; import ; import ; import ; import ; public class DetailActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.detail_info); } }
Simple animation effectsImplementation
Just use it directly
package .test_listview; import ; import ; import ; 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); (300); (anim); } }
interface
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro xmlns:tools="/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=".test_listview.MainActivity"> <ListView android: android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </RelativeLayout>
Listview child
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android: 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: 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: android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="14sp" android:text="Commercial Multi-Online" android:textColor="#000" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android: 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: 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: android:layout_width="30dip" android:layout_height="30dip" android:background="@drawable/good_no" android:layout_marginRight="5dip" /> <TextView android: android:layout_width="50dip" android:layout_height="wrap_content" android:textColor="#000" android:text="10" android:textSize="14dip" android:layout_gravity="center" /> <ImageView android: android:layout_width="30dip" android:layout_height="30dip" android:background="@drawable/bad_no" android:layout_marginRight="5dip" /> <TextView android: 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>
The interface you enter after clicking on item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro 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>
The code is my understanding of the program. Some places may not be very clear, but I still have to improve them.
This article has helped me a lot, and the animation effect is moved here, haha, it's very good, I've learned it.
Reference article:https:///article/
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.