Skip to content

Commit

Permalink
Fixed so any layout manager can be set on the recyclerview
Browse files Browse the repository at this point in the history
  • Loading branch information
woxblom committed Jun 4, 2015
1 parent 721c035 commit 57884d2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ Youtube demo video<br>
}

dependencies {
compile 'com.github.woxthebox:draglistview:1.1.2'
compile 'com.github.woxthebox:draglistview:1.1.3'
}

## Usage
**NOTE: The adapter must use stable ids and only layout managers based on a LinearLayoutManager are supported.
**NOTE: The adapter must use stable ids.
List and Grid layouts are used as example in the sample project.

For list and grid view use the DragListView.
Expand Down
4 changes: 2 additions & 2 deletions library/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=1.1.2
VERSION_CODE=12
VERSION_NAME=1.1.3
VERSION_CODE=13
GROUP=com.github.woxthebox

POM_DESCRIPTION=Drag and drop to re-order items in a list, grid or board.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
Expand Down Expand Up @@ -50,6 +49,7 @@ private enum DragState {
private int mDragItemPosition;
private int mTouchSlop;
private float mStartY;
private boolean mClipToPadding;

public DragItemRecyclerView(Context context) {
super(context);
Expand Down Expand Up @@ -104,6 +104,12 @@ public long getDragItemId() {
return mDragItemId;
}

@Override
public void setClipToPadding(boolean clipToPadding) {
super.setClipToPadding(clipToPadding);
mClipToPadding = clipToPadding;
}

@Override
public void setAdapter(Adapter adapter) {
if (!(adapter instanceof DragItemAdapter)) {
Expand All @@ -117,14 +123,6 @@ public void setAdapter(Adapter adapter) {
mAdapter = (DragItemAdapter) adapter;
}

@Override
public void setLayoutManager(LayoutManager layout) {
super.setLayoutManager(layout);
if (!(layout instanceof LinearLayoutManager)) {
throw new RuntimeException("Layout must be an instance of LinearLayoutManager");
}
}

@Override
public void onAutoScrollPositionBy(int dx, int dy) {
if (isDragging()) {
Expand Down Expand Up @@ -166,11 +164,22 @@ private void updateDragPositionAndScroll() {
mDragItemPosition = newPos;
}

LinearLayoutManager layout = (LinearLayoutManager) getLayoutManager();
if (mDragItem.getY() > getHeight() - view.getHeight() / 2 && layout.findLastCompletelyVisibleItemPosition() !=
mAdapter.getItemCount() - 1) {
boolean lastItemReached = false;
boolean firstItemReached = false;
int top = mClipToPadding ? getPaddingTop() : 0;
int bottom = mClipToPadding ? getHeight() - getPaddingBottom() : getHeight();
ViewHolder lastChild = findViewHolderForAdapterPosition(mAdapter.getItemCount() - 1);
ViewHolder firstChild = findViewHolderForAdapterPosition(0);
if (lastChild != null && lastChild.itemView.getBottom() <= bottom) {
lastItemReached = true;
}
if (firstChild != null && firstChild.itemView.getTop() >= top) {
firstItemReached = true;
}

if (mDragItem.getY() > getHeight() - view.getHeight() / 2 && !lastItemReached) {
mAutoScroller.startAutoScroll(AutoScroller.ScrollDirection.UP);
} else if (mDragItem.getY() < view.getHeight() / 2 && layout.findFirstCompletelyVisibleItemPosition() != 0) {
} else if (mDragItem.getY() < view.getHeight() / 2 && !firstItemReached) {
mAutoScroller.startAutoScroll(AutoScroller.ScrollDirection.DOWN);
} else {
mAutoScroller.stopAutoScroll();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright 2014 Magnus Woxblom
*
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down

0 comments on commit 57884d2

Please sign in to comment.