Java Code Examples for android.support.v7.widget.RecyclerView#getChildPosition()

The following examples show how to use android.support.v7.widget.RecyclerView#getChildPosition() . 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 File: DividerItemDecoration.java    From citra_android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
        RecyclerView.State state)
{
  super.getItemOffsets(outRect, view, parent, state);
  if (mDivider == null)
  {
    return;
  }
  if (parent.getChildPosition(view) < 1)
  {
    return;
  }

  if (getOrientation(parent) == LinearLayoutManager.VERTICAL)
  {
    outRect.top = mDivider.getIntrinsicHeight();
  }
  else
  {
    outRect.left = mDivider.getIntrinsicWidth();
  }
}
 
Example 2
Source File: DividerItemDecoration.java    From AndroidSwipeLayout with MIT License 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    if (mDivider == null) {
        return;
    }
    if (parent.getChildPosition(view) < 1) {
        return;
    }

    if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
        outRect.top = mDivider.getIntrinsicHeight();
    } else {
        outRect.left = mDivider.getIntrinsicWidth();
    }
}
 
Example 3
Source File: DividerItemDecoration.java    From QuickReturn with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                           RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    if (mDivider == null) {
        return;
    }
    if (parent.getChildPosition(view) < 1) {
        return;
    }

    if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
        outRect.top = mDivider.getIntrinsicHeight();
    } else {
        outRect.left = mDivider.getIntrinsicWidth();
    }
}
 
Example 4
Source File: DividerItemDecoration.java    From android with Apache License 2.0 6 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
    RecyclerView.State state) {
  super.getItemOffsets(outRect, view, parent, state);
  if (mDivider == null) {
    return;
  }
  if (parent.getChildPosition(view) < 1) {
    return;
  }

  if (getOrientation(parent) == LinearLayoutManager.VERTICAL) {
    outRect.top = mDivider.getIntrinsicHeight();
  } else {
    outRect.left = mDivider.getIntrinsicWidth();
  }
}
 
Example 5
Source File: MySwipeRefreshLayout.java    From Cotable with Apache License 2.0 6 votes vote down vote up
/**
 * @return Whether it is possible for the child view of this layout to
 * scroll up. Override this if the child view is a custom view.
 */
public boolean canChildScrollUp() {
    if (android.os.Build.VERSION.SDK_INT < 14) {
        if (mTarget instanceof AbsListView) {
            final AbsListView absListView = (AbsListView) mTarget;
            return absListView.getChildCount() > 0
                    && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                    .getTop() < absListView.getPaddingTop());
        } else {
            //Log.d(TAG, "canChildScrollUp :"+ mTarget.getScrollY()+" "+mTarget);
            boolean flag = mTarget.getScrollY() > 0;
            if (mTarget instanceof RecyclerView) {
                RecyclerView rv = (RecyclerView) mTarget;
                int firstVisiblePosition = rv.getChildPosition(rv.getChildAt(0));
                if (firstVisiblePosition == 0)
                    return rv.getChildAt(0).getTop() == 0 ? false : true;
                else
                    return true;
            }
            return flag;//ViewCompat.canScrollVertically(mTarget, -1);
        }
    } else {
        return ViewCompat.canScrollVertically(mTarget, -1);
    }
}
 
Example 6
Source File: DividerGridItemDecoration.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
@Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
         int totalSpanCount = getSpanCount(parent);
         int itemPosition = parent.getChildPosition(view);

         boolean isLeft = isLeft(view, totalSpanCount);
         boolean isRight = isRight(view, totalSpanCount);
         boolean isFull = isFull(view, totalSpanCount);
         boolean isCenter = !isLeft && !isRight && !isFull;
         boolean isTop = isTop(parent, itemPosition, totalSpanCount);
         boolean isBottom = isBottom(parent, itemPosition, totalSpanCount);

//        LogUtil.d(itemPosition, "total:"+totalSpanCount, "index:"+getSpanIndex(view), "Size:"+getSpanSize(view), isLeft, isTop, isRight, isBottom, isFull, isCenter);

         int left = (isRight || isCenter) ? mHalfSpacingWidth : 0;
         int right = (isLeft || isCenter) ? mHalfSpacingWidth : 0;
         int top = !isTop ? mHalfSpacingHeight : 0;
         int bottom = !isBottom ? mHalfSpacingHeight : 0;

        outRect.set(left, top, right, bottom);
    }
 
Example 7
Source File: MainActivity.java    From MultiLevelExpandableIndentableListView with MIT License 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    // code copied from https://developer.android.com/training/material/lists-cards.html
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.content_recyclerview);

    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new MyAdapter(getActivity(), new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = mRecyclerView.getChildPosition(v);
            mAdapter.toggleGroup(position);
        }
    });
    mRecyclerView.setAdapter(mAdapter);

    MyContent content = getDummyContent();
    mAdapter.add(content);
    List<MyComment> comments = getDummyComments();
    mAdapter.addAll(comments);

    if (savedInstanceState != null) {
        List<Integer> groups = savedInstanceState.getIntegerArrayList(GROUPS_KEY);
        mAdapter.restoreGroups(groups);
    }

    return rootView;
}
 
Example 8
Source File: StickyHeadersItemDecoration.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {


    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    View header = headerViewHolder.itemView;
    Float lastY = null;

    for (int i = childCount - 1; i >= 0; i--) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams)child.getLayoutParams();
        final int position = parent.getChildPosition(child);
        RecyclerView.ViewHolder holder = parent.getChildViewHolder(child);

        if (!lp.isItemRemoved()) {

            float translationY = ViewCompat.getTranslationY(child);

            if (i == 0 || isHeader(holder)) {

                float y = getHeaderY(child, lm) + translationY;

                if (lastY != null && lastY < y + headerHeight) {
                    y = lastY - headerHeight;
                }

                adapter.onBindViewHolder(headerViewHolder, position);

                c.save();
                c.translate(0, y);
                header.draw(c);
                c.restore();

                lastY = y;
            }
        }
    }
}
 
Example 9
Source File: StickyHeadersItemDecoration.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {


    final int childCount = parent.getChildCount();
    final RecyclerView.LayoutManager lm = parent.getLayoutManager();
    View header = headerViewHolder.itemView;
    Float lastY = null;

    for (int i = childCount - 1; i >= 0; i--) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams)child.getLayoutParams();
        final int position = parent.getChildPosition(child);
        RecyclerView.ViewHolder holder = parent.getChildViewHolder(child);

        if (!lp.isItemRemoved()) {

            float translationY = ViewCompat.getTranslationY(child);

            if (i == 0 || isHeader(holder)) {

                float y = getHeaderY(child, lm) + translationY;

                if (lastY != null && lastY < y + headerHeight) {
                    y = lastY - headerHeight;
                }

                adapter.onBindViewHolder(headerViewHolder, position);

                c.save();
                c.translate(0, y);
                header.draw(c);
                c.restore();

                lastY = y;
            }
        }
    }
}
 
Example 10
Source File: JazzyRecyclerViewScrollListener.java    From JazzyListView with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    int firstVisibleItem = recyclerView.getChildPosition(recyclerView.getChildAt(0));
    int visibleItemCount = recyclerView.getChildCount();
    int totalItemCount = recyclerView.getAdapter().getItemCount();

    mHelper.onScrolled(recyclerView, firstVisibleItem, visibleItemCount, totalItemCount);

    notifyAdditionalOnScrolledListener(recyclerView, dx, dy);
}
 
Example 11
Source File: DragSortRecycler.java    From DragSortRecycler with Apache License 2.0 4 votes vote down vote up
/**
 * Find the new position by scanning through the items on
 * screen and finding the positional relationship.
 * This *seems* to work, another method would be to use
 * getItemOffsets, but I think that could miss items?..
 */
private int getNewPostion(RecyclerView rv)
{
    int itemsOnScreen = rv.getLayoutManager().getChildCount();

    float floatMiddleY = floatingItemBounds.top + floatingItemBounds.height()/2;

    int above=0;
    int below = Integer.MAX_VALUE;
    for (int n=0;n < itemsOnScreen;n++) //Scan though items on screen, however they may not
    {                                   // be in order!

        View view = rv.getLayoutManager().getChildAt(n);

        if (view.getVisibility() != View.VISIBLE)
            continue;

        int itemPos = rv.getChildPosition(view);

        if (itemPos == selectedDragItemPos) //Don't check against itself!
            continue;

        float viewMiddleY = view.getTop() + view.getHeight()/2;
        if (floatMiddleY > viewMiddleY) //Is above this item
        {
            if (itemPos > above)
                above = itemPos;
        }
        else if (floatMiddleY <= viewMiddleY) //Is below this item
        {
            if (itemPos < below)
                below = itemPos;
        }
    }
    debugLog("above = " + above + " below = " + below);

    if (below != Integer.MAX_VALUE) {
        if (below < selectedDragItemPos) //Need to count itself
            below++;
        return below - 1;
    }
    else
    {
        if (above < selectedDragItemPos)
            above++;

        return above;
    }
}
 
Example 12
Source File: DragSortRecycler.java    From rox-android with Apache License 2.0 4 votes vote down vote up
/**
 * Find the new position by scanning through the items on
 * screen and finding the positional relationship.
 * This *seems* to work, another method would be to use
 * getItemOffsets, but I think that could miss items?..
 */
private int getNewPostion(RecyclerView rv)
{
    int itemsOnScreen = rv.getLayoutManager().getChildCount();

    float floatMiddleY = floatingItemBounds.top + floatingItemBounds.height()/2;

    int above=0;
    int below = Integer.MAX_VALUE;
    for (int n=0;n < itemsOnScreen;n++) //Scan though items on screen, however they may not
    {                                   // be in order!

        View view = rv.getLayoutManager().getChildAt(n);

        if (view.getVisibility() != View.VISIBLE)
            continue;

        int itemPos = rv.getChildPosition(view);

        if (itemPos == selectedDragItemPos) //Don't check against itself!
            continue;

        float viewMiddleY = view.getTop() + view.getHeight()/2;
        if (floatMiddleY > viewMiddleY) //Is above this item
        {
            if (itemPos > above)
                above = itemPos;
        }
        else if (floatMiddleY <= viewMiddleY) //Is below this item
        {
            if (itemPos < below)
                below = itemPos;
        }
    }
    debugLog("above = " + above + " below = " + below);

    if (below != Integer.MAX_VALUE) {
        if (below < selectedDragItemPos) //Need to count itself
            below++;
        return below - 1;
    }
    else
    {
        if (above < selectedDragItemPos)
            above++;

        return above;
    }
}
 
Example 13
Source File: DragSortRecycler.java    From rox-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onInterceptTouchEvent");

    //if (e.getAction() == MotionEvent.ACTION_DOWN)
    {
        View itemView = rv.findChildViewUnder(e.getX(), e.getY());

        if (itemView==null)
            return false;

        boolean dragging = false;

        if ((dragHandleWidth > 0 ) && (e.getX() < dragHandleWidth))
        {
            dragging = true;
        }
        else if (viewHandleId != -1)
        {
            //Find the handle in the list item
            View handleView = itemView.findViewById(viewHandleId);

            if (handleView == null)
            {
                Log.e(TAG, "The view ID " + viewHandleId + " was not found in the RecycleView item");
                return false;
            }

            //View should be visible to drag
            if(handleView.getVisibility()!=View.VISIBLE) {
                return false;
            }

            //We need to find the relative position of the handle to the parent view
            //Then we can work out if the touch is within the handle
            int[] parentItemPos = new int[2];
            itemView.getLocationInWindow(parentItemPos);

            int[] handlePos = new int[2];
            handleView.getLocationInWindow(handlePos);

            int xRel = handlePos[0] - parentItemPos[0];
            int yRel = handlePos[1] - parentItemPos[1];

            Rect touchBounds = new Rect(itemView.getLeft() + xRel, itemView.getTop() + yRel,
                    itemView.getLeft() + xRel + handleView.getWidth(),
                    itemView.getTop() + yRel  + handleView.getHeight()
            );

            if (touchBounds.contains((int)e.getX(), (int)e.getY()))
                dragging = true;

            debugLog("parentItemPos = " + parentItemPos[0] + " " + parentItemPos[1]);
            debugLog("handlePos = " + handlePos[0] + " " + handlePos[1]);
        }


        if (dragging)
        {
            debugLog("Started Drag");

            setIsDragging(true);

            floatingItem = createFloatingBitmap(itemView);

            fingerAnchorY = (int)e.getY();
            fingerOffsetInViewY = fingerAnchorY - itemView.getTop();
            fingerY = fingerAnchorY;

            selectedDragItemPos = rv.getChildPosition(itemView);
            debugLog("selectedDragItemPos = " + selectedDragItemPos);

            return true;
        }
    }
    return false;
}
 
Example 14
Source File: DragSortRecycler.java    From rox-android with Apache License 2.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView rv, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, rv, state);

    debugLog("getItemOffsets");

    debugLog("View top = " + view.getTop());
    if (selectedDragItemPos != -1)
    {
        int itemPos =  rv.getChildPosition(view);
        debugLog("itemPos =" + itemPos);

        if(!canDragOver(itemPos)) {
            return;
        }

        //Movement of finger
        float totalMovement = fingerY-fingerAnchorY;

        if (itemPos == selectedDragItemPos)
        {
            view.setVisibility(View.INVISIBLE);
        }
        else
        {
            //Make view visible incase invisible
            view.setVisibility(View.VISIBLE);

            //Find middle of the floatingItem
            float floatMiddleY = floatingItemBounds.top + floatingItemBounds.height()/2;

            //Moving down the list
            //These will auto-animate if the device continually sends touch motion events
            // if (totalMovment>0)
            {
                if ((itemPos > selectedDragItemPos) && (view.getTop() < floatMiddleY))
                {
                    float amountUp = (floatMiddleY - view.getTop()) / (float)view.getHeight();
                    //  amountUp *= 0.5f;
                    if (amountUp > 1)
                        amountUp = 1;

                    outRect.top = -(int)(floatingItemBounds.height()*amountUp);
                    outRect.bottom = (int)(floatingItemBounds.height()*amountUp);
                }

            }//Moving up the list
            // else if (totalMovment < 0)
            {
                if((itemPos < selectedDragItemPos) && (view.getBottom() > floatMiddleY))
                {
                    float amountDown = ((float)view.getBottom() - floatMiddleY) / (float)view.getHeight();
                    //  amountDown *= 0.5f;
                    if (amountDown > 1)
                        amountDown = 1;

                    outRect.top = (int)(floatingItemBounds.height()*amountDown);
                    outRect.bottom = -(int)(floatingItemBounds.height()*amountDown);
                }
            }
        }
    }
    else
    {
        outRect.top = 0;
        outRect.bottom = 0;
        //Make view visible incase invisible
        view.setVisibility(View.VISIBLE);
    }
}
 
Example 15
Source File: IndexLayoutManager.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
private void updatePosBasedOnReferenceList(RecyclerView referenceRv) {
    View firstVisibleView = referenceRv.getChildAt(0);
    int actual = referenceRv.getChildPosition(firstVisibleView);
    ((LinearLayoutManager) indexList.getLayoutManager()).scrollToPositionWithOffset(actual, firstVisibleView.getTop() + 0);
}
 
Example 16
Source File: GridSpacingDecoration.java    From Popeens-DSub with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
	super.getItemOffsets(outRect, view, parent, state);

	int spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, SPACING, view.getResources().getDisplayMetrics());
	int halfSpacing = spacing / 2;

	int childCount = parent.getChildCount();
	int childIndex = parent.getChildPosition(view);
	// Not an actual child (ie: during delete event)
	if(childIndex == -1) {
		return;
	}
	int spanCount = getTotalSpan(view, parent);
	int spanIndex = childIndex % spanCount;

	// If we can, use the SpanSizeLookup since headers screw up the index calculation
	RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
	if(layoutManager instanceof GridLayoutManager) {
		GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
		GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();
		if(spanSizeLookup != null) {
			spanIndex = spanSizeLookup.getSpanIndex(childIndex, spanCount);
		}
	}
	int spanSize = getSpanSize(parent, childIndex);

       /* INVALID SPAN */
	if (spanCount < 1 || spanSize > 1) return;

	int margins = 0;
	if(view instanceof UpdateView) {
		View firstChild = ((ViewGroup) view).getChildAt(0);
		ViewGroup.LayoutParams layoutParams = firstChild.getLayoutParams();
		if (layoutParams instanceof LinearLayout.LayoutParams) {
			margins = ((LinearLayout.LayoutParams) layoutParams).bottomMargin;
		} else if (layoutParams instanceof FrameLayout.LayoutParams) {
			margins = ((FrameLayout.LayoutParams) layoutParams).bottomMargin;
		}
	}
	int doubleMargins = margins * 2;

	outRect.top = halfSpacing - margins;
	outRect.bottom = halfSpacing - margins;
	outRect.left = halfSpacing - margins;
	outRect.right = halfSpacing - margins;

	if (isTopEdge(childIndex, spanIndex, spanCount)) {
		outRect.top = spacing - doubleMargins;
	}

	if (isLeftEdge(spanIndex, spanCount)) {
		outRect.left = spacing - doubleMargins;
	}

	if (isRightEdge(spanIndex, spanCount)) {
		outRect.right = spacing - doubleMargins;
	}

	if (isBottomEdge(childIndex, childCount, spanCount)) {
		outRect.bottom = spacing - doubleMargins;
	}
}
 
Example 17
Source File: DragSortRecycler.java    From Slide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView rv, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, rv, state);

    debugLog("getItemOffsets");

    debugLog("View top = " + view.getTop());
    if (selectedDragItemPos != -1) {
        int itemPos = rv.getChildPosition(view);
        debugLog("itemPos =" + itemPos);

        if (!canDragOver(itemPos)) {
            return;
        }

        if (itemPos == selectedDragItemPos) {
            view.setVisibility(View.INVISIBLE);
        } else {
            //Make view visible incase invisible
            view.setVisibility(View.VISIBLE);

            //Find middle of the floatingItem
            float floatMiddleY = floatingItemBounds.top + floatingItemBounds.height() / 2;

            //Moving down the list
            //These will auto-animate if the device continually sends touch motion events
            // if (totalMovment>0)
            {
                if ((itemPos > selectedDragItemPos) && (view.getTop() < floatMiddleY)) {
                    float amountUp = (floatMiddleY - view.getTop()) / (float) view.getHeight();
                    //  amountUp *= 0.5f;
                    if (amountUp > 1)
                        amountUp = 1;

                    outRect.top = -(int) (floatingItemBounds.height() * amountUp);
                    outRect.bottom = (int) (floatingItemBounds.height() * amountUp);
                }

            }//Moving up the list
            // else if (totalMovment < 0)
            {
                if ((itemPos < selectedDragItemPos) && (view.getBottom() > floatMiddleY)) {
                    float amountDown = ((float) view.getBottom() - floatMiddleY) / (float) view.getHeight();
                    //  amountDown *= 0.5f;
                    if (amountDown > 1)
                        amountDown = 1;

                    outRect.top = (int) (floatingItemBounds.height() * amountDown);
                    outRect.bottom = -(int) (floatingItemBounds.height() * amountDown);
                }
            }
        }
    } else {
        outRect.top = 0;
        outRect.bottom = 0;
        //Make view visible incase invisible
        view.setVisibility(View.VISIBLE);
    }
}
 
Example 18
Source File: DragSortRecycler.java    From Slide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Find the new position by scanning through the items on
 * screen and finding the positional relationship.
 * This *seems* to work, another method would be to use
 * getItemOffsets, but I think that could miss items?..
 */
private int getNewPostion(RecyclerView rv) {
    int itemsOnScreen = rv.getLayoutManager().getChildCount();

    float floatMiddleY = floatingItemBounds.top + floatingItemBounds.height() / 2;

    int above = 0;
    int below = Integer.MAX_VALUE;
    for (int n = 0; n < itemsOnScreen; n++) //Scan though items on screen, however they may not
    {                                   // be in order!

        View view = rv.getLayoutManager().getChildAt(n);

        if (view.getVisibility() != View.VISIBLE)
            continue;

        int itemPos = rv.getChildPosition(view);

        if (itemPos == selectedDragItemPos) //Don't check against itself!
            continue;

        float viewMiddleY = view.getTop() + view.getHeight() / 2;
        if (floatMiddleY > viewMiddleY) //Is above this item
        {
            if (itemPos > above)
                above = itemPos;
        } else if (floatMiddleY <= viewMiddleY && itemPos < below) //Is below this item
        {
            below = itemPos;
        }
    }
    debugLog("above = " + above + " below = " + below);

    if (below != Integer.MAX_VALUE) {
        if (below < selectedDragItemPos) //Need to count itself
            below++;
        return below - 1;
    } else {
        if (above < selectedDragItemPos)
            above++;

        return above;
    }
}
 
Example 19
Source File: DragSortRecycler.java    From Slide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    debugLog("onInterceptTouchEvent");

    //if (e.getAction() == MotionEvent.ACTION_DOWN)
    {
        View itemView = rv.findChildViewUnder(e.getX(), e.getY());

        if (itemView == null)
            return false;

        boolean dragging = false;

        if ((dragHandleWidth > 0) && (e.getX() < dragHandleWidth)) {
            dragging = true;
        } else if (viewHandleId != -1) {
            //Find the handle in the list item
            View handleView = itemView.findViewById(viewHandleId);

            if (handleView == null) {
                String TAG = "DragSortRecycler";
                Log.e(TAG, "The view ID " + viewHandleId + " was not found in the RecycleView item");
                return false;
            }

            //View should be visible to drag
            if (handleView.getVisibility() != View.VISIBLE) {
                return false;
            }

            //We need to find the relative position of the handle to the parent view
            //Then we can work out if the touch is within the handle
            int[] parentItemPos = new int[2];
            itemView.getLocationInWindow(parentItemPos);

            int[] handlePos = new int[2];
            handleView.getLocationInWindow(handlePos);

            int xRel = handlePos[0] - parentItemPos[0];
            int yRel = handlePos[1] - parentItemPos[1];

            Rect touchBounds = new Rect(itemView.getLeft() + xRel, itemView.getTop() + yRel,
                    itemView.getLeft() + xRel + handleView.getWidth(),
                    itemView.getTop() + yRel + handleView.getHeight()
            );

            if (touchBounds.contains((int) e.getX(), (int) e.getY()))
                dragging = true;

            debugLog("parentItemPos = " + parentItemPos[0] + " " + parentItemPos[1]);
            debugLog("handlePos = " + handlePos[0] + " " + handlePos[1]);
        }


        if (dragging) {
            debugLog("Started Drag");

            setIsDragging(true);

            floatingItem = createFloatingBitmap(itemView);

            fingerAnchorY = (int) e.getY();
            fingerOffsetInViewY = fingerAnchorY - itemView.getTop();
            fingerY = fingerAnchorY;

            selectedDragItemPos = rv.getChildPosition(itemView);
            debugLog("selectedDragItemPos = " + selectedDragItemPos);

            return true;
        }
    }
    return false;
}
 
Example 20
Source File: IndexLayoutManager.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
private void updatePosBasedOnReferenceList(RecyclerView referenceRv) {
    View firstVisibleView = referenceRv.getChildAt(0);
    int actual = referenceRv.getChildPosition(firstVisibleView);
    ((LinearLayoutManager) indexList.getLayoutManager()).scrollToPositionWithOffset(actual, firstVisibleView.getTop() + 0);
}