@Override public View getViewForLocation(int x, int y) { getLocationOnScreen(mLocationOnScreen); // Calculate the X and Y coordinates of the drag event relative to the view final int viewX = x - mLocationOnScreen[0]; final int viewY = y - mLocationOnScreen[1]; View child = getViewAtPosition(viewX, viewY); return child; }
private void ensureScrollHandler() { if (mScrollHandler == null) { mScrollHandler = getHandler(); } }
/** * @return True if the drag is started, false if the drag is cancelled for some reason. */ boolean handleDragStarted(int x, int y) { final View tileView = mDragItemContainer.getViewForLocation(x, y); if (tileView == null) { return false; } for (int i = 0; i < mOnDragDropListeners.size(); i++) { mOnDragDropListeners.get(i).onDragStarted(x, y, tileView); } return true; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple); mSimpleTileAdapter = new SimpleTileAdapter(this, this, new TileView.OnSelectedListener() { @Override public void onTileSelected(IDragEntity entity) { Toast.makeText(SimpleActivity.this, ((SimpleDragEntity) entity).getName(), Toast.LENGTH_SHORT).show(); } }); //TODO :set data source mSimpleTileAdapter.setData(obtainData()); mListView = (DragDropListView) findViewById(R.id.list_view); mListView.setVerticalScrollBarEnabled(false); mListView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_RIGHT); mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY); mListView.getDragDropController().addOnDragDropListener(mSimpleTileAdapter); final ImageView dragShadowOverlay = (ImageView) findViewById(R.id.tile_drag_shadow_overlay); mListView.setDragShadowOverlay(dragShadowOverlay); final LayoutAnimationController controller = new LayoutAnimationController( AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); controller.setDelay(0); mListView.setLayoutAnimation(controller); mListView.setAdapter(mSimpleTileAdapter); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_thumbtack); mThumbtackTileAdapter = new ThumbtackTileAdapter(this, this, new TileView.OnSelectedListener() { @Override public void onTileSelected(IDragEntity entity) { Toast.makeText(ThumbtackActivity.this, ((SimpleDragEntity) entity).getName(), Toast.LENGTH_SHORT).show(); } }); //TODO :set data source mThumbtackTileAdapter.setData(obtainData()); mListView = (DragDropListView) findViewById(R.id.list_view); mListView.setVerticalScrollBarEnabled(false); mListView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_RIGHT); mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY); mListView.getDragDropController().addOnDragDropListener(mThumbtackTileAdapter); final ImageView dragShadowOverlay = (ImageView) findViewById(R.id.tile_drag_shadow_overlay); mListView.setDragShadowOverlay(dragShadowOverlay); final LayoutAnimationController controller = new LayoutAnimationController( AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); controller.setDelay(0); mListView.setLayoutAnimation(controller); mListView.setAdapter(mThumbtackTileAdapter); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_single_row); mSimpleTileAdapter = new SimpleTileAdapter(this, this, new TileView.OnSelectedListener() { @Override public void onTileSelected(IDragEntity entity) { Toast.makeText(SingleRowActivity.this, ((SimpleDragEntity) entity).getName(), Toast.LENGTH_SHORT).show(); } }); //TODO :set data source mSimpleTileAdapter.setData(obtainData()); mListView = (DragDropListView) findViewById(R.id.list_view); mListView.setVerticalScrollBarEnabled(false); mListView.setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_RIGHT); mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY); mListView.getDragDropController().addOnDragDropListener(mSimpleTileAdapter); final ImageView dragShadowOverlay = (ImageView) findViewById(R.id.tile_drag_shadow_overlay); mListView.setDragShadowOverlay(dragShadowOverlay); final LayoutAnimationController controller = new LayoutAnimationController( AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); controller.setDelay(0); mListView.setLayoutAnimation(controller); mListView.setAdapter(mSimpleTileAdapter); }
private void saveOffsets() { final int firstVisiblePosition = mDragDropListener.getDragDropListView().getFirstVisiblePosition(); for (int i = 0; i < mDragDropListener.getDragDropListView().getChildCount(); i++) { final View child = mDragDropListener.getDragDropListView().getChildAt(i); final int position = firstVisiblePosition + i; if (!isIndexInBound(position)) { continue; } final long itemId = getItemId(position); // Log.d(TAG, "Saving itemId: " + itemId + " for listview child " + i + " Top: " // + child.getTop() + " Left: " // + child.getLeft()); mItemIdTopMap.put(itemId, child.getTop()); mItemIdLeftMap.put(itemId, child.getLeft()); } }
return false; if (!mDragDropController.handleDragStarted(eX, eY)) { return false; mDragDropController.handleDragHovered(this, eX, eY); if (!mIsDragScrollerRunning && (Math.abs(mLastDragY - mTouchDownForDragStartY) >= 4 * mTouchSlop)) { mIsDragScrollerRunning = true; ensureScrollHandler(); mScrollHandler.postDelayed(mDragScroller, SCROLL_HANDLER_DELAY_MILLIS); final int boundGap = (int) (getHeight() * BOUND_GAP_RATIO); mTopScrollBound = (getTop() + boundGap); mBottomScrollBound = (getBottom() - boundGap); break; case DragEvent.ACTION_DRAG_EXITED: case DragEvent.ACTION_DRAG_ENDED: case DragEvent.ACTION_DROP: ensureScrollHandler(); mScrollHandler.removeCallbacks(mDragScroller); mIsDragScrollerRunning = false; mDragDropController.handleDragFinished(eX, eY, false);
/** * Mark the tile as drop area by given the item index in {@link #mDragEntries}. * * @param itemIndex Position of the contact in {@link #mDragEntries}. */ private void markDropArea(int itemIndex) { if (mDraggedEntry != null && isIndexInBound(mDragEnteredEntryIndex) && isIndexInBound(itemIndex)) { cacheOffsetsForDataSetChange(); // Remove the old placeholder item and place the new placeholder item. final int oldIndex = mDragEnteredEntryIndex; mDragEntries.remove(mDragEnteredEntryIndex); mDragEnteredEntryIndex = itemIndex; mDragEntries.add(mDragEnteredEntryIndex, BLANK_ENTRY); BLANK_ENTRY.setId(mDraggedEntry.getId()); onDataSetChangedForAnimation(); notifyDataSetChanged(); } }
public void handleDragHovered(View v, int x, int y) { v.getLocationOnScreen(mLocationOnScreen); final int screenX = x + mLocationOnScreen[0]; final int screenY = y + mLocationOnScreen[1]; final View view = mDragItemContainer.getViewForLocation( screenX, screenY); if(view == null){ return; } for (int i = 0; i < mOnDragDropListeners.size(); i++) { mOnDragDropListeners.get(i).onDragHovered(screenX, screenY, view); } }
public void handleDragFinished(int x, int y, boolean isRemoveView) { if (isRemoveView) { for (int i = 0; i < mOnDragDropListeners.size(); i++) { mOnDragDropListeners.get(i).onDroppedOnRemove(); } } for (int i = 0; i < mOnDragDropListeners.size(); i++) { mOnDragDropListeners.get(i).onDragFinished(x, y); } }
/** * Find the view under the pointer. */ private View getViewAtPosition(int x, int y) { final int count = getChildCount(); View child; for (int childIdx = 0; childIdx < count; childIdx++) { child = getChildAt(childIdx); if (y >= child.getTop() && y <= child.getBottom() && x >= child.getLeft() && x <= child.getRight()) { return child; } } return null; }
public DragDropListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DragDropListView); mIsFixedHeight = ta.getBoolean(R.styleable.DragDropListView_fixed_type, false); mAnimationDuration = ta.getInteger(R.styleable.DragDropListView_anim_duration, context.getResources().getInteger(R.integer.fade_duration)); ta.recycle(); mTouchSlop = ViewConfiguration.get(context).getScaledPagingTouchSlop(); mDragDropController.addOnDragDropListener(this); }
final ViewTreeObserver observer = mDragDropListener.getDragDropListView().getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @SuppressWarnings("unchecked")
@Override public void run() { if (mLastDragY <= mTopScrollBound) { smoothScrollBy(-DRAG_SCROLL_PX_UNIT, (int) SCROLL_HANDLER_DELAY_MILLIS); } else if (mLastDragY >= mBottomScrollBound) { smoothScrollBy(DRAG_SCROLL_PX_UNIT, (int) SCROLL_HANDLER_DELAY_MILLIS); } mScrollHandler.postDelayed(this, SCROLL_HANDLER_DELAY_MILLIS); } };
/** * For the top row of tiled contacts, the item id is the position of the row of * contacts. * For frequent contacts, the item id is the maximum number of rows of tiled contacts + * the actual contact id. Since contact ids are always greater than 0, this guarantees that * all items within this adapter will always have unique ids. */ @Override public long getItemId(int position) { return mDragEntries.get(position).getId(); }
@Override protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); }
@Override public void onDragStarted(int x, int y, View tileView) { if (mDragShadowOverlay == null) { return; } mDragShadowOverlay.clearAnimation(); mDragShadowBitmap = createDraggedChildBitmap(tileView); if (mDragShadowBitmap == null) { return; } tileView.getLocationOnScreen(mLocationOnScreen); mDragShadowLeft = mLocationOnScreen[0]; mDragShadowTop = mLocationOnScreen[1]; // To offset the drag shadow position, this looks under finger. mTouchOffsetToChildLeft = x - mDragShadowLeft; mTouchOffsetToChildTop = y - mDragShadowTop; mDragShadowParent.getLocationOnScreen(mLocationOnScreen); mDragShadowLeft -= mLocationOnScreen[0]; mDragShadowTop -= mLocationOnScreen[1]; mDragShadowOverlay.setImageBitmap(mDragShadowBitmap); mDragShadowOverlay.setVisibility(VISIBLE); mDragShadowOverlay.setAlpha(DRAG_SHADOW_ALPHA); mDragShadowOverlay.setX(mDragShadowLeft); mDragShadowOverlay.setY(mDragShadowTop); }
public boolean onPreDraw() { observer.removeOnPreDrawListener(this); final int firstVisiblePosition = mDragDropListener.getDragDropListView().getFirstVisiblePosition(); for (int i = 0; i < mDragDropListener.getDragDropListView().getChildCount(); i++) { final View child = mDragDropListener.getDragDropListView().getChildAt(i); int position = firstVisiblePosition + i; if (!isIndexInBound(position)) { continue; final long itemId = getItemId(position);