有时候,为了实现项目中的需求,完成设计好的用户交互体验,不的不把这些VIEw重新改造成自己想要的效果。
AndroID原生的ListVIEw是不支持左右滑动的,但是看到微信电话本上,联系人可以左右滑动进行 *** 作的,就通过自己的设想和思路,并加以实现了。
思路:
1.获取到手指放到屏幕时的x,y位置,并判断点击的处于ListVIEw的那个position。
2.判断滑动的方向,如果是上下方向,touch事件就交给ListVIEw处理;如果是左右方向,就禁止ListVIEw进行滑动。
3.根据手指的移动,来移动选中的VIEw。
4.滑动结束后,把VIEw归位。
5.通过传入的监听器,告诉用户是左滑动还是右滑动。
效果图:
重新的ListVIEw的代码:
package com.example.wz.vIEw;import androID.annotation.Suppresslint;import androID.content.Context;import androID.os.Handler;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.VeLocityTracker;import androID.vIEw.VIEw;import androID.vIEw.VIEwConfiguration;import androID.vIEw.WindowManager;import androID.Widget.AdapterVIEw;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;import com.example.wz.R;import com.example.wz.util.MyLog;import com.example.wz.util.OpenLooper;import com.example.wz.util.OpenLooper.LoopCallback;public class MyListVIEw extends ListVIEw { public MyLog log = new MyLog(this,true); public float screenWIDth; public int mtouchSlop; public float density; public MyListVIEw(Context context) { super(context); } public MyListVIEw(Context context,AttributeSet attrs) { this(context,attrs,0); } public float transleteSpeed = 2f; public OpenLooper openLooper = null; public LoopCallback loopCallback = null; @SuppressWarnings("deprecation") public MyListVIEw(Context context,AttributeSet attrs,int defStyle) { super(context,defStyle); this.screenWIDth = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultdisplay().getWIDth(); this.mtouchSlop = VIEwConfiguration.get(getContext()).getScaledtouchSlop(); this.density = context.getResources().getdisplayMetrics().density; this.openLooper = new OpenLooper(); this.openLooper.createOpenLooper(); this.loopCallback = new ListLoopCallback(this.openLooper); this.openLooper.loopCallback = this.loopCallback; } public class BodyStatus { public int None = 0,Down = 1,Move = 2,Up = 3,Homing = 4; int state = None; } public BodyStatus bodyStatus = new BodyStatus(); public class RemoveDirection { public int None = 0,left = 1,Right = 2,Homing_left = 3,Homing_Right = 4; public int state = None; } public RemoveDirection removeDirection = new RemoveDirection(); public class ListLoopCallback extends LoopCallback { public ListLoopCallback(OpenLooper openLooper) { openLooper.super(); } @OverrIDe public voID loop(double ellapsedMillis) { if (bodyStatus.state == bodyStatus.Homing) { goHoming((float) ellapsedMillis); } } } public voID goHoming(float delta) { float distance = (float) delta * transleteSpeed; if (removeDirection.state == removeDirection.left) { float currentX = itemVIEw.getScrollX() + distance; if (currentX > screenWIDth) { distance = distance - (currentX - screenWIDth); } itemVIEw.scrollBy((int) (distance),itemVIEw.getScrollY()); if (itemVIEw.getScrollX() > screenWIDth / 2 + 40 * screenWIDth / 1080) { t2.setTranslationX(itemVIEw.getScrollX() - screenWIDth / 2 - 40 * 3f); } else { t2.setTranslationX(40 * 3f); } } else if (removeDirection.state == removeDirection.Right) { float currentX = itemVIEw.getScrollX() - distance; if (currentX < -screenWIDth) { distance = distance - (Math.abs(currentX) - screenWIDth); } itemVIEw.scrollBy((int) (-distance),itemVIEw.getScrollY()); if (itemVIEw.getScrollX() < -(screenWIDth / 2 + 40 * 3f * 2)) { t1.setTranslationX(-(Math.abs(itemVIEw.getScrollX()) - screenWIDth / 2 - 40 * 3f)); } else { t1.setTranslationX(-40 * 3f); } } else if (removeDirection.state == removeDirection.Homing_left) { float currentX = itemVIEw.getScrollX() - distance; if (currentX < 0) { distance = distance - (Math.abs(currentX)); } itemVIEw.scrollBy((int) (-distance),itemVIEw.getScrollY()); } else if (removeDirection.state == removeDirection.Homing_Right) { float currentX = itemVIEw.getScrollX() + distance; if (currentX > 0) { distance = distance - currentX; } itemVIEw.scrollBy((int) (distance),itemVIEw.getScrollY()); } if (itemVIEw.getScrollX() == 0 || itemVIEw.getScrollX() >= screenWIDth || itemVIEw.getScrollX() <= -screenWIDth) { openLooper.stop(); if (itemVIEw.getScrollX() >= screenWIDth) { mRemoveListener.removeItem(removeDirection,position); } else if (itemVIEw.getScrollX() <= -screenWIDth) { mRemoveListener.removeItem(removeDirection,position); } new Handler().postDelayed(new Runnable() { @OverrIDe public voID run() { itemVIEw.scrollTo(0,itemVIEw.getScrollY()); bodyStatus.state = bodyStatus.None; } },300); } } public int touch_down_x; public int touch_down_y; public VIEw itemVIEw; public TextVIEw t1; public TextVIEw t2; public int SNAP_VELociTY = 800; public int position; @OverrIDe public boolean dispatchtouchEvent(MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { // addVeLocityTracker(event); if (bodyStatus.state != bodyStatus.None) { return super.dispatchtouchEvent(event); } this.touch_down_x = (int) event.getX(); this.touch_down_y = (int) event.getY(); position = pointToposition(touch_down_x,touch_down_y); if (position == AdapterVIEw.INVALID_position) { return super.dispatchtouchEvent(event); } itemVIEw = getChildAt(position - getFirstVisibleposition()); t2 = (TextVIEw) itemVIEw.findVIEwByID(R.ID.t2); t1 = (TextVIEw) itemVIEw.findVIEwByID(R.ID.t1); } else if (action == MotionEvent.ACTION_MOVE) { if (Math.abs(getScrollVeLocity()) > SNAP_VELociTY || (Math.abs(event.getX() - touch_down_x) > mtouchSlop && Math.abs(event.getY() - touch_down_y) < mtouchSlop)) { isSlIDe = true; } } else if (action == MotionEvent.ACTION_UP) { int veLocityX = getScrollVeLocity(); if (Math.abs(veLocityX) > SNAP_VELociTY) { if (veLocityX > SNAP_VELociTY) { bodyStatus.state = bodyStatus.Homing; removeDirection.state = removeDirection.Right; openLooper.start(); } else { bodyStatus.state = bodyStatus.Homing; removeDirection.state = removeDirection.left; openLooper.start(); } } else { if (itemVIEw.getScrollX() >= screenWIDth / 2) { bodyStatus.state = bodyStatus.Homing; removeDirection.state = removeDirection.left; openLooper.start(); } else if (itemVIEw.getScrollX() <= -screenWIDth / 2) { bodyStatus.state = bodyStatus.Homing; removeDirection.state = removeDirection.Right; openLooper.start(); } else { if (itemVIEw.getScrollX() < 0) { removeDirection.state = removeDirection.Homing_Right; } else { removeDirection.state = removeDirection.Homing_left; } bodyStatus.state = bodyStatus.Homing; openLooper.start(); } } recycleVeLocityTracker(); isSlIDe = false; } return super.dispatchtouchEvent(event); } public boolean isSlIDe = false; @Suppresslint("Recycle") @OverrIDe public boolean ontouchEvent(MotionEvent event) { if (isSlIDe && position != AdapterVIEw.INVALID_position) { requestdisallowIntercepttouchEvent(true); addVeLocityTracker(event); int x = (int) event.getX(); int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { } else if (action == MotionEvent.ACTION_MOVE) { MotionEvent cancelEvent = MotionEvent.obtain(event); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); ontouchEvent(cancelEvent); int deltaX = touch_down_x - x; touch_down_x = x; if (itemVIEw.getScrollX() > screenWIDth / 2 + 40 * 3f * 2) { t2.setTranslationX(itemVIEw.getScrollX() - screenWIDth / 2 - 40 * 3f); } else { t2.setTranslationX(40 * 3f); } if (itemVIEw.getScrollX() < -(screenWIDth / 2 + 40 * 3f * 2)) { t1.setTranslationX(-(Math.abs(itemVIEw.getScrollX()) - screenWIDth / 2 - 40 * 3f)); } else { t1.setTranslationX(-40 * 3f); } itemVIEw.scrollBy(deltaX,0); return true; } } return super.ontouchEvent(event); } public RemoveListener mRemoveListener; public voID setRemoveListener(RemoveListener removeListener) { this.mRemoveListener = removeListener; } public interface RemoveListener { public voID removeItem(RemoveDirection direction,int position); } public VeLocityTracker veLocityTracker; public voID addVeLocityTracker(MotionEvent event) { if (veLocityTracker == null) { veLocityTracker = VeLocityTracker.obtain(); } veLocityTracker.addMovement(event); } public int getScrollVeLocity() { if (veLocityTracker != null) { veLocityTracker.computeCurrentVeLocity(1000); int veLocity = (int) veLocityTracker.getXVeLocity(); return veLocity; } return 0; } public voID recycleVeLocityTracker() { if (veLocityTracker != null) { veLocityTracker.recycle(); veLocityTracker = null; } }}
测试ListVIEw的Activity代码:
package com.example.wz;import java.util.ArrayList;import androID.app.Activity;import androID.os.Bundle;import androID.util.displayMetrics;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.TextVIEw;import com.example.wz.vIEw.MyListVIEw;import com.example.wz.vIEw.MyListVIEw.RemoveDirection;import com.example.wz.vIEw.MyListVIEw.RemoveListener;public class TestListActivity extends Activity { LayoutInflater mInflater; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { displayMetrics = new displayMetrics(); this.getwindowManager().getDefaultdisplay().getMetrics(displayMetrics); super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_testList); mInflater = getLayoutInflater(); ListVIEw = (MyListVIEw) findVIEwByID(R.ID.slIDeCutListVIEw); showAddressDialog(); } public displayMetrics displayMetrics; MyListVIEw ListVIEw; public ArrayList<String> items; public voID showAddressDialog() { items = new ArrayList<String>() { { add("item...1"); add("item...2"); add("item...3"); add("item...4"); add("item...5"); add("item...6"); add("item...7"); add("item...8"); add("item...9"); add("item...10"); add("item...11"); add("item...12"); add("item...13"); add("item...14"); add("item...15"); add("item...16"); add("item...17"); add("item...18"); add("item...19"); add("item...20"); } }; NearbyRelationAdapter nearbyRelationAdapter = new NearbyRelationAdapter(); ListVIEw.setAdapter(nearbyRelationAdapter); ListVIEw.setRemoveListener(new RemoveListener() { @OverrIDe public voID removeItem(RemoveDirection direction,int position) { if (direction.state == direction.left) { Log.e("A","left" + "-" + position); } else if (direction.state == direction.Right) { Log.e("A","right" + "-" + position); } } }); } public class NearbyRelationAdapter extends BaseAdapter { @OverrIDe public int getCount() { return items.size(); } @OverrIDe public Object getItem(int posotion) { return items.get(posotion); } @OverrIDe public long getItemID(int posotion) { return posotion; } @OverrIDe public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) { Holder holder = null; if (convertVIEw == null) { holder = new Holder(); convertVIEw = mInflater.inflate(R.layout.vIEw_menu_item1,null); holder.Title = (TextVIEw) convertVIEw.findVIEwByID(R.ID.Title); holder.o1 = convertVIEw.findVIEwByID(R.ID.o1); holder.o1.setTranslationX(-displayMetrics.wIDthPixels); holder.o2 = convertVIEw.findVIEwByID(R.ID.o2); holder.o2.setTranslationX(displayMetrics.wIDthPixels); convertVIEw.setTag(holder); } else { holder = (Holder) convertVIEw.getTag(); } holder.Title.setText(items.get(position)); convertVIEw.scrollTo(0,convertVIEw.getScrollY()); return convertVIEw; } class Holder { TextVIEw Title; VIEw o1,o2; } }}
ListVIEw布局文件:
<?xml version="1.0" enCoding="utf-8"?><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:background="@androID:color/darker_gray" > <com.example.wz.vIEw.MyListVIEw androID:ID="@+ID/slIDeCutListVIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:cachecolorHint="@androID:color/transparent" androID:ListSelector="@androID:color/transparent" > </com.example.wz.vIEw.MyListVIEw> <TextVIEw androID:layout_wIDth="1dp" androID:layout_height="match_parent" androID:layout_centerHorizontal="true" androID:background="#0099cd" /></relativeLayout>
ListVIEw 的Item布局:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="#fff" > <relativeLayout androID:ID="@+ID/o1" androID:layout_wIDth="match_parent" androID:layout_height="60dp" androID:background="#ff0000" > <TextVIEw androID:ID="@+ID/t1" androID:layout_wIDth="80dp" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:gravity="center" androID:padding="10dp" androID:text="删除" androID:textcolor="#99000000" androID:textSize="18sp" /> </relativeLayout> <relativeLayout androID:ID="@+ID/o2" androID:layout_wIDth="match_parent" androID:layout_height="60dp" androID:background="#660099cd" > <TextVIEw androID:ID="@+ID/t2" androID:layout_wIDth="80dp" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:layout_centerVertical="true" androID:gravity="center" androID:padding="10dp" androID:text="编辑" androID:textcolor="#99000000" androID:textSize="18sp" /> </relativeLayout> <relativeLayout androID:ID="@+ID/r1" androID:layout_wIDth="match_parent" androID:layout_height="60dp" androID:background="#fff" > <TextVIEw androID:ID="@+ID/Title" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:gravity="center_vertical" androID:textcolor="#99000000" androID:textSize="18sp" /> </relativeLayout></relativeLayout>
代码中用到的其他的类,在上一篇文章中有: https://www.oudahe.com/p/23078/
转载来自:http://blog.csdn.net/qxs965266509
以上就是本文的全部内容,希望对大家的学习有所帮助。
总结以上是内存溢出为你收集整理的Android实现ListView左右滑动删除和编辑全部内容,希望文章能够帮你解决Android实现ListView左右滑动删除和编辑所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)