ListView implements left and right sliding with Filpper effect to delete Item, listviewfilpper

Source: Internet
Author: User

ListView implements left and right sliding with Filpper effect to delete Item, listviewfilpper

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 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 animation) {// TODO Auto-generated method stub} @ Overridepublic void Merge (Animation animation) {// TODO Auto-generated method stub} @ Overridepublic void onAnimationEnd (Animation 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 context) {super (context); // TODO Auto-generated constructor stub} public FilpperListvew (Context 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




Android ListView Item sliding deletion Effect

GOOGLE SwipeListView

How can we solve the conflict between the sliding between the left and right of a single item in Android listview and the sliding up and down of the entire listview?

The conflict between the left and right sliding of a single item and the uplink and downlink sliding of the entire listview lies in the identification of whether the user's action is to slide the item or listview, the sliding behavior after knowing the user's intention will not be a conflict (a deep understanding of this sentence ).
Currently, we can barely cope with this by identifying the user's intention based on the user's moving coordinates:
1. Record the start X and Y coordinates of the movement triggered by the user;
2. records the X and Y coordinates of the user's movement process;
3. Calculate the horizontal and vertical offset. If the former is large, it is regarded as horizontal movement and moving item; otherwise, it is regarded as vertical movement, which is regarded as moving listview.

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.