标签:android style blog http color java 使用 os
关于ListView拖拽移动位置,想必大家并不陌生,比较不错的软件都用到如此功能了.如:搜狐,网易,百度等,但是相比来说还是百度的用户体验较好,不偏心了,下面看几个示例:
首先说一下:拖拽ListView的item就不应该可以任意移动,只应该在
ListView所在的范围内,而网易的你看看我都可以移动到状态栏了,虽然你做了处理,但是用户体验我个人感觉不好,在看看百度的,不仅控制了移动范
围,更不错的百度的移动起来会时时的换位,看起来相当的形象,所以我认为这样相当的棒.
说明一点,我没有那么有才,我也是看别人代码,然后自己整理下.在这里就简单记载一下.
首先对touch事件的处理,从应用中,我们可以得出,在我们点击后面拖拉图标后,就会创建一个item的影像视图.并且可以移动该影像,而此时的ListView不应该有touch事件.
onInterceptTouchEvent方法.
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
-
- if (ev.getAction() == MotionEvent.ACTION_DOWN) {
- int x = (int) ev.getX();
- int y = (int) ev.getY();
- dragSrcPosition = dragPosition = pointToPosition(x, y);
-
- if (dragPosition == AdapterView.INVALID_POSITION) {
- return super.onInterceptTouchEvent(ev);
- }
-
-
- ViewGroup itemView = (ViewGroup) getChildAt(dragPosition
- - getFirstVisiblePosition());
-
-
- dragPoint = y - itemView.getTop();
-
- dragOffset = (int) (ev.getRawY() - y);
-
-
- View dragger = itemView.findViewById(R.id.iv_drag_list_item_2);
-
-
- if (dragger != null && x > dragger.getLeft() - 20) {
-
- upScrollBounce = getHeight() / 3;
- downScrollBounce = getHeight() * 2 / 3;
-
- itemView.setDrawingCacheEnabled(true);
- Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache());
- startDrag(bm, y);
- }
-
- }
-
- return super.onInterceptTouchEvent(ev);
- }
这个方法的作用很简单:当我们摁下的如果是可拖拽的图标,那么进行初始化该Item的映像试图.
而在这里如果大家对WindowManager和WindowManager.LayoutParams不熟悉的朋友先去参考下这篇文章,要对WindowManager有一定的了解,简单的会应用.
接下来我们看onTouchEvent事件:
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
-
- if (dragImageView != null && dragPosition != INVALID_POSITION) {
- int action = ev.getAction();
- switch (action) {
- case MotionEvent.ACTION_UP:
- int upY = (int) ev.getY();
- stopDrag();
- onDrop(upY);
- break;
- case MotionEvent.ACTION_MOVE:
- int moveY = (int) ev.getY();
- onDrag(moveY);
-
- break;
- case MotionEvent.ACTION_DOWN:
- break;
- default:
- break;
- }
- return true;
- }
-
- return super.onTouchEvent(ev);
- }
简单说明:首先在Touch中,我们要进行判断,是否点击的是拖动图标,如果是的话,那么对
ACTION_MOVE and ACTION_UP相应事件进行处理,并且返回true or
false.作用:取消ListView自身的Touch事件.如果不是的话,执行ListView 本身的Touch事件.
大致就介绍这么多,具体的实现,还是大家看源码吧,我注释的还算清晰,只要大家仔细看的话,一定可以掌握的,为什么这么说呢,技术只有在掌握了情况下才可以进行拓展.
对了,提醒大家要理解这三句话:
getRawX()和getRawY():获得的是相对屏幕的位置.
getX()和getY():获得的永远是相对view的触摸位置
坐标(这两个值不会超过view的长度和宽度)。
, getTop,getRight源码:
- package com.jj.drag;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.os.AsyncTask;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewConfiguration;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.AdapterView;
- import android.widget.ImageView;
- import android.widget.ListView;
-
- import com.jj.drag.MainActivity.DragListAdapter;
-
- public class DragListView extends ListView {
-
- private WindowManager windowManager;
- private WindowManager.LayoutParams windowParams;
-
- private int scaledTouchSlop;
-
- private ImageView dragImageView;
- private int dragSrcPosition;
- private int dragPosition;
-
- private int dragPoint;
- private int dragOffset;
-
- private int upScrollBounce;
- private int downScrollBounce;
-
- private final static int step = 1;
-
- private int current_Step;
-
-
- public DragListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
-
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
-
- if (ev.getAction() == MotionEvent.ACTION_DOWN) {
- int x = (int) ev.getX();
- int y = (int) ev.getY();
- dragSrcPosition = dragPosition = pointToPosition(x, y);
-
- if (dragPosition == AdapterView.INVALID_POSITION) {
- return super.onInterceptTouchEvent(ev);
- }
-
-
- ViewGroup itemView = (ViewGroup) getChildAt(dragPosition
- - getFirstVisiblePosition());
-
-
- dragPoint = y - itemView.getTop();
-
- dragOffset = (int) (ev.getRawY() - y);
-
-
- View dragger = itemView.findViewById(R.id.iv_drag_list_item_2);
-
-
- if (dragger != null && x > dragger.getLeft() - 20) {
-
- upScrollBounce = getHeight() / 3;
- downScrollBounce = getHeight() * 2 / 3;
-
- itemView.setDrawingCacheEnabled(true);
- Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache());
- startDrag(bm, y);
- }
- }
-
- return super.onInterceptTouchEvent(ev);
- }
-
-
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
-
- if (dragImageView != null && dragPosition != INVALID_POSITION) {
- int action = ev.getAction();
- switch (action) {
- case MotionEvent.ACTION_UP:
- int upY = (int) ev.getY();
- stopDrag();
- onDrop(upY);
- break;
- case MotionEvent.ACTION_MOVE:
- int moveY = (int) ev.getY();
- onDrag(moveY);
- break;
- case MotionEvent.ACTION_DOWN:
- break;
- default:
- break;
- }
- return true;
- }
-
- return super.onTouchEvent(ev);
- }
-
-
- private void startDrag(Bitmap bm, int y) {
-
-
- windowParams = new WindowManager.LayoutParams();
- windowParams.gravity = Gravity.TOP;
- windowParams.x = 0;
- windowParams.y = y - dragPoint + dragOffset;
- windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
-
- windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
- | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
- | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
- | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
-
-
- windowParams.windowAnimations = 0;
-
- ImageView imageView = new ImageView(getContext());
- imageView.setImageBitmap(bm);
- windowManager = (WindowManager) getContext().getSystemService("window");
- windowManager.addView(imageView, windowParams);
- dragImageView = imageView;
-
- }
-
-
- public void onDrag(int y) {
- int drag_top = y - dragPoint;
- if (dragImageView != null && drag_top >= 0) {
- windowParams.alpha = 0.5f;
- windowParams.y = y - dragPoint + dragOffset;
- windowManager.updateViewLayout(dragImageView, windowParams);
- }
-
- int tempPosition = pointToPosition(0, y);
- if (tempPosition != INVALID_POSITION) {
- dragPosition = tempPosition;
-
- }
- doScroller(y);
- }
-
-
-
- public void doScroller(int y) {
- Log.e("jj", "y=" + y);
- Log.e("jj", "upScrollBounce=" + upScrollBounce);
-
- if (y < upScrollBounce) {
- current_Step = step + (upScrollBounce - y) / 10;
- }
- else if (y > downScrollBounce) {
- current_Step = -(step + (y - downScrollBounce)) / 10;
- } else {
- current_Step = 0;
- }
-
-
- View view = getChildAt(dragPosition - getFirstVisiblePosition());
-
- setSelectionFromTop(dragPosition, view.getTop() + current_Step);
-
- }
-
-
- public void stopDrag() {
- if (dragImageView != null) {
- windowManager.removeView(dragImageView);
- dragImageView = null;
- }
- }
-
-
- public void onDrop(int y) {
-
-
- int tempPosition = pointToPosition(0, y);
- if (tempPosition != INVALID_POSITION) {
- dragPosition = tempPosition;
- }
-
-
- if (y < getChildAt(0).getTop()) {
-
- dragPosition = 0;
-
- } else if (y > getChildAt(getChildCount() - 1).getBottom()) {
-
- dragPosition = getAdapter().getCount() - 1;
- }
-
-
- if (dragPosition < getAdapter().getCount()) {
- DragListAdapter adapter = (DragListAdapter) getAdapter();
- adapter.update(dragSrcPosition, dragPosition);
- }
-
- }
-
- }
下面我说下适配器:
- class DragListAdapter extends BaseAdapter {
- private ArrayList<String> arrayTitles;
- private ArrayList<Integer> arrayDrawables;
- private Context context;
-
- public DragListAdapter(Context context, ArrayList<String> arrayTitles,
- ArrayList<Integer> arrayDrawables) {
- this.context = context;
- this.arrayTitles = arrayTitles;
- this.arrayDrawables = arrayDrawables;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = convertView;
-
- view = LayoutInflater.from(context).inflate(
- R.layout.drag_list_item, null);
-
- TextView textView = (TextView) view
- .findViewById(R.id.tv_drag_list_item_text);
- ImageView imageView = (ImageView) view
- .findViewById(R.id.iv_drag_list_item_1);
- imageView.setImageResource(arrayDrawables.get(position));
- textView.setText(arrayTitles.get(position));
- return view;
- }
-
-
- public void update(int start, int down) {
-
- String title = arrayTitles.get(start);
- int drawable_id = arrayDrawables.get(start);
-
- arrayTitles.remove(start);
- arrayDrawables.remove(start);
-
- arrayTitles.add(down, title);
- arrayDrawables.add(down, drawable_id);
-
- notifyDataSetChanged();
- }
-
- @Override
- public int getCount() {
-
- return Title.length;
- }
-
- @Override
- public Object getItem(int position) {
- return Title[position];
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- }
这里不过多解释了,相信大家都看的明白.如果疑问请留言.
展示下运行效果:
效果看起来还行吧,如果觉得不错的话,记得要赞一个哦.
下面我们接着修改,模拟百度嘛,谁让百度这么牛叉呢.
思路:点中拖拉图标的时候,每次移动只要dragPosition发生改变,也就是我移动到了下一个位置,那么此时我就进行交换执行update.并且除了第一次移动外,在每次交换后要除去映射源的显示,这样用户觉得这里的空位就是就是为我准备的,比较人性化.
实现起来并不复杂,前提是你得掌握上面的操作.
源码如下;
- package com.jj.drag;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.os.AsyncTask;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewConfiguration;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.AdapterView;
- import android.widget.ImageView;
- import android.widget.ListView;
-
- import com.jj.drag.MainActivity.DragListAdapter;
-
- public class DragListView extends ListView {
-
- private WindowManager windowManager;
- private WindowManager.LayoutParams windowParams;
-
- private int scaledTouchSlop;
-
- private ImageView dragImageView;
- private int dragSrcPosition;
- private int dragPosition;
-
- private int dragPoint;
- private int dragOffset;
-
- private int upScrollBounce;
- private int downScrollBounce;
-
- private final static int step = 1;
-
- private int current_Step;
-
- private int temChangId;
-
- private boolean isLock;
-
- public void setLock(boolean isLock) {
- this.isLock = isLock;
- }
-
- public DragListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
- }
-
-
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
-
- if (ev.getAction() == MotionEvent.ACTION_DOWN && !isLock) {
- int x = (int) ev.getX();
- int y = (int) ev.getY();
- temChangId = dragSrcPosition = dragPosition = pointToPosition(x, y);
-
- if (dragPosition == AdapterView.INVALID_POSITION) {
- return super.onInterceptTouchEvent(ev);
- }
-
-
- ViewGroup itemView = (ViewGroup) getChildAt(dragPosition
- - getFirstVisiblePosition());
-
-
- dragPoint = y - itemView.getTop();
-
- dragOffset = (int) (ev.getRawY() - y);
-
-
- View dragger = itemView.findViewById(R.id.iv_drag_list_item_2);
-
-
- if (dragger != null && x > dragger.getLeft() - 20) {
-
- upScrollBounce = getHeight() / 3;
- downScrollBounce = getHeight() * 2 / 3;
- itemView.setBackgroundColor(Color.BLUE);
- itemView.setDrawingCacheEnabled(true);
- Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache());
- startDrag(bm, y);
- }
- return false;
- }
-
- return super.onInterceptTouchEvent(ev);
- }
-
-
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
-
- if (dragImageView != null && dragPosition != INVALID_POSITION
- && !isLock) {
- int action = ev.getAction();
- switch (action) {
- case MotionEvent.ACTION_UP:
- int upY = (int) ev.getY();
- stopDrag();
- onDrop(upY);
- break;
- case MotionEvent.ACTION_MOVE:
- int moveY = (int) ev.getY();
- onDrag(moveY);
-
- break;
- case MotionEvent.ACTION_DOWN:
- break;
- default:
- break;
- }
- return true;
- }
-
- return super.onTouchEvent(ev);
- }
-
-
- private void startDrag(Bitmap bm, int y) {
-
-
- windowParams = new WindowManager.LayoutParams();
- windowParams.gravity = Gravity.TOP;
- windowParams.x = 0;
- windowParams.y = y - dragPoint + dragOffset;
- windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
-
- windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
- | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
- | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
- | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
-
-
- windowParams.windowAnimations = 0;
-
- ImageView imageView = new ImageView(getContext());
- imageView.setImageBitmap(bm);
- windowManager = (WindowManager) getContext().getSystemService("window");
- windowManager.addView(imageView, windowParams);
- dragImageView = imageView;
-
- }
-
-
- public void onDrag(int y) {
- int drag_top = y - dragPoint;
- if (dragImageView != null && drag_top >= 0) {
- windowParams.alpha = 0.5f;
- windowParams.y = y - dragPoint + dragOffset;
- windowManager.updateViewLayout(dragImageView, windowParams);
- }
-
- int tempPosition = pointToPosition(0, y);
- if (tempPosition != INVALID_POSITION) {
- dragPosition = tempPosition;
- }
-
- onChange(y);
-
- doScroller(y);
- }
-
-
-
- public void doScroller(int y) {
-
-
-
- if (y < upScrollBounce) {
- current_Step = step + (upScrollBounce - y) / 10;
- }
- else if (y > downScrollBounce) {
- current_Step = -(step + (y - downScrollBounce)) / 10;
- } else {
- current_Step = 0;
- }
-
-
- View view = getChildAt(dragPosition - getFirstVisiblePosition());
-
- setSelectionFromTop(dragPosition, view.getTop() + current_Step);
-
- }
-
-
- public void stopDrag() {
- if (dragImageView != null) {
- windowManager.removeView(dragImageView);
- dragImageView = null;
- }
- }
-
-
- private void onChange(int y) {
-
- if (dragPosition < getAdapter().getCount()) {
- DragListAdapter adapter = (DragListAdapter) getAdapter();
- adapter.isHidden = false;
- if (dragPosition != temChangId) {
- adapter.update(temChangId, dragPosition);
- temChangId = dragPosition;
- }
- }
-
-
- int tempPosition = pointToPosition(0, y);
- if (tempPosition != INVALID_POSITION) {
- dragPosition = tempPosition;
- }
-
-
- if (y < getChildAt(0).getTop()) {
-
- dragPosition = 0;
-
- } else if (y > getChildAt(getChildCount() - 1).getBottom()) {
-
- dragPosition = getAdapter().getCount() - 1;
- }
-
- }
-
-
- public void onDrop(int y) {
-
- if (dragPosition < getAdapter().getCount()) {
- DragListAdapter adapter = (DragListAdapter) getAdapter();
- adapter.isHidden = false;
- adapter.notifyDataSetChanged();
- }
- }
-
- }
因为我们要时时交换位置,所以将原先的拖动方法onDrop方法移动到onChange中.具体的还是看源码吧.
另外的就是对适配器的修改,因为你要对特殊的item进行隐藏之类的操作,这些代码我就不写了,我会将案例上传网上,不懂的可以下载源码.
好了还是我们来观看下效果吧.
怎么样,这个效果看起来要比上面那个效果更人性化点吧,我的操作或许有点快,不信的话,你自己手机体验一下吧.
关于ListView拖拽就说到这里,如有不足请大家自己创新.
下面我们接着对GridView的拖拽简单说明.因为这些在项目中我们都会用到,所以既然做到就做全面点吧.好了大家接着往下看吧.
首先说明,原理一样,都是拖动映像,记录拖动位置,然后调用notifyDataSetChanged更新UI.
而GridView不同的是你要根据x,y值共同获取点击的position和移动至的position,而ListView因为不涉及x坐标.
嗯,最初的原始移动我就不给大家展示了,效果也不是很友好,我直接展示时时更新的那种方法.效果类是与上面那个时时更新ListView一样。
原理也一样.下面我们直接看代码吧.
- package com.jj.draggrid;
-
- import java.util.logging.Handler;
-
- import com.jj.draggrid.MainActivity.DragGridAdapter;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.PixelFormat;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.Toast;
-
-
- public class DragGridView extends GridView {
-
- private WindowManager windowManager;
- private WindowManager.LayoutParams windowParams;
-
- private int scaledTouchSlop;
-
- private ImageView dragImageView;
- private int dragSrcPosition;
- private int dragPosition;
-
- private int dragPointX;
- private int dragPointY;
- private int dragOffsetX;
- private int dragOffsetY;
-
- private int upScrollBounce;
- private int downScrollBounce;
-
- private int temChangId;
-
- private boolean isDoTouch = false;
-
- private boolean isHide = false;
-
- private Handler handler;
-
- public void setDoTouch(boolean isDoTouch) {
- this.isDoTouch = isDoTouch;
- }
-
- public DragGridView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
-
- if (ev.getAction() == MotionEvent.ACTION_DOWN) {
- int x = (int) ev.getX();
- int y = (int) ev.getY();
-
- temChangId = dragSrcPosition = dragPosition = pointToPosition(x, y);
-
- if (dragPosition == AdapterView.INVALID_POSITION) {
- return super.onInterceptTouchEvent(ev);
- }
-
- ViewGroup itemView = (ViewGroup) getChildAt(dragPosition
- - getFirstVisiblePosition());
-
- dragPointX = x - itemView.getLeft();
- dragPointY = y - itemView.getTop();
- dragOffsetX = (int) (ev.getRawX() - x);
- dragOffsetY = (int) (ev.getRawY() - y);
-
- View dragger = itemView.findViewById(R.id.drag_grid_item);
-
-
- if (dragger != null && dragPointX > dragger.getLeft()
- && dragPointX < dragger.getRight()
- && dragPointY > dragger.getTop()
- && dragPointY < dragger.getBottom() + 20) {
-
- upScrollBounce = getHeight() / 4;
- downScrollBounce = getHeight() * 3 / 4;
-
- itemView.setDrawingCacheEnabled(true);
- Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache());
- startDrag(bm, x, y);
-
- dragger.setVisibility(View.INVISIBLE);
- }
- }
-
- return super.onInterceptTouchEvent(ev);
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
-
- if (dragImageView != null && dragPosition != INVALID_POSITION
- && isDoTouch) {
- int action = ev.getAction();
- switch (action) {
-
- case MotionEvent.ACTION_UP:
- int upX = (int) ev.getX();
- int upY = (int) ev.getY();
- stopDrag();
- onDrop(upX, upY);
-
- break;
-
- case MotionEvent.ACTION_MOVE:
- int moveX = (int) ev.getX();
- int moveY = (int) ev.getY();
- onDrag(moveX, moveY);
- break;
- case MotionEvent.ACTION_DOWN:
- int downX = (int) ev.getX();
- int downY = (int) ev.getY();
- onHide(downX, downY);
- break;
- default:
- break;
- }
- return true;
- }
-
- return super.onTouchEvent(ev);
- }
-
-
- public void startDrag(Bitmap bm, int x, int y) {
-
- windowParams = new WindowManager.LayoutParams();
- windowParams.gravity = Gravity.TOP | Gravity.LEFT;
- windowParams.x = x - dragPointX + dragOffsetX;
- windowParams.y = y - dragPointY + dragOffsetY;
- windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
- windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
- | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
- | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
- | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
-
- windowParams.windowAnimations = 0;
-
- ImageView imageView = new ImageView(getContext());
- imageView.setImageBitmap(bm);
- windowManager = (WindowManager) getContext().getSystemService("window");
- windowManager.addView(imageView, windowParams);
- dragImageView = imageView;
-
- }
-
-
- private void onChange(int x, int y) {
-
- DragGridAdapter adapter = (DragGridAdapter) getAdapter();
-
- if (dragPosition < getAdapter().getCount()) {
-
- if (dragPosition != temChangId) {
- adapter.update(temChangId, dragPosition);
- temChangId = dragPosition;
- }
-
- }
-
-
- int tempPosition = pointToPosition(x, y);
- if (tempPosition != INVALID_POSITION) {
- dragPosition = tempPosition;
- }
-
- }
-
-
- public void onDrag(int x, int y) {
-
- if (dragImageView != null) {
- windowParams.alpha = 0.8f;
- windowParams.x = x - dragPointX + dragOffsetX;
- windowParams.y = y - dragPointY + dragOffsetY;
- windowManager.updateViewLayout(dragImageView, windowParams);
- }
-
- onChange(x, y);
-
-
- if (y < upScrollBounce || y > downScrollBounce) {
-
- setSelection(dragPosition);
- }
-
- }
-
-
- private void onHide(int x, int y) {
-
- DragGridAdapter adapter = (DragGridAdapter) getAdapter();
-
- int tempPosition = pointToPosition(x, y);
- if (tempPosition != INVALID_POSITION) {
- dragPosition = tempPosition;
- }
- adapter.setIsHidePosition(dragPosition);
-
- }
-
-
- public void stopDrag() {
- if (dragImageView != null) {
- windowManager.removeView(dragImageView);
- dragImageView = null;
- }
- }
-
-
- public void onDrop(int x, int y) {
-
- DragGridAdapter adapter = (DragGridAdapter) getAdapter();
- adapter.setIsHidePosition(-1);
-
- }
-
- }
相信大家只要ListView拖拽弄白后,这个GridView也会轻易弄出来,其实拖拽就是对坐标的考察。
向大家展示一下效果:
但是有个不足的地方,网上一些例子都是长按可以拖拽,而点击则执行点击事件.其实实现起来也不是很复杂,可是在实现的过程中,遇到了诡异纠结的问题,郁闷了一天,结果目前先放弃,以后哪天在搞搞吧.纠结的问题就是错位.
我说下我的思路:首先,我们在自定义GridView中创建一个控制是否可以Touch拖拽的变量,而这个变量的值我们通过对GridView的setOnItemClickListener和setOnItemLongClickListener来获取,
如:
- gv_main.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- gv_main.setDoTouch(false);
- Toast.makeText(MainActivity.this,
- adapter.getItemId(position) + "", 1).show();
- }
- });
-
- gv_main.setOnItemLongClickListener(new OnItemLongClickListener() {
-
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view,
- int position, long id) {
- gv_main.setDoTouch(true);
- return true;
- }
- });
这样我们就实现了长按可以拖拽的效果了,可是遇到个变态的问题,不过这个思路没有错,肯定可以实现.就
先说到这里,其实通过这个例子,我们还可以拓展实现ListView上滑动的时候,到达Title时,Title停留在顶部,当下一个Titile滑动到
这里的时候,那么代替前面那个TItle.网上有写应该就是这么搞的,具体实现不知道,不过这种方案可以实现,有时间接着续.
android ListView和GridView拖拽移位具体实现及拓展,布布扣,bubuko.com
android ListView和GridView拖拽移位具体实现及拓展
标签:android style blog http color java 使用 os
原文地址:http://www.cnblogs.com/Free-Thinker/p/3895156.html