横向滑动可以删除的listView


主要的思路是:

重写ListView中的Item,也就是LinearLayout的onTouchEvent方法,以监听横向滑动和纵向滑动:

         1)纵向滑动时,不管

         2)横向滑动时,请求父容器,也即是ListView不要拦截触摸事件,自己在子View(也就是LinearLayout)里面处理就好了,当横向触摸时间结束(MotionEvent.Action_UP)或者划出边界(MotionEvent.Action_CANCEL)时,恢复允许父容器拦截触摸事件。


先上效果图



重写的LinearLayout如下:


  1. package ex.oyyj.listviewfulldemo;  
  2.   
  3. import android.animation.Animator;  
  4. import android.animation.ObjectAnimator;  
  5. import android.content.Context;  
  6. import android.util.AttributeSet;  
  7. import android.util.Log;  
  8. import android.util.TypedValue;  
  9. import android.view.MotionEvent;  
  10. import android.view.ViewParent;  
  11. import android.widget.LinearLayout;  
  12.   
  13. /** 
  14.  * Created by oyyj on 2015/8/6. 
  15.  */  
  16. public class HorizontalSlideLayout extends LinearLayout {  
  17.     private static String TAG = "VerticalSlideLayout";  
  18.     private int DRAG_X_THROD = 0;  
  19.     private int SCROLL_X = 0;  
  20.     private final int ANIM_DURATION = 300;  
  21.   
  22.     private static final int SLIDE_TO_LEFT = -1;  
  23.     private static final int SLIDE_TO_RIGHT = 1;  
  24.     private int mSlideDirection = 0;  
  25.   
  26.   
  27.     public HorizontalSlideLayout(Context context, AttributeSet attrs, int defStyle) {  
  28.         super(context, attrs, defStyle);  
  29.         //判断横划的阈值,为了兼容不同尺寸的设备,以dp为单位  
  30.         DRAG_X_THROD = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());  
  31.         SCROLL_X = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, context.getResources().getDisplayMetrics());  
  32.     }  
  33.   
  34.     public HorizontalSlideLayout(Context context, AttributeSet attrs) {  
  35.         this(context, attrs, 0);  
  36.     }  
  37.   
  38.     public HorizontalSlideLayout(Context context) {  
  39.         this(context, null0);  
  40.     }  
  41.   
  42.     @Override  
  43.     protected void onAttachedToWindow() {  
  44.         super.onAttachedToWindow();  
  45.     }  
  46.   
  47.     int downX, downY;  
  48.     boolean isNeedToGoBack;  
  49.     private ObjectAnimator mAnimator;  
  50.   
  51.     @Override  
  52.     public boolean onTouchEvent(MotionEvent ev) {  
  53.   
  54.         boolean isInterceptHere = false;  
  55.         try {  
  56.             switch (ev.getAction()) {  
  57.                 case MotionEvent.ACTION_DOWN:  
  58.                     downX = (int) ev.getX();  
  59.                     downY = (int) ev.getY();  
  60.                     isInterceptHere = true;  
  61.                     if (mAnimator != null) {  
  62.                         mAnimator.cancel();  
  63.                     }  
  64.                     break;  
  65.   
  66.                 case MotionEvent.ACTION_MOVE:  
  67.   
  68.                     int dx = (int) Math.abs(ev.getX() - downX);  
  69.                     int dy = (int) Math.abs(ev.getY() - downY);  
  70.                     if (dx > dy && dx > DRAG_X_THROD) {  
  71.                         Log.i(TAG, "横划!拦截它");  
  72.                        setParentInterceptTouchEvent(true);  
  73.                         isInterceptHere = true;  
  74.                         mSlideDirection = (ev.getX() - downX) > 0 ? SLIDE_TO_RIGHT : SLIDE_TO_LEFT;  
  75.                         if (mSlideDirection == SLIDE_TO_LEFT) {  
  76.                             isNeedToGoBack = true;  
  77.                             playAnimation(SCROLL_X, ANIM_DURATION);  
  78.                         } else if (mSlideDirection == SLIDE_TO_RIGHT && isNeedToGoBack) {  
  79.                             playAnimation(0, ANIM_DURATION);  
  80.                         }  
  81.                     } else if (dy > dx) {  
  82.                         Log.i(TAG, "竖划!不拦截");  
  83.                     }  
  84.                     break;  
  85.                 case MotionEvent.ACTION_UP:  
  86.                 case MotionEvent.ACTION_CANCEL:  
  87.                     setParentInterceptTouchEvent(false);  
  88.                     isInterceptHere = false;  
  89.                     downX = 0;  
  90.                     downY = 0;  
  91.                     break;  
  92.             }  
  93.         } catch (Exception e) {  
  94.             e.printStackTrace();  
  95.         }  
  96.   
  97.         return isInterceptHere;  
  98.     }  
  99.   
  100.     private void playAnimation(int translationX, int duration) {  
  101.         if (mAnimator != null) {  
  102.             mAnimator.cancel();  
  103.         }  
  104.   
  105.         mAnimator = ObjectAnimator.ofInt(this"scrollX", translationX);  
  106.         mAnimator.setDuration(duration);  
  107.         mAnimator.start();  
  108.   
  109.         mAnimator.addListener(listener);  
  110.     }  
  111.   
  112.     /*    这个函数很重要,请求禁止父容器拦截触摸事件  */  
  113.     public void setParentInterceptTouchEvent(boolean disallow) {  
  114.         ViewParent parent = getParent();  
  115.         if (parent != null) {  
  116.             parent.requestDisallowInterceptTouchEvent(disallow);  
  117.         }  
  118.     }  
  119.   
  120.     Animator.AnimatorListener listener = new Animator.AnimatorListener() {  
  121.         @Override  
  122.         public void onAnimationStart(Animator animator) {  
  123.   
  124.         }  
  125.   
  126.         @Override  
  127.         public void onAnimationEnd(Animator animator) {  
  128.             mAnimator = null;  
  129.         }  
  130.   
  131.         @Override  
  132.         public void onAnimationCancel(Animator animator) {  
  133.             mAnimator = null;  
  134.         }  
  135.   
  136.         @Override  
  137.         public void onAnimationRepeat(Animator animator) {  
  138.   
  139.         }  
  140.     };  
  141. }  
package ex.oyyj.listviewfulldemo;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.LinearLayout;

/**
 * Created by oyyj on 2015/8/6.
 */
public class HorizontalSlideLayout extends LinearLayout {
    private static String TAG = "VerticalSlideLayout";
    private int DRAG_X_THROD = 0;
    private int SCROLL_X = 0;
    private final int ANIM_DURATION = 300;

    private static final int SLIDE_TO_LEFT = -1;
    private static final int SLIDE_TO_RIGHT = 1;
    private int mSlideDirection = 0;


    public HorizontalSlideLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //判断横划的阈值,为了兼容不同尺寸的设备,以dp为单位
        DRAG_X_THROD = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());
        SCROLL_X = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, context.getResources().getDisplayMetrics());
    }

    public HorizontalSlideLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalSlideLayout(Context context) {
        this(context, null, 0);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    int downX, downY;
    boolean isNeedToGoBack;
    private ObjectAnimator mAnimator;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        boolean isInterceptHere = false;
        try {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downX = (int) ev.getX();
                    downY = (int) ev.getY();
                    isInterceptHere = true;
                    if (mAnimator != null) {
                        mAnimator.cancel();
                    }
                    break;

                case MotionEvent.ACTION_MOVE:

                    int dx = (int) Math.abs(ev.getX() - downX);
                    int dy = (int) Math.abs(ev.getY() - downY);
                    if (dx > dy && dx > DRAG_X_THROD) {
                        Log.i(TAG, "横划!拦截它");
                       setParentInterceptTouchEvent(true);
                        isInterceptHere = true;
                        mSlideDirection = (ev.getX() - downX) > 0 ? SLIDE_TO_RIGHT : SLIDE_TO_LEFT;
                        if (mSlideDirection == SLIDE_TO_LEFT) {
                            isNeedToGoBack = true;
                            playAnimation(SCROLL_X, ANIM_DURATION);
                        } else if (mSlideDirection == SLIDE_TO_RIGHT && isNeedToGoBack) {
                            playAnimation(0, ANIM_DURATION);
                        }
                    } else if (dy > dx) {
                        Log.i(TAG, "竖划!不拦截");
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    setParentInterceptTouchEvent(false);
                    isInterceptHere = false;
                    downX = 0;
                    downY = 0;
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isInterceptHere;
    }

    private void playAnimation(int translationX, int duration) {
        if (mAnimator != null) {
            mAnimator.cancel();
        }

        mAnimator = ObjectAnimator.ofInt(this, "scrollX", translationX);
        mAnimator.setDuration(duration);
        mAnimator.start();

        mAnimator.addListener(listener);
    }

    /*    这个函数很重要,请求禁止父容器拦截触摸事件  */
    public void setParentInterceptTouchEvent(boolean disallow) {
        ViewParent parent = getParent();
        if (parent != null) {
            parent.requestDisallowInterceptTouchEvent(disallow);
        }
    }

    Animator.AnimatorListener listener = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animator) {

        }

        @Override
        public void onAnimationEnd(Animator animator) {
            mAnimator = null;
        }

        @Override
        public void onAnimationCancel(Animator animator) {
            mAnimator = null;
        }

        @Override
        public void onAnimationRepeat(Animator animator) {

        }
    };
}





贴上详细调用代码:

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4.     android:paddingRight="@dimen/activity_horizontal_margin"  
  5.     android:paddingTop="@dimen/activity_vertical_margin"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"  
  7.     android:background="#ffffff">  
  8.   
  9.     <ListView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/listView"  
  13.         android:divider="@drawable/divider"  
  14.         android:listSelector="@drawable/selector_list_background"  
  15.         android:layout_centerInParent="true" />  
  16. </RelativeLayout>  
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="#ffffff">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:divider="@drawable/divider"
        android:listSelector="@drawable/selector_list_background"
        android:layout_centerInParent="true" />
</RelativeLayout>

MainActivity.java

 
  1. package ex.oyyj.listviewfulldemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.ListView;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private ListView mlistView;  
  15.     String[] films = new String[]{  
  16.             "煎饼侠",  
  17.             "猎妖记",  
  18.             "大圣归来",  
  19.             "道士下山",  
  20.             "王朝的女人·杨贵妃",  
  21.             "栀子花开",  
  22.             "太平轮(下) ",};  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.         mlistView = (ListView) findViewById(R.id.listView);  
  29.         final TitleAdapter titleAdapter = new TitleAdapter(LayoutInflater.from(this), films);  
  30.         mlistView.setAdapter(titleAdapter);  
  31.     }  
  32.   
  33.     private class TitleAdapter extends BaseAdapter {  
  34.   
  35.         String[] itemNames;  
  36.         LayoutInflater inflater;  
  37.   
  38.         public TitleAdapter(LayoutInflater _inflater, String[] names) {  
  39.             inflater = _inflater;  
  40.             itemNames = names.clone();  
  41.         }  
  42.   
  43.         @Override  
  44.         public Object getItem(int i) {  
  45.             return itemNames[i];  
  46.         }  
  47.   
  48.         @Override  
  49.         public long getItemId(int i) {  
  50.             return i;  
  51.         }  
  52.   
  53.         @Override  
  54.         public int getCount() {  
  55.             return itemNames.length;  
  56.         }  
  57.   
  58.         @Override  
  59.         public View getView(int i, View view, ViewGroup viewGroup) {  
  60.             try {  
  61.                 ViewHolder holder = new ViewHolder();  
  62.                 if (view == null ) {  
  63.                     view = inflater.inflate(R.layout.item_layout, viewGroup, false);  
  64.                     holder.title = (TextView) view.findViewById(R.id.item);  
  65.                     view.setTag(holder);  
  66.                 } else {  
  67.                     holder = (ViewHolder) view.getTag();  
  68.                 }  
  69.   
  70.                 if (holder != null && holder.title != null) {  
  71.                     TextView tv = holder.title;  
  72.                     tv.setText(itemNames[i]);  
  73.                 }  
  74.             } catch (Exception e) {  
  75.                 e.printStackTrace();  
  76.             }  
  77.             return view;  
  78.         }  
  79.   
  80.         private class ViewHolder {  
  81.             TextView title;  
  82.         }  
  83.     }  
  84. }  
package ex.oyyj.listviewfulldemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ListView mlistView;
    String[] films = new String[]{
            "煎饼侠",
            "猎妖记",
            "大圣归来",
            "道士下山",
            "王朝的女人·杨贵妃",
            "栀子花开",
            "太平轮(下) ",};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlistView = (ListView) findViewById(R.id.listView);
        final TitleAdapter titleAdapter = new TitleAdapter(LayoutInflater.from(this), films);
        mlistView.setAdapter(titleAdapter);
    }

    private class TitleAdapter extends BaseAdapter {

        String[] itemNames;
        LayoutInflater inflater;

        public TitleAdapter(LayoutInflater _inflater, String[] names) {
            inflater = _inflater;
            itemNames = names.clone();
        }

        @Override
        public Object getItem(int i) {
            return itemNames[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public int getCount() {
            return itemNames.length;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            try {
                ViewHolder holder = new ViewHolder();
                if (view == null ) {
                    view = inflater.inflate(R.layout.item_layout, viewGroup, false);
                    holder.title = (TextView) view.findViewById(R.id.item);
                    view.setTag(holder);
                } else {
                    holder = (ViewHolder) view.getTag();
                }

                if (holder != null && holder.title != null) {
                    TextView tv = holder.title;
                    tv.setText(itemNames[i]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return view;
        }

        private class ViewHolder {
            TextView title;
        }
    }
}

转自:http://blog.csdn.net/oyyj42/article/details/47333443
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值