Listview implements left and right sliding with filpper effect to delete items

Source: Internet
Author: User

The most important method for listview to implement sliding left and right items with filpper effect is to inherit and override listview. Then add the translateanimation slide event during the deletion process.


<Span style = "font-size: 14px;"> public class filpperactivity extends activity {private filpperlistvew flipperlistview; private myadapter adapter; private list <string> items; private int width; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_filpper); displaymetrics dm = new displaymetrics (); getwindowmanager (). getdefaultdi Splay (). getmetrics (DM); width = DM. widthpixels; flipperlistview = (filpperlistvew) findviewbyid (R. id. filpperlistview); items = new arraylist <string> (); For (INT I = 0; I <15; I ++) {items. add ("item ---->" + I);} adapter = new myadapter (this, items); flipperlistview. setadapter (adapter); // custom interface flipperlistview. setfilpperdeletelistener (New filpperdeletelistener () {@ overridepublic void filpperdelete (float xposition, FL Oat yposition) {// The listview must contain items. Otherwise, if (flipperlistview. getchildcount () = 0) return; // obtain the index final int Index = flipperlistview of the deleted item based on the coordinate. pointtoposition (INT) xposition, (INT) yposition); // in the next two steps, you can obtain the relative position of the entry in the screen. If you delete the entry directly based on the index, an error occurs when it is null. Because child in listview is not empty only when it is displayed on the screen. Int firstvisiblepostion = flipperlistview. getfirstvisibleposition (); view = flipperlistview. getchildat (index-firstvisiblepostion); <span style = "color: # ff0000;"> translateanimation trananimation = new translateanimation (0, width, 0, 0); trananimation. setduration (500); trananimation. setfillafter (true); view. startanimation (trananimation); </span> // delete an animation after it is played. Otherwise, no animation effect will appear (tested by yourself ). Trananimation. setanimationlistener (New animationlistener () {@ overridepublic void onanimationstart (animation) {// todo auto-generated method stub} @ overridepublic void Merge (animation) {// todo auto-generated method stub} @ overridepublic void onanimationend (animation) {// delete an itemitems. remove (INDEX); adapter. notifydatasetchanged () ;}}) ;}}}</span>

Rewritten listview:

<Span style = "font-size: 14px;"> package COM. example. filpperdeletelist; import android. content. context; import android. util. attributeset; import android. view. motionevent; import android. widget. listview; public class filpperlistvew extends listview {private float mylastx =-1; private float mylasty =-1; private Boolean Delete = false; // custom sliding deletion listener private filpperdeletelistener filpperdeleterlistener; public fil Pperlistvew (context) {super (context); // todo auto-generated constructor stub} public filpperlistvew (context, attributeset attrs) {super (context, attrs ); // todo auto-generated constructor stub} @ overridepublic Boolean ontouchevent (motionevent eV) {// todo auto-generated method stubswitch (ev. getaction () {Case motionevent. action_down: // obtain the X coordinate of the first vertex mylastx = eV. getx (0); mylasty = eV. gety (0); break; Case motionevent. action_move: // get the coordinate float deltax = eV of the last vertex. getx (ev. getpointercount ()-1)-mylastx; float deltay = math. ABS (ev. gety (ev. getpointercount ()-1)-mylasty); // conditions for sliding and deletion: Transverse sliding is greater than 100, and vertical deviation is less than 50if (deltax> 100.0 & deltay <50) {Delete = true;} break; Case motionevent. action_up: If (delete & filpperdeleterlistener! = NULL) {filpperdeleterlistener. filpperdelete (mylastx, mylasty) ;}reset (); break;} return Super. ontouchevent (EV);} public void reset () {Delete = false; mylastx =-1; mylasty =-1;} public void setfilpperdeletelistener (filpperdeletelistener f) {listener = f ;} // custom interface public interface filpperdeletelistener {public void filpperdelete (float xposition, float yposition) ;}</span>

Adapter code:


<span style="font-size:14px;">package com.example.filpperdeletelist;import java.util.List;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;public class MyAdapter extends BaseAdapter {    private List<String> list ;    private Context context ;    public MyAdapter(Context context,List<String> list){    this.context = context ;    this.list = list;    }@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn list.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubconvertView = LayoutInflater.from(context).inflate(R.layout.lv_item, null);TextView text = (TextView)convertView.findViewById(R.id.tv);text.setText(list.get(position));return convertView;}}</span>



:

Http://download.csdn.net/detail/jia635/7651981



Listview implements left and right sliding with filpper effect to delete items

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.