Java Code Examples for android.widget.AdapterView#INVALID_POSITION

The following examples show how to use android.widget.AdapterView#INVALID_POSITION . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: UltimateAndroid   File: SwipeLayout.java    License: Apache License 2.0 6 votes vote down vote up
private void performAdapterViewItemClick(MotionEvent e){
    ViewParent t = getParent();
    while(t != null) {
        if(t instanceof AdapterView){
            AdapterView view = (AdapterView)t;
            int p = view.getPositionForView(SwipeLayout.this);
            if( p != AdapterView.INVALID_POSITION &&
                    view.performItemClick(view.getChildAt(p), p, view.getAdapter().getItemId(p)))
                return;
        }else{
            if(t instanceof View && ((View) t).performClick())
                return;
        }
        t = t.getParent();
    }
}
 
Example 2
Source Project: TopGrid   File: DragGrid.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    boolean bool = true;
    if (dragImageView != null && dragPosition != AdapterView.INVALID_POSITION) {
        // 移动时候的对应x,y位置
        bool = super.onTouchEvent(ev);
        int x = (int) ev.getX();
        int y = (int) ev.getY();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getX();
                windowX = (int) ev.getX();
                downY = (int) ev.getY();
                windowY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                onDrag(x, y, (int) ev.getRawX(), (int) ev.getRawY());
                if (!isMoving) {
                    OnMove(x, y);
                }
                if (pointToPosition(x, y) != AdapterView.INVALID_POSITION) {
                    break;
                }
                break;
            case MotionEvent.ACTION_UP:
                stopDrag();
                onDrop(x, y);
                requestDisallowInterceptTouchEvent(false);
                break;

            default:
                break;
        }
    }
    return super.onTouchEvent(ev);
}
 
Example 3
Source Project: Jreader   File: MyGridView.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 交换item,并且控制item之间的显示与隐藏效果
 * @param moveX
 * @param moveY
 */
private void onSwapItem(int moveX, int moveY){
    //获取我们手指移动到的那个item的position
    final int tempPosition = pointToPosition(moveX, moveY);

    //假如tempPosition 改变了并且tempPosition不等于-1,则进行交换
    if(tempPosition != mDragPosition && tempPosition != AdapterView.INVALID_POSITION && mAnimationEnd ){

        mDragAdapter.setHideItem(tempPosition);

        mDragAdapter.reorderItems(mDragPosition, tempPosition);


        final ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                observer.removeOnPreDrawListener(this);
                animateReorder(mDragPosition, tempPosition);
                mDragPosition = tempPosition;  //交换结束更新mDragPosition
                return true;
            }
        } );

    }

}
 
Example 4
Source Project: AndroidSwipeLayout   File: SwipeLayout.java    License: MIT License 5 votes vote down vote up
private void performAdapterViewItemClick() {
    if (getOpenStatus() != Status.Close) return;
    ViewParent t = getParent();
    if (t instanceof AdapterView) {
        AdapterView view = (AdapterView) t;
        int p = view.getPositionForView(SwipeLayout.this);
        if (p != AdapterView.INVALID_POSITION) {
            view.performItemClick(view.getChildAt(p - view.getFirstVisiblePosition()), p, view
                    .getAdapter().getItemId(p));
        }
    }
}
 
Example 5
/**
 * 获取末个HeaderItem位置
 * 无HeaderItem则返回 AdapterView.INVALID_POSITION
 *
 * @return 末个HeaderItem位置
 */
@SuppressWarnings("unused")
public int getLastHeaderItemPosition() {
    // Header
    if (mHeaderItemInfo.size() > 0) {
        return getHeaderViewsCount() + getHeaderItemsCount() - 1;
    } else {
        return AdapterView.INVALID_POSITION;
    }
}
 
Example 6
Source Project: Jreader   File: MyGridView.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void initCreatDragImage(int position) {

        //根据按下的X,Y坐标获取所点击item的position
        mDragPosition = position;
        Log.d("mDagPosition is", "" + mDragPosition);

        if(mDragPosition == AdapterView.INVALID_POSITION){
            return ;
        }

        //根据position获取该item所对应的View
        mStartDragItemView = getChildAt(mDragPosition - getFirstVisiblePosition());

        //获取DragGridView自动向上滚动的偏移量,小于这个值,DragGridView向下滚动
        mDownScrollBorder = getHeight() / 5;
        //获取DragGridView自动向下滚动的偏移量,大于这个值,DragGridView向上滚动
        mUpScrollBorder = getHeight() * 4/5;

        //开启mDragItemView绘图缓存
        mStartDragItemView.setDrawingCacheEnabled(true);

        //获取mDragItemView在缓存中的Bitmap对象
        mDragBitmap = Bitmap.createBitmap(mStartDragItemView.getDrawingCache());
        //这一步很关键,释放绘图缓存,避免出现重复的镜像
        mStartDragItemView.destroyDrawingCache();

    }
 
Example 7
/**
 * 获取HeaderView的集合位置(HeaderView占用的空白位置也算作HeaderView)
 * 返回 AdapterView.INVALID_POSITION 表明其对应的不是HeaderView
 *
 * @param position 位置
 * @return HeaderView的集合位置
 */
public int positionToHeaderViewIndex(int position) {

    if (getHeaderViewsCount() > 0 && position < getHeaderViewsCount()) {
        return position / mNumColumns;
    } else {
        return AdapterView.INVALID_POSITION;
    }
}
 
Example 8
/**
 * 获取FooterView的集合位置(FooterView占用的空白位置也算作FooterView)
 * 返回 AdapterView.INVALID_POSITION 表明其对应的不是FooterView
 *
 * @param position 位置
 * @return FooterView的集合位置
 */
public int positionToFooterViewIndex(int position) {
    int startPosition = getHeaderViewsCount() + getHeaderItemsCount()
            + getItemCount() + getFooterItemsCount() + unusedPositionCount;
    if (getFooterViewsCount() > 0 && position >= startPosition) {
        return (position - startPosition) / mNumColumns;
    } else {
        return AdapterView.INVALID_POSITION;
    }
}
 
Example 9
public int viewIdHitPosition(MotionEvent ev, int id) {
    final int x = (int) ev.getX();
    final int y = (int) ev.getY();

    int touchPos = mDslv.pointToPosition(x, y); // includes headers/footers

    final int numHeaders = mDslv.getHeaderViewsCount();
    final int numFooters = mDslv.getFooterViewsCount();
    final int count = mDslv.getCount();

    // Log.d("mobeta", "touch down on position " + itemnum);
    // We're only interested if the touch was on an
    // item that's not a header or footer.
    if (touchPos != AdapterView.INVALID_POSITION && touchPos >= numHeaders
            && touchPos < (count - numFooters)) {
        final View item = mDslv.getChildAt(touchPos
                - mDslv.getFirstVisiblePosition());
        final int rawX = (int) ev.getRawX();
        final int rawY = (int) ev.getRawY();

        View dragBox = id == 0 ? item : (View) item.findViewById(id);
        if (dragBox != null) {
            dragBox.getLocationOnScreen(mTempLoc);

            if (rawX > mTempLoc[0] && rawY > mTempLoc[1]
                    && rawX < mTempLoc[0] + dragBox.getWidth()
                    && rawY < mTempLoc[1] + dragBox.getHeight()) {

                mItemX = item.getLeft();
                mItemY = item.getTop();

                return touchPos;
            }
        }
    }

    return MISS;
}
 
Example 10
Source Project: PTVGlass   File: LayoutAdapter.java    License: MIT License 5 votes vote down vote up
@Override
public int getPosition(Object item) {
    for (int i = 0; i < ThemeCards.values().length; i++) {
        if (getItem(i).equals(item)) {
            return i;
        }
    }
    return AdapterView.INVALID_POSITION;
}
 
Example 11
/**
 * 获取子项的第一项position(包含HeaderItem与FooterItem,不包含HeaderView及其占用的空白View)
 * 原Adapter无Item并且也无HeaderItem与FooterItem时,返回 AdapterView.INVALID_POSITION
 *
 * @return 子项的第一项position
 */
public int getFirstItemPosition() {
    // adapter有值或者有HeaderItem
    if ((mAdapter != null && mAdapter.getCount() > 0)
            || getHeaderItemsCount() > 0 || getFooterItemsCount() > 0)
        return getHeaderViewsCount();
    else
        return AdapterView.INVALID_POSITION;
}
 
Example 12
public int viewIdHitPosition(MotionEvent ev, int id) {
    final int x = (int) ev.getX();
    final int y = (int) ev.getY();

    int touchPos = mDslv.pointToPosition(x, y); // includes headers/footers

    final int numHeaders = mDslv.getHeaderViewsCount();
    final int numFooters = mDslv.getFooterViewsCount();
    final int count = mDslv.getCount();

    // Log.d("mobeta", "touch down on position " + itemnum);
    // We're only interested if the touch was on an
    // item that's not a header or footer.
    if (touchPos != AdapterView.INVALID_POSITION && touchPos >= numHeaders
            && touchPos < (count - numFooters)) {
        final View item = mDslv.getChildAt(touchPos - mDslv.getFirstVisiblePosition());
        final int rawX = (int) ev.getRawX();
        final int rawY = (int) ev.getRawY();

        View dragBox = id == 0 ? item : (View) item.findViewById(id);
        if (dragBox != null) {
            dragBox.getLocationOnScreen(mTempLoc);

            if (rawX > mTempLoc[0] && rawY > mTempLoc[1] &&
                    rawX < mTempLoc[0] + dragBox.getWidth() &&
                    rawY < mTempLoc[1] + dragBox.getHeight()) {

                mItemX = item.getLeft();
                mItemY = item.getTop();

                return touchPos;
            }
        }
    }

    return MISS;
}
 
Example 13
Source Project: LoyalNativeSlider   File: MaterialRippleLayout.java    License: MIT License 5 votes vote down vote up
private void clickAdapterView(AdapterView parent) {
    final int position = parent.getPositionForView(MaterialRippleLayout.this);
    final long itemId = parent.getAdapter() != null
            ? parent.getAdapter().getItemId(position)
            : 0;
    if (position != AdapterView.INVALID_POSITION) {
        parent.performItemClick(MaterialRippleLayout.this, position, itemId);
    }
}
 
Example 14
public int viewIdHitPosition(MotionEvent ev, int id) {
    final int x = (int) ev.getX();
    final int y = (int) ev.getY();

    int touchPos = mDslv.pointToPosition(x, y); // includes headers/footers

    final int numHeaders = mDslv.getHeaderViewsCount();
    final int numFooters = mDslv.getFooterViewsCount();
    final int count = mDslv.getCount();

    // Log.d("mobeta", "touch down on position " + itemnum);
    // We're only interested if the touch was on an
    // item that's not a header or footer.
    if (touchPos != AdapterView.INVALID_POSITION && touchPos >= numHeaders
            && touchPos < (count - numFooters)) {
        final View item = mDslv.getChildAt(touchPos - mDslv.getFirstVisiblePosition());
        final int rawX = (int) ev.getRawX();
        final int rawY = (int) ev.getRawY();

        View dragBox = id == 0 ? item : (View) item.findViewById(id);
        if (dragBox != null) {
            dragBox.getLocationOnScreen(mTempLoc);

            if (rawX > mTempLoc[0] && rawY > mTempLoc[1] &&
                    rawX < mTempLoc[0] + dragBox.getWidth() &&
                    rawY < mTempLoc[1] + dragBox.getHeight()) {

                mItemX = item.getLeft();
                mItemY = item.getTop();

                return touchPos;
            }
        }
    }

    return MISS;
}
 
Example 15
Source Project: AndroidSwipeLayout   File: SwipeLayout.java    License: MIT License 5 votes vote down vote up
private boolean performAdapterViewItemLongClick() {
    if (getOpenStatus() != Status.Close) return false;
    ViewParent t = getParent();
    if (t instanceof AdapterView) {
        AdapterView view = (AdapterView) t;
        int p = view.getPositionForView(SwipeLayout.this);
        if (p == AdapterView.INVALID_POSITION) return false;
        long vId = view.getItemIdAtPosition(p);
        boolean handled = false;
        try {
            Method m = AbsListView.class.getDeclaredMethod("performLongPress", View.class, int.class, long.class);
            m.setAccessible(true);
            handled = (boolean) m.invoke(view, SwipeLayout.this, p, vId);

        } catch (Exception e) {
            e.printStackTrace();

            if (view.getOnItemLongClickListener() != null) {
                handled = view.getOnItemLongClickListener().onItemLongClick(view, SwipeLayout.this, p, vId);
            }
            if (handled) {
                view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
            }
        }
        return handled;
    }
    return false;
}
 
Example 16
private int viewIdHitPosition(MotionEvent ev, int id)
{
    final int x = (int) ev.getX();
    final int y = (int) ev.getY();

    int touchPos = mDslv.pointToPosition(x, y); // includes headers/footers

    final int numHeaders = mDslv.getHeaderViewsCount();
    final int numFooters = mDslv.getFooterViewsCount();
    final int count = mDslv.getCount();

    // Log.d("mobeta", "touch down on position " + itemnum);
    // We're only interested if the touch was on an
    // item that's not a header or footer.
    if (touchPos != AdapterView.INVALID_POSITION && touchPos >= numHeaders
            && touchPos < (count - numFooters))
    {
        final View item = mDslv.getChildAt(touchPos - mDslv.getFirstVisiblePosition());
        final int rawX = (int) ev.getRawX();
        final int rawY = (int) ev.getRawY();

        View dragBox = id == 0 ? item : item.findViewById(id);
        if (dragBox != null)
        {
            dragBox.getLocationOnScreen(mTempLoc);

            if (rawX > mTempLoc[0] && rawY > mTempLoc[1] &&
                    rawX < mTempLoc[0] + dragBox.getWidth() &&
                    rawY < mTempLoc[1] + dragBox.getHeight())
            {

                mItemX = item.getLeft();
                mItemY = item.getTop();

                return touchPos;
            }
        }
    }

    return MISS;
}
 
Example 17
private boolean launchDragAndDrop(final MotionEvent event) {

		final int x = (int) event.getX();
		final int y = (int) event.getY();

		mCurrentPosition = mDragPosition = mDropPosition = pointToPosition(mDragPosition, x, y);

		if (mDragPosition != AdapterView.INVALID_POSITION && mDragAndDropListener.isDragAndDropEnabled(mDragPosition)) {

			mDragOffsetX = (int) (event.getRawX() - x);
			mDragOffsetY = (int) (event.getRawY() - y);

			startDrag(x, y);

			return true;
		}

		return false;
	}
 
Example 18
private void onDrop() {

		destroyDragImageView();

		removeCallbacks(mDelayedOnDragRunnable);

		View v = getChildAt(mDropPosition);
		v.setVisibility(View.VISIBLE);

		v.clearAnimation();

		if (mDragAndDropListener != null && mDropPosition != AdapterView.INVALID_POSITION) {

			mDragAndDropListener.onDropItem(mDragPosition, mDropPosition);
		}

		mDragPosition = mDropPosition = mCurrentPosition = AdapterView.INVALID_POSITION;
		mDragAndDropStarted = false;
	}
 
Example 19
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    switch(ev.getAction()){
        case MotionEvent.ACTION_DOWN:
            mDownX = (int) ev.getX();
            mDownY = (int) ev.getY();

            //根据按下的X,Y坐标获取所点击item的position
            mDragPosition = pointToPosition(mDownX, mDownY);
           // Log.d("mDagPosition is", "" + mDragPosition);

            if(mDragPosition == AdapterView.INVALID_POSITION){
                return super.dispatchTouchEvent(ev);
            }

            //使用Handler延迟dragResponseMS执行mLongClickRunnable,
            // 大于20dp才执行mLongClickRunnable避免与Drawlayout发生冲突
            int panding = (int) CommonUtil.convertDpToPixel(mcontext,20);
            if(mDownX > panding) {
                mHandler.postDelayed(mLongClickRunnable, dragResponseMS);
            }
            //根据position获取该item所对应的View
            mStartDragItemView = getChildAt(mDragPosition - getFirstVisiblePosition());

            //
            mPoint2ItemTop = mDownY - mStartDragItemView.getTop();
            mPoint2ItemLeft = mDownX - mStartDragItemView.getLeft();

            mOffset2Top = (int) (ev.getRawY() - mDownY);
            mOffset2Left = (int) (ev.getRawX() - mDownX);

            //获取DragGridView自动向上滚动的偏移量,小于这个值,DragGridView向下滚动
            mDownScrollBorder = getHeight() / 5;
            //获取DragGridView自动向下滚动的偏移量,大于这个值,DragGridView向上滚动
            mUpScrollBorder = getHeight() * 4/5;



            //开启mDragItemView绘图缓存
            mStartDragItemView.setDrawingCacheEnabled(true);

            //获取mDragItemView在缓存中的Bitmap对象
            mDragBitmap = Bitmap.createBitmap(mStartDragItemView.getDrawingCache());
            //这一步很关键,释放绘图缓存,避免出现重复的镜像
            mStartDragItemView.destroyDrawingCache();


            break;
        case MotionEvent.ACTION_MOVE:
            int moveX = (int)ev.getX();
            int moveY = (int) ev.getY();

            //如果我们在按下的item上面移动,只要不超过item的边界我们就不移除mRunnable
            if(!isTouchInItem(mStartDragItemView, moveX, moveY)){
                mHandler.removeCallbacks(mLongClickRunnable);
            }
            break;
        case MotionEvent.ACTION_UP:
            mHandler.removeCallbacks(mLongClickRunnable);
            mHandler.removeCallbacks(mScrollRunnable);
            break;
    }
    return super.dispatchTouchEvent(ev);
}
 
Example 20
Source Project: GravityBox   File: TouchInterceptor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if ((mDragListener != null || mDropListener != null) && mDragView != null) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                Rect r = mTempRect;
                mDragView.getDrawingRect(r);
                stopDragging();
                if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) {
                    mDropListener.drop(mFirstDragPos, mDragPos);
                }
                unExpandViews(false);
                break;

            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                int x = (int) ev.getX();
                int y = (int) ev.getY();
                dragView(x, y);
                int itemnum = getItemForPosition(y);
                if (itemnum >= 0) {
                    if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) {
                        if (mDragListener != null) {
                            mDragListener.drag(mDragPos, itemnum);
                        }
                        mDragPos = itemnum;
                        doExpansion();
                    }
                    int speed = 0;
                    adjustScrollBounds(y);
                    if (y > mLowerBound) {
                        // scroll the list up a bit
                        speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4;
                    } else if (y < mUpperBound) {
                        // scroll the list down a bit
                        speed = y < mUpperBound / 2 ? -16 : -4;
                    }
                    if (speed != 0) {
                        int ref = pointToPosition(0, mHeight / 2);
                        if (ref == AdapterView.INVALID_POSITION) {
                            // we hit a divider or an invisible view, check
                            // somewhere else
                            ref = pointToPosition(0, mHeight / 2 + getDividerHeight() + 64);
                        }
                        View v = getChildAt(ref - getFirstVisiblePosition());
                        if (v != null) {
                            int pos = v.getTop();
                            setSelectionFromTop(ref, pos - speed);
                        }
                    }
                }
                break;
        }
        return true;
    }
    return super.onTouchEvent(ev);
}