use of android.support.v7.widget.RecyclerView in project UltimateRecyclerView by cymcsg.
the class DragManager method onDrag.
@Override
public boolean onDrag(View v, DragEvent event) {
if (v != recyclerViewRef.get() || !(event.getLocalState() instanceof DragInfo)) {
return false;
}
final RecyclerView recyclerView = (RecyclerView) v;
final DragInfo dragInfo = (DragInfo) event.getLocalState();
final long itemId = dragInfo.itemId();
switch(event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
draggingId = itemId;
adapter.notifyItemChanged(recyclerView.findViewHolderForItemId(itemId).getAdapterPosition());
break;
case DragEvent.ACTION_DRAG_LOCATION:
float x = event.getX();
float y = event.getY();
int fromPosition = adapter.getPositionForId(itemId);
int toPosition = -1;
View child = recyclerView.findChildViewUnder(event.getX(), event.getY());
if (child != null) {
toPosition = recyclerView.getChildViewHolder(child).getAdapterPosition();
}
if (toPosition >= 0 && fromPosition != toPosition) {
RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
boolean scheduleNextMove = nextMoveTouchPoint.equals(MIN_VALUE, MIN_VALUE);
nextMoveTouchPoint.set(x, y);
if (scheduleNextMove)
animator.isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
@Override
public void onAnimationsFinished() {
if (nextMoveTouchPoint.equals(MIN_VALUE, MIN_VALUE)) {
return;
}
final int fromPosition = adapter.getPositionForId(itemId);
View child = recyclerView.findChildViewUnder(nextMoveTouchPoint.x, nextMoveTouchPoint.y);
if (child != null) {
final int toPosition = recyclerView.getChildViewHolder(child).getAdapterPosition();
if (adapter.move(fromPosition, toPosition)) {
if (fromPosition == 0 || toPosition == 0) {
// fix for weird scrolling when animating first item
recyclerView.scrollToPosition(0);
}
recyclerView.post(new Runnable() {
@Override
public void run() {
adapter.notifyItemMoved(fromPosition, toPosition);
}
});
}
}
// reset so we know to schedule listener again next time
clearNextMove();
}
});
}
lastDragInfo = dragInfo;
lastDragInfo.setDragPoint(x, y);
adapter.handleDragScroll(recyclerView, dragInfo);
break;
case DragEvent.ACTION_DRAG_ENDED:
draggingId = RecyclerView.NO_ID;
lastDragInfo = null;
// queue up the show animation until after all move animations are finished
recyclerView.getItemAnimator().isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
@Override
public void onAnimationsFinished() {
int position = adapter.getPositionForId(itemId);
RecyclerView.ViewHolder vh = recyclerView.findViewHolderForItemId(itemId);
if (vh != null && vh.getAdapterPosition() != position) {
// if positions don't match, there's still an outstanding move animation
// so we try to reschedule the notifyItemChanged until after that
recyclerView.post(new Runnable() {
@Override
public void run() {
recyclerView.getItemAnimator().isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
@Override
public void onAnimationsFinished() {
adapter.notifyItemChanged(adapter.getPositionForId(itemId));
}
});
}
});
} else {
adapter.notifyItemChanged(adapter.getPositionForId(itemId));
}
}
});
break;
case DragEvent.ACTION_DROP:
adapter.onDrop();
break;
case DragEvent.ACTION_DRAG_ENTERED:
// probably not used?
break;
case DragEvent.ACTION_DRAG_EXITED:
// TODO edge scrolling
break;
}
return true;
}
use of android.support.v7.widget.RecyclerView in project UltimateRecyclerView by cymcsg.
the class HeaderPositionCalculator method itemIsObscuredByHeader.
/**
* Determines if an item is obscured by a header
*
* @param parent
* @param item to determine if obscured by header
* @param header that might be obscuring the item
* @param orientation of the {@link RecyclerView}
* @return true if the item view is obscured by the header view
*/
private boolean itemIsObscuredByHeader(RecyclerView parent, View item, View header, int orientation) {
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) item.getLayoutParams();
Rect headerMargins = mDimensionCalculator.getMargins(header);
int adapterPosition = parent.getChildAdapterPosition(item);
if (adapterPosition == RecyclerView.NO_POSITION || mHeaderProvider.getHeader(parent, adapterPosition) != header) {
// Handles an edge case where a trailing header is smaller than the current sticky header.
return false;
}
if (orientation == LinearLayoutManager.VERTICAL) {
int itemTop = item.getTop() - layoutParams.topMargin;
int headerBottom = header.getBottom() + headerMargins.bottom + headerMargins.top;
if (itemTop > headerBottom) {
return false;
}
} else {
int itemLeft = item.getLeft() - layoutParams.leftMargin;
int headerRight = header.getRight() + headerMargins.right + headerMargins.left;
if (itemLeft > headerRight) {
return false;
}
}
return true;
}
use of android.support.v7.widget.RecyclerView in project UltimateRecyclerView by cymcsg.
the class HeaderPositionCalculator method getHeaderBounds.
public Rect getHeaderBounds(RecyclerView recyclerView, View header, View firstView, boolean firstHeader) {
int orientation = mOrientationProvider.getOrientation(recyclerView);
Rect bounds = getDefaultHeaderOffset(recyclerView, header, firstView, orientation);
if (firstHeader && isStickyHeaderBeingPushedOffscreen(recyclerView, header)) {
View viewAfterNextHeader = getFirstViewUnobscuredByHeader(recyclerView, header);
int firstViewUnderHeaderPosition = recyclerView.getChildAdapterPosition(viewAfterNextHeader);
View secondHeader = mHeaderProvider.getHeader(recyclerView, firstViewUnderHeaderPosition);
translateHeaderWithNextHeader(recyclerView, mOrientationProvider.getOrientation(recyclerView), bounds, header, viewAfterNextHeader, secondHeader);
}
return bounds;
}
use of android.support.v7.widget.RecyclerView in project UltimateRecyclerView by cymcsg.
the class StickyRecyclerHeadersDecoration method getItemOffsets.
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
int itemPosition = parent.getChildAdapterPosition(view);
if (itemPosition == RecyclerView.NO_POSITION) {
return;
}
if (mHeaderPositionCalculator.hasNewHeader(itemPosition)) {
View header = getHeaderView(parent, itemPosition);
setItemOffsetsForHeader(outRect, header, mOrientationProvider.getOrientation(parent));
}
}
use of android.support.v7.widget.RecyclerView in project UltimateRecyclerView by cymcsg.
the class SwipeListViewTouchListener method makeScrollListener.
/**
* Return ScrollListener for ListView
*
* @return OnScrollListener
*/
public RecyclerView.OnScrollListener makeScrollListener() {
return new RecyclerView.OnScrollListener() {
private boolean isFirstItem = false;
private boolean isLastItem = false;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
setEnabled(newState != recyclerView.SCROLL_STATE_DRAGGING);
if (swipeClosesAllItemsWhenListMoves && newState == recyclerView.SCROLL_STATE_DRAGGING) {
closeOpenedItems();
}
if (newState == recyclerView.SCROLL_STATE_DRAGGING) {
listViewMoving = true;
setEnabled(false);
}
if (newState != recyclerView.SCROLL_STATE_SETTLING && newState != recyclerView.SCROLL_STATE_DRAGGING) {
listViewMoving = false;
downPosition = ListView.INVALID_POSITION;
swipeListView.resetScrolling();
new Handler().postDelayed(new Runnable() {
public void run() {
setEnabled(true);
}
}, 500);
}
}
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
// if (isFirstItem) {
// boolean onSecondItemList = firstVisibleItem == 1;
// if (onSecondItemList) {
// isFirstItem = false;
// }
// } else {
// boolean onFirstItemList = firstVisibleItem == 0;
// if (onFirstItemList)
// isFirstItem = true;
// swipeListView.onFirstListItem();
// }
// }
// if (isLastItem) {
// boolean onBeforeLastItemList = firstVisibleItem + visibleItemCount == totalItemCount - 1;
// if (onBeforeLastItemList) {
// isLastItem = false;
// }
// } else {
// boolean onLastItemList = firstVisibleItem + visibleItemCount >= totalItemCount;
// if (onLastItemList) {
// isLastItem = true;
// swipeListView.onLastListItem();
// }
// }
}
};
}
Aggregations