High imitation American group swipe left to delete entries

Source: Internet
Author: User
Tags gety

1.



2. Function realization

2.1 Layout structure

<?xml version= "1.0" encoding= "UTF-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" 80dip "android:background=" #FF5555 "> &L T TextView android:id= "@+id/tv_show_info" android:layout_width= "match_parent" android:layout_height= "Ma Tch_parent "android:layout_marginright=" 10dip "android:gravity=" right|center_vertical "Android:paddin G= "15dip" android:text= "swipe left to delete" android:textcolor= "#FFFFFF"/> <linearlayout android:layout_ Width= "Fill_parent" android:layout_height= "match_parent" android:background= "#00FFFFFF" > <line Arlayout android:layout_width= "fill_parent" android:layout_height= "Match_parent" Android: background= "#FFFFFF" > <textview android:id= "@+id/list_item" android:layout             _width= "Match_parent"   android:layout_height= "match_parent" android:gravity= "center" android:padding= "15dip" android:text= "ad" android:textsize= "18sp"/> </LinearLayout> </linearlayo Ut></framelayout>
The Framelayout layout is used here, and its first child textview is the text that is displayed, and when it moves to the left, its font size slowly becomes larger, the transparency becomes clearer, and the text is eventually hidden when it slides to the right. The second child has a linearlayout and it is transparent, because when the view moves it is its child view, if you do not addLinearLayout So the entire layout slides to the left and doesn't see the following words.

2.2 Classes that need to be used

You need to inherit the ListView to extend it, the idea is to get the position of the current item according to the coordinates of the user at the time of position, to get its corresponding view item according to the location, then manipulate the view to move it.

Second, there is the natural thought of rolling to use the Scroller class, you can make the layout to the left slide, here in order to facilitate the addition of Gesturedetector gesture recognition class.

Finally, rewrite the Computescroll method to make it complete the scrolling animation.

2.3 Implementation

Package Com.example.customui.view;import Android.content.context;import Android.util.attributeset;import Android.view.gesturedetector;import Android.view.gesturedetector.ongesturelistener;import Android.view.motionevent;import Android.view.view;import Android.view.viewconfiguration;import Android.view.viewgroup;import Android.view.windowmanager;import Android.widget.adapterview;import Android.widget.listview;import Android.widget.scroller;import Com.example.customui.util.logger;public Class Swipelistview extends ListView {/** * gesture recognition */private gesturedetector mgesturedetector;/** * Sliding class */private Scroller Mscroll er;/** * Need to move the view */private view currentmoveview;private int downx;private int downy;/** * Convert the coordinates of the finger down to the position of the ListView item * /private int slideposition;/** * is sliding */private boolean isslide;//fast sliding//private boolean isfling; private int mtouchslop;/** * screen width */private int screenwidth;/** * ListView Item */private viewgroup viewgroup;private Stat  IC final int snap_velocity = 600; PubliC Swipelistview (Context context) {super (context); init (context);} Public Swipelistview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);} private void init (context context) {Mscroller = new Scroller (context); mtouchslop = Viewconfiguration.get (GetContext ()).  Getscaledtouchslop (); ScreenWidth = ((WindowManager) Context.getsystemservice (Context.window_service)). Getdefaultdisplay (). GetWidth (); Mgesturedetector = new Gesturedetector (context,new Ongesturelistener () {@Overridepublic Boolean onsingletapup ( Motionevent e) {return false;} @Overridepublic void Onshowpress (Motionevent e) {} @Overridepublic boolean onscroll (motionevent E1, motionevent E2, float Distancex,float Distancey) {if (isslide && currentmoveview!=null) {if (listener!=null) {Listener.scroll ( CURRENTMOVEVIEW.GETSCROLLX (), viewgroup);} Currentmoveview.scrollby ((int) Distancex, 0);} return false;} @Overridepublic void Onlongpress (Motionevent e) {} @Overridepublic boolean onfling (motionevent E1, motionevent E2, float velocityx,float velocityy) {//if (Isslide && (E2.getx ()-e1.getx ()) <0) {//isfling = true;// Logger.getlogger (). I ("==============> Quick Slide" + (E2.getx ()-e1.getx ()));//}return false;} @Overridepublic Boolean Ondown (Motionevent e) {return false;}}); @Overridepublic boolean dispatchtouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent.action_ Down: {Logger.getlogger (). I ("--------dispatchtouchevent-------->"); if (!mscroller.isfinished () | | Isslide) {return super.dispatchtouchevent (event);} DOWNX = (int) event.getx ();d owny = (int) event.gety (); Logger.getlogger (). I ("-------------downx=" +downx) slideposition = Pointtoposition (Downx, DownY);//Invalid position, Isslide prevents the value of isfling when multiple fingers are pressed, resulting in deletion if (slideposition = = adapterview.invalid_position) {return Super.dispatchtouchevent (event);}    Get the item that we clicked on view ViewGroup = (viewgroup) getchildat (Slideposition-getfirstvisibleposition ()); Need a certain Viewcurrentmoveview = Viewgroup.getchildat (1); break;} CasE motionevent.action_move:{int x = (int) (EVENT.GETX ()-downx); int y = (int) (Event.gety ()-downy);//logger.getlogger (). I ("-------------x=" +x+ "y=" +y+ "Mtouchslop:" +mtouchslop); if (Math.Abs (x) >mtouchslop && math.abs (x) > Math.Abs (y) && x<0) {isslide = true;} Break;}} Return Super.dispatchtouchevent (event);} @Overridepublic boolean ontouchevent (motionevent ev) {mgesturedetector.ontouchevent (EV); if (isslide) {if (! Mscroller.isfinished ()) {return true;} Switch (ev.getaction ()) {Case motionevent.action_down:{downx = (int) ev.getx (); Case MotionEvent.ACTION_CANCEL:case motionevent.action_up:{/*if (isfling | | Isremoveitem ()) {if (isfling && This.listener!=null) {Logger.getlogger (). I ("sliding Delete: "); This.listener.removeItem (slideposition);} Else*/if (Isremoveitem () && this.listener!=null) {this.listener.removeItem (slideposition); Mscroller.startscroll (Currentmoveview.getscrollx (), 0,screenwidth, 0,math.abs (300)); else {int delta = CurrentmoveviEw.getscrollx (); Mscroller.startscroll (Currentmoveview.getscrollx (), 0,-delta, 0,math.abs (500)); Postinvalidate ();}} return true;} return super.ontouchevent (EV);} @Overridepublic void Computescroll () {///Call Startscroll when Scroller.computescrolloffset () returns True if (Mscroller.comp Utescrolloffset ()) {//Let ListView item scroll Currentmoveview.scrollto based on the current scroll offset (mscroller.getcurrx              (), 0);              Postinvalidate ();            Call the callback interface if (mscroller.isfinished ()) {currentmoveview.scrollto (0, 0) at the end of the scrolling animation;            if (this.listener!=null && isslide) {this.listener.scrollFinish ();            } isslide = false; }}}private Boolean Isremoveitem () {if (CURRENTMOVEVIEW.GETSCROLLX () &GT;SCREENWIDTH/2) {return true;} return false;} Private Removeitemlistviewlistener listener;public void Setremoveitemlistviewlistener (Removeitemlistviewlistener Listener) {This.listener = listener;} Public InterfaCe removeitemlistviewlistener{public void RemoveItem (int pos);p ublic void scroll (float scrollx,view View);p ublic void Scrollfinish ();}}
2.4 Activity Code

Sets the font size and transparency of the TextView by scrolling the distance.

Package Com.example.customui.activity;import Java.util.arraylist;import java.util.list;import android. R.integer;import Android.content.context;import Android.os.bundle;import Android.view.view;import Android.view.windowmanager;import Android.view.animation.alphaanimation;import android.view.animation.Animation; Import Android.view.animation.scaleanimation;import Android.widget.arrayadapter;import Android.widget.TextView; Import Com.example.customui.baseactivity;import Com.example.customui.r;import Com.example.customui.adapter.commonadapter;import Com.example.customui.adapter.viewholder;import Com.example.customui.util.densityutil;import Com.example.customui.util.logger;import Com.example.customui.view.swipelistview;import Com.example.customui.view.swipelistview.removeitemlistviewlistener;public class Swipelistviewactivity extends baseactivity {private Swipelistview mlistview;private list<string> datasourcelist;private ArrayAdapter mAdapter ;p rivate int screenwidth; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.swipe_ Listview_main); Mlistview = (Swipelistview) Findviewbyid (r.id.lv_data); screenwidth = ((WindowManager) Getsystemservice (Context.window_service)). Getdefaultdisplay (). GetWidth ();d atasourcelist = new ArrayList<String > (); for (int i = 0; i < i++) {Datasourcelist.add ("item" +i);} Madapter = new Arrayadapter (Getapplicationcontext (), r.layout.list_view_item,r.id.list_item,datasourcelist); Mlistview.setadapter (Madapter); Mlistview.setremoveitemlistviewlistener (new Removeitemlistviewlistener () {Private TextView TextView; @Overridepublic void RemoveItem (int pos) {position = pos;} @Overridepublic void Scroll (float scrollx, view view) {if (textview==null) {TextView = (TextView) View.findviewbyid (r.id. Tv_show_info);} int fontsize=1;float alpha=0.0f;if (SCROLLX&GT;SCREENWIDTH/3) {alpha=1.0f;fontsize=maxfontsize;} else {alpha = scrollx/(SCREENWIDTH/3); fontSize = (int) (alpha * maxfontsize);}textview.setalpha (Alpha); Textview.settextsize (fontSize);} @Overridepublic void Scrollfinish () {textview=null;if (position>=0) {Logger.getlogger (). I ("--------------------- --Delete Location: "+position";d atasourcelist.remove (position); Madapter.notifydatasetchanged ();p osition=-1;}});} private int position=-1; private int maxfontsize=22;}




High imitation American group swipe left to delete entries

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.