The QQ friends list slides to the left to display the top, delete -- third-party open source -- SwipeMenuListView, swipemenulistview

Source: Internet
Author: User

The QQ friends list slides to the left to display the top, delete -- third-party open source -- SwipeMenuListView, swipemenulistview

SwipeMenuListView is a third-party open source project on github. The link to this project on github is: https://github.com/baoyongzhang/SwipeMenuListView.

After downloading, copy and paste the project package to the desired project:

Test code:

Item. xml:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 6 <ImageView 7 android: id = "@ + id/imageView" 8 android: layout_width = "60dp" 9 android: layout_height = "60dp" 10 android: src = "@ drawable/ic_launcher"/> 11 12 <TextView13 android: id = "@ + id/textView" 14 android: layout_width = "wrap_content" 15 android: layout_height = "wrap_content" 16 android: layout_alignBottom = "@ + id/imageView" 17 android: layout_alignParentRight = "true" 18 android: layout_alignParentTop = "true" 19 android: layout_toRightOf = "@ + id/imageView" 20 android: gravity = "center" 21 android: textSize = "20sp"/> 22 23 </RelativeLayout>Item. xml

Activity_main.xml:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" > 5  6     <com.baoyz.swipemenulistview.SwipeMenuListView 7         android:id="@+id/listView" 8         android:layout_width="match_parent" 9         android:layout_height="match_parent" />10 11 </LinearLayout>

MainActivity. java:

1 package com. zzw. testswipemenulistview; 2 3 import java. util. arrayList; 4 5 import com. baoyz. swipemenulistview. swipeMenu; 6 import com. baoyz. swipemenulistview. swipeMenuCreator; 7 import com. baoyz. swipemenulistview. swipeMenuItem; 8 import com. baoyz. swipemenulistview. swipeMenuListView; 9 import com. baoyz. swipemenulistview. swipeMenuListView. onMenuItemClickListener; 10 import com. baoyz. swipemenulistvi Ew. swipeMenuListView. onSwipeListener; 11 12 import android. app. activity; 13 import android. content. context; 14 import android. graphics. color; 15 import android. graphics. drawable. colorDrawable; 16 import android. OS. asyncTask; 17 import android. OS. bundle; 18 import android. OS. handler; 19 import android. OS. message; 20 import android. util. log; 21 import android. view. layoutInflater; 22 import android. v Iew. view; 23 import android. view. viewGroup; 24 import android. widget. arrayAdapter; 25 import android. widget. textView; 26 import android. widget. toast; 27 28 public class MainActivity extends Activity {29 30 private ArrayList <String> datas; 31 private ArrayAdapter adapter; 32 private SwipeMenuListView listView; 33 private Handler handler; 34 35 @ Override 36 protected void onCreate (Bundle savedInsta NceState) {37 super. onCreate (savedInstanceState); 38 setContentView (R. layout. activity_main); 39 40 handler = new Handler () {41 @ Override 42 public void handleMessage (Message msg) {43 if (msg. what = 0) {44 45 int position = (Integer) msg. obj; 46 String str = datas. get (position); 47 datas. remove (position); 48 datas. add (0, str); 49 adapter. notifyDataSetChanged (); 50 Toast. makeText (getApplication Context (), "success", 0 ). show (); 51 52} else if (msg. what = 1) {53 54 Toast. makeText (getApplicationContext (), "open", 0 ). show (); 55 56} else if (msg. what = 2) {57 58 int position = (Integer) msg. obj; 59 datas. remove (position); 60 adapter. notifyDataSetChanged (); 61 Toast. makeText (getApplicationContext (), "successfully deleted", 0 ). show (); 62 63} 64} 65}; 66 67 // Add test data 68 datas = new ArrayList <String> (); 69 f Or (int I = 0; I <= 50; I ++) {70 datas. add ("Test Data -->" + I); 71} 72 73 adapter = new MyAdapter (this,-1); 74 75 SwipeMenuCreator creator = new SwipeMenuCreator () {76 77 @ Override 78 public void create (SwipeMenu menu) {79 // set the top menu button and set the icon and title 80 SwipeMenuItem top = new SwipeMenuItem (getApplicationContext ()); 81 top. setBackground (new ColorDrawable (Color. LTGRAY); 82 top. setWidth (dp2px (90); 83 P. setTitle ("top"); 84 top. setTitleSize (20); 85 top. setIcon (R. drawable. ic_top); 86 top. setTitleColor (Color. RED); 87 menu. addMenuItem (top); 88 89 // set the menu button to enter, only set the title 90 SwipeMenuItem openItem = new SwipeMenuItem (getApplicationContext (); 91 openItem. setBackground (new ColorDrawable (Color. GREEN); 92 openItem. setWidth (dp2px (90); 93 openItem. setTitle ("open"); 94 openItem. setTitleSize (20); 95 openItem. set TitleColor (Color. WHITE); 96 menu. addMenuItem (openItem); 97 98 // set the delete menu button. Only the icons 99 SwipeMenuItem deleteItem = new SwipeMenuItem (getApplicationContext (); 100 deleteItem are set. setWidth (dp2px (90); 101 deleteItem. setBackground (new ColorDrawable (Color. RED); 102 deleteItem. setIcon (R. drawable. ic_delete); 103 menu. addMenuItem (deleteItem); 104} 105}; 106 107 listView = (SwipeMenuListView) findViewById (R. id. listV Iew); 108 listView. setMenuCreator (creator); 109 110 // set the event 111 listView triggered when different menus are clicked. listener (new OnMenuItemClickListener () {112 113 @ Override114 public boolean onMenuItemClick (int position, SwipeMenu menu, int index) {115 // The value of index is to add SwipeMenuItem values in sequence in SwipeMenu, similar to the subscript of an array. 116 // starting from 0, in sequence: 0, 1, 2, 3... 117 switch (index) {118 case 0: 119 // make the menu smooth and close the 120 listView. smoothCloseMenu (); 121 122 Message msg0 = handler. obtainMessage (); 123 msg0.what = 0; 124 msg0.obj = position; 125 // sent after 1 second, in order to reflect the effect of smooth menu closure 126 handler. sendMessageDelayed (msg0, 1000); 127 String str = datas. get (position); 128 129 break; 130 case 132 Message msg1 = handler. obtainMessage (); 133 msg1.why = 1; 134 msg1.obj = Position; 135 handler. sendMessage (msg1); 136 137 break; 138 case listView. smoothCloseMenu (); 140 141 Message msg2 = handler. obtainMessage (); 142 msg2.what = 2; 143 msg2.obj = position; 144 handler. sendMessageDelayed (msg2, 1000); 145 146 break; 147} 148 // false: The menu is automatically collapsed when a user triggers a screen somewhere else. 149 // true: do not change the style of the opened menu and keep it unchanged. 150 return false; 151} 152}); 153 154 // monitor the user's SwipeMenu slide event in ListView. 155 listView. setOnSwipeListener (new OnSwipeListener () {156 157 @ Override158 public void onSwipeStart (int position) {159 Log. d ("Location: --" + position, ""); 160} 161 162 @ Override163 public void onSwipeEnd (int position) {164 Log. d ("Location: --" + position, ""); 165} 166}); 167 168 listView. setAdapter (adapter); 169} 170 171 private class MyAdapter extends ArrayAdapter {172 173 LayoutInflater inflater; 174 175 176 public MyAdapter (Context context, int resource) {super (context, resource ); 177 inflater = LayoutInflater. from (context); 178} 179 180 @ Override181 public int getCount () {182 return datas. size (); 183} 184 185 @ Override186 public View getView (int position, View convertView, ViewGroup parent) {187 if (convertView = null) {188 convertView = inflater. inflate (R. layout. item, null); 189} 190 191 TextView textView = (TextView) convertView. findViewById (R. id. textView); 192 textView. setText (datas. get (position); 193 194 return convertView; 195} 196 197} 198 199 public int dp2px (float dipValue) {200 final float scale = this. getResources (). getDisplayMetrics (). density; 201 return (int) (dipValue * scale + 0.5f); 202} 203}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.