Analog Mobile QQ Swipe Delete message

Source: Internet
Author: User
<span id="Label3"></p><p><p><br></p></p> <pre class="brush:java;toolbar: true; auto-links: false;">package com.example.srolltest;import java.util.arraylist;import java.util.list;import  android.app.activity;import android.os.bundle;import android.util.displaymetrics;import  android.view.layoutinflater;import android.view.motionevent;import android.view.view;import  android.view.view.onclicklistener;import android.view.view.ontouchlistener;import  android.view.viewgroup;import android.widget.baseadapter;import android.widget.button;import  android.widget.horizontalscrollview;import android.widget.listview;import android.widget.textview; public class mainactivity extends activity {    private  listview listview;    private myadapter adapter;     private list<string> datas;    private int screenwidth;      @Override     proteCted void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview ( r.layout.activity_main);        listview =  (listView)   Findviewbyid (r.id.listview);         displaymetrics outmetrics  = new displaymetrics ();         getwindowmanager (). Getdefaultdisplay (). getmetrics (outmetrics);         screenwidth =  outmetrics.widthpixels;        initdatas ();         adapter = new myadapter ();         listview.setadapter (adapter);     }    private void  initdatas ()  {        // TODO Auto-generated method stub         datas = new ArrayList<String> ();         for  (int i = 0; i < 20; i++)  {             datas.add ("delete me, Delete me"  + i);         }    }    class  myadapter extends baseadapter {         @Override          public int getcount ()  {             // TODO Auto-generated method stub             return datas.size ();         }         @Override         public  object getitem (int position)  {             // TODO Auto-generated method stub             return datas.get (position);         }         @Override          public long getitemid (int position)  {             // TODO Auto-generated method stub             return position;         }         @Override          public view getvieW (final int position, view convertview, viewgroup parent)  {             // TODO Auto-generated method  stub            final viewholder  viewholder;            if  (convertView = = null)  {                 convertview = layoutinflater.from (mainactivity.this). Inflate (                          r.layout.item_main, null);                 viewholder = new viewholder ();                 viewholder.horizontalscrollview =  (horizontalscrollview)   Convertview.findviewbyid (r.id.horizontalscrollview);                 viewHolder.tvContexTextView =  (TextView)   Convertview.findviewbyid (r.id.tv_context);                 viewHolder.btnDelete =  (Button)  convertview.findviewbyid (r.id.btn _delete);                 viewholder.totop =  (Button)  convertview.findviewbyid (r.id.btn_totop);                 convertview.settag (viewHolder);             } else {                 viewholder =  (viewholder)  convertview.gettag ();             }             viewholder.tvcontextextview.settext (datas.get (position));             viewholder.tvcontextextview.setwidth (screenwidth);             if  (viewHolder.horizontalScrollView.getScrollX ()   > 0)  {                 viewholder.horizontalscrollview.smoothscrollto (0, 0);             }             Convertview.setontouchlistener (new ontouchlistener ()  {                                   @Override                  public boolean ontouch (view v, motionevent event)  {                     switch   (event.getaction ())  {                     case MotionEvent.ACTION_UP:                          Int actionwidth = viewholder.btndelete.getwidth () +viewholder.totop.getwidth ();                          int SCROLLX&NBSP;=VIEWHOLDER.HORIZONTALSCROLLVIEW.GETSCROLLX ();                         if (scrollX> Actionwidth/2) {                              ViewHolder.horizontalScrollView.smoothScrollTo (actionwidth, 0);                         }else  {                              ViewHolder.horizontalScrollView.smoothScrollTo (0, 0);                         }                         return true;                     }                     return false;                 }             });             viewholder.tvcontextextview.setonclicklistener (new onclicklistener ()  {                  @Override                  public void onclick ( View v)  {                     / / todo auto-generated method stub                     notifydatasetchanged ();                 }             });             viewholder.btndelete.setonclicklistener (new onclicklistener ()  {                                   @Override                  public void onclick (View v)  {      &Nbsp;              datas.remove (position );                     notifydatasetchanged ();                 }            });             viewholder.totop.setonclicklistener (new  onclicklistener ()  {                                   @Override                  public void onclick (view v)  {               &nBsp;     string temp = datas.get (0);                     datas.set (0,  Datas.get (position));                     datas.set (position, temp);                     notifydatasetchanged ();                                      }             });             return convertview;        }         class viewholder {             HorizontalScrollView horizontalScrollView;             TextView tvContexTextView;             Button btnDelete;             button totop;        }    }}</pre> <p><p>Activity_main.xml</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar: true; auto-links: false;"><linearlayout 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:orien        tation= "vertical" > <listview android:id= "@+id/listview" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:fastscrollenabled= "true"/></linearlayout></pre></pre> <p><p>Item_main.xml</p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><?xml version= "1.0"  encoding= "utf-8"? >

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.