Scroroller application: ListView sliding deletion, scrollerlistview

Source: Internet
Author: User

Scroroller application: ListView sliding deletion, scrollerlistview
1. Design Ideas

In Scroller's application-Sliding Screen implementation, scroroller is used to achieve sliding screen effect. Here, scroroller and ListView are used to achieve similar QQ sliding, and click Delete. The design idea is that items are slide using scroroller, listView determines whether it is a horizontal or vertical Slide Based on the touch. About how to handle the Click Event: The onClick event of the View is the same as that of the normal one. The OnItemClick event is processed and the touch distance is determined, if the value is less than 5, the MotionEvent in the onTouchEvent method of the Item. return false in ACTION_UP, so that the super of dispatchTouchEvent in ListView. dispatchTouchEvent (event) returns false. The current position and the clicked view are obtained based on x and y, and super is called. extends mitemclick (view, position, view. getId ( ) To tell the ListView to start the onItemClick event.

2. Item code

Package com. jwzhangjie. scrollview; import android. content. context; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. view; import android. widget. linearLayout; import android. widget. scroller; public class ListItemDelete extends LinearLayout {private Scroller mScroller; // sliding control private float mLastMotionX; // remember the last touch screen position private int deltaX; private int B Ack_width; private float downX; public ListItemDelete (Context context) {this (context, null);} public ListItemDelete (Context context, AttributeSet attrs) {super (context, attrs ); init (context);} private void init (Context context) {mScroller = new Scroller (context) ;}@ Overridepublic void computeScroll () {if (mScroller. computescroloffset () {// update the current x and y position scrollTo (mScroller. getCurrX (), mScroller. g EtCurrY (); postInvalidate () ;}@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); int count = getChildCount (); for (int I = 0; I <count; I ++) {measureChild (getChildAt (I), widthMeasureSpec, heightMeasureSpec); if (I = 1) {back_width = getChildAt (I ). getMeasuredWidth () ;}}@ Overridepublic boolean onTouchEvent (MotionEvent Event) {int action = event. getAction (); float x = event. getX (); switch (action) {case MotionEvent. ACTION_DOWN: Log. e ("test", "item ACTION_DOWN"); mLastMotionX = x; downX = x; break; case MotionEvent. ACTION_MOVE: Log. e ("test", back_width + "item ACTION_MOVE" + getScrollX (); deltaX = (int) (mLastMotionX-x); mLastMotionX = x; int scrollx = getScrollX () + deltaX; if (scrollx> 0 & scrollx <back_width) {scr OllBy (deltaX, 0);} else if (scrollx> back_width) {scrollTo (back_width, 0);} else if (scrollx <0) {scrollTo (0, 0 );} break; case MotionEvent. ACTION_UP: Log. e ("test", "item ACTION_UP"); int scroll = getScrollX (); if (scroll> back_width/2) {scrollTo (back_width, 0 );} else {scrollTo (0, 0);} if (Math. abs (x-downX) <5) {// here, you can determine whether it is itemClickreturn false Based on the click distance;} break; case MotionEvent. ACTION_CANCEL: scrollTo (0, 0); break;} return true;} @ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {int margeLeft = 0; int size = getChildCount (); for (int I = 0; I <size; I ++) {View view = getChildAt (I); if (view. getVisibility ()! = View. GONE) {int childWidth = view. getMeasuredWidth (); // arrange the internal child in a horizontal view. layout (margeLeft, 0, margeLeft + childWidth, view. getMeasuredHeight (); margeLeft + = childWidth ;}}}}


3. ListView code

Package com. jwzhangjie. scrollview; import android. content. context; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. view; import android. widget. listView; public class ScrollListviewDelete extends ListView {private float minDis = 10; private float mLastMotionX; // remember the position of private float mLastMotionY on the last X touch screen; // remember the position of the last Y touch screen. private boolean isLock = false; Public ScrollListviewDelete (Context context, AttributeSet attrs) {super (context, attrs);}/*** if the onInterceptTouchEvent () method of a ViewGroup returns true, the Touch event is intercepted, * The Child View no longer receives the Touch event, but switches to the * onTouchEvent () method of the current ViewGroup for processing. Starting from Down, the subsequent Move and Up operations will be processed directly in the onTouchEvent () method. * The child view that is still processing touch event will receive an ACTION_CANCEL. * If onInterceptTouchEvent () returns false, the event is handed over to the child view for processing. * // @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {if (! IsIntercept (ev) {return false;} return super. onInterceptTouchEvent (ev) ;}@ Overridepublic boolean dispatchTouchEvent (MotionEvent event) {boolean dte = super. dispatchTouchEvent (event); if (MotionEvent. ACTION_UP = event. getAction ()&&! Dte) {// onItemClickint position = pointToPosition (int) event. getX (), (int) event. getY (); View view = getChildAt (position); super. extends mitemclick (view, position, view. getId ();} return dte;} @ Override // process the click event. If it is a gesture event, the normal Viewpublic boolean cursor mclick () {return super. optional mclick () ;}@ Override // process the click event. If it is a gesture event, the ListViewpublic boolean partial mitemclick (View view, int position, long id) {ret Urn super. performItemClick (view, position, id);}/*** checks whether the slider is ListView sliding or the isLock is an item sliding, otherwise, false */private boolean isIntercept (MotionEvent ev) {float x = ev. getX (); float y = ev. getY (); int action = ev. getAction (); switch (action) {case MotionEvent. ACTION_DOWN: Log. e ("test", "isIntercept ACTION_DOWN" + isLock); mLastMotionX = x; mLastMotionY = y; break; case MotionEvent. ACTION_MOVE: Log. e ("tes T "," isIntercept ACTION_MOVE "+ isLock); if (! IsLock) {float deltaX = Math. abs (mLastMotionX-x); float deltay = Math. abs (mLastMotionY-y); mLastMotionX = x; mLastMotionY = y; if (deltaX> deltay & deltaX> minDis) {isLock = true; return false ;}} else {return false;} break; case MotionEvent. ACTION_UP: Log. e ("test", "isIntercept ACTION_UP" + isLock); isLock = false; break; case MotionEvent. ACTION_CANCEL: Log. e ("test", "isIntercept ACTION_CANCEL" + isLock); isLock = false; break;} return true ;}}


4. Activity Code

Package com. jwzhangjie. scrollview; import android. content. context; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. view; import android. widget. listView; public class ScrollListviewDelete extends ListView {private float minDis = 10; private float mLastMotionX; // remember the position of private float mLastMotionY on the last X touch screen; // remember the position of the last Y touch screen. private boolean isLock = false; Public ScrollListviewDelete (Context context, AttributeSet attrs) {super (context, attrs);}/*** if the onInterceptTouchEvent () method of a ViewGroup returns true, the Touch event is intercepted, * The Child View no longer receives the Touch event, but switches to the * onTouchEvent () method of the current ViewGroup for processing. Starting from Down, the subsequent Move and Up operations will be processed directly in the onTouchEvent () method. * The child view that is still processing touch event will receive an ACTION_CANCEL. * If onInterceptTouchEvent () returns false, the event is handed over to the child view for processing. * // @ Overridepublic boolean onInterceptTouchEvent (MotionEvent ev) {if (! IsIntercept (ev) {return false;} return super. onInterceptTouchEvent (ev) ;}@ Overridepublic boolean dispatchTouchEvent (MotionEvent event) {boolean dte = super. dispatchTouchEvent (event); if (MotionEvent. ACTION_UP = event. getAction ()&&! Dte) {// onItemClickint position = pointToPosition (int) event. getX (), (int) event. getY (); View view = getChildAt (position); super. extends mitemclick (view, position, view. getId ();} return dte;} @ Override // process the click event. If it is a gesture event, the normal Viewpublic boolean cursor mclick () {return super. optional mclick () ;}@ Override // process the click event. If it is a gesture event, the ListViewpublic boolean partial mitemclick (View view, int position, long id) {ret Urn super. performItemClick (view, position, id);}/*** checks whether the slider is ListView sliding or the isLock is an item sliding, otherwise, false */private boolean isIntercept (MotionEvent ev) {float x = ev. getX (); float y = ev. getY (); int action = ev. getAction (); switch (action) {case MotionEvent. ACTION_DOWN: Log. e ("test", "isIntercept ACTION_DOWN" + isLock); mLastMotionX = x; mLastMotionY = y; break; case MotionEvent. ACTION_MOVE: Log. e ("tes T "," isIntercept ACTION_MOVE "+ isLock); if (! IsLock) {float deltaX = Math. abs (mLastMotionX-x); float deltay = Math. abs (mLastMotionY-y); mLastMotionX = x; mLastMotionY = y; if (deltaX> deltay & deltaX> minDis) {isLock = true; return false ;}} else {return false;} break; case MotionEvent. ACTION_UP: Log. e ("test", "isIntercept ACTION_UP" + isLock); isLock = false; break; case MotionEvent. ACTION_CANCEL: Log. e ("test", "isIntercept ACTION_CANCEL" + isLock); isLock = false; break;} return true ;}}


5. XML Code

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <com.jwzhangjie.scrollview.ScrollListviewDelete        android:id="@android:id/list"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

<? Xml version = "1.0" encoding = "UTF-8"?> <Com. jwzhangjie. scrollview. listItemDelete xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <LinearLayout android: id = "@ + id/front" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: background = "@ drawable/bg_item_list_8" android: gravity = "center" android: orientation = "horizontal"> <TextView android: id = "@ + id/itemData" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Test Data"/> </LinearLayout> <LinearLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: gravity = "center" android: orientation = "horizontal"> <Button android: id = "@ + id/btnNao" android: layout_width = "wrap_content" android: layout_height = "match_parent" android: background = "@ drawable/bg_item_list_4" android: text = "alarm"/> <Button android: id = "@ + id/btnDelete" android: layout_width = "wrap_content" android: layout_height = "match_parent" android: background = "@ drawable/bg_item_list_5" android: text = "delete"/> </LinearLayout> </com. jwzhangjie. scrollview. listItemDelete>
6. Interface Effects

The above code is not guaranteed to be up-to-date, update the Code address: https://github.com/jwzhangjie/-ScrollerDelete


Android ListView Item sliding deletion Effect

GOOGLE SwipeListView

How can I remove the flashing effect of listview and the blue effect when the listview slides to the top or bottom?

SetSelector (null );
Or set item disable In the Adapter.
Android: listSelector = "#00000000"

Set this attribute.

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.