Skip to content

Commit

Permalink
Changed how to use and extend the DragItemAdapter
Browse files Browse the repository at this point in the history
You should no longer override removeItem, addItem, changeItemPosition
and getPositionForItemId(). This is now done by the super class.
Instead you need to set the item list by calling setItemList() with
a List<T> type.

If you want you can still override the methods if needed.
  • Loading branch information
woxblom committed May 10, 2015
1 parent 361cff0 commit 49ce656
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 79 deletions.
40 changes: 8 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Youtube demo video<br>
}

dependencies {
compile 'com.github.woxthebox:draglistview:1.0.9'
compile 'com.github.woxthebox:draglistview:1.1.0'
}

## Usage
Expand Down Expand Up @@ -96,39 +96,15 @@ List and Grid layouts are used as example in the sample project.
mBoardView.addColumnList(listAdapter, header, false);


For your adapter, extend DragItemAdapter and implement the methods below.
For your adapter, extend DragItemAdapter and call setItemList() with a List<T> type. setItemList() can be called anytime later to change the list.
You also need to provide a boolean to the super constructor to decide if you want the drag to happen on long press or directly when touching the item.

private ArrayList<Pair<Long, String>> mItemList;

@Override
public Object removeItem(int pos) {
Object item = mItemList.remove(pos);
notifyItemRemoved(pos);
return item;
}

@Override
public void addItem(int pos, Object item) {
mItemList.add(pos, (Pair<Long, String>) item);
notifyItemInserted(pos);
}

@Override
public int getPositionForItemId(long id) {
for (int i = 0; i < mItemList.size(); i++) {
if (id == mItemList.get(i).first) {
return i;
}
}
return -1;
}

@Override
public void changeItemPosition(int fromPos, int toPos) {
Pair<Long, String> pair = mItemList.remove(fromPos);
mItemList.add(toPos, pair);
notifyDataSetChanged();
public ItemAdapter(ArrayList<Pair<Long, String>> list, int layoutId, int grabHandleId, boolean dragOnLongPress) {
super(dragOnLongPress);
mLayoutId = layoutId;
mGrabHandleId = grabHandleId;
setHasStableIds(true);
setItemList(list);
}

Your ViewHolder should extend DragItemAdapter.ViewHolder and you must supply an id of the view that should respond to a drag.
Expand Down
2 changes: 1 addition & 1 deletion library/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.0.9
VERSION_NAME=1.1.0
VERSION_CODE=10
GROUP=com.github.woxthebox

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import android.view.MotionEvent;
import android.view.View;

public abstract class DragItemAdapter<VH extends DragItemAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {
import java.util.List;

public abstract class DragItemAdapter<T, VH extends DragItemAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {

interface DragStartedListener {
void onDragStarted(View itemView, long itemId);
Expand All @@ -30,17 +32,58 @@ interface DragStartedListener {
private long mDragItemId = -1;
private boolean mDragOnLongPress;
private boolean mDragEnabled = true;
protected List<T> mItemList;

public abstract Object removeItem(int pos);
public DragItemAdapter(boolean dragOnLongPress) {
mDragOnLongPress = dragOnLongPress;
}

public abstract void addItem(int pos, Object item);
public void setItemList(List<T> itemList) {
mItemList = itemList;
notifyDataSetChanged();
}

public abstract int getPositionForItemId(long id);
public List<T> getItemList() {
return mItemList;
}

public abstract void changeItemPosition(int fromPos, int toPos);
public Object removeItem(int pos) {
if (mItemList != null && mItemList.size() > pos) {
Object item = mItemList.remove(pos);
notifyItemRemoved(pos);
return item;
}
return null;
}

public DragItemAdapter(boolean dragOnLongPress) {
mDragOnLongPress = dragOnLongPress;
public void addItem(int pos, T item) {
if (mItemList != null && mItemList.size() > pos) {
mItemList.add(pos, item);
notifyItemInserted(pos);
}
}

public void changeItemPosition(int fromPos, int toPos) {
if (mItemList != null && mItemList.size() > fromPos && mItemList.size() > toPos) {
T item = mItemList.remove(fromPos);
mItemList.add(toPos, item);
notifyDataSetChanged();
}
}

public int getPositionForItemId(long id) {
int count = getItemCount();
for (int i = 0; i < count; i++) {
if (id == getItemId(i)) {
return i;
}
}
return -1;
}

@Override
public int getItemCount() {
return mItemList == null ? 0 : mItemList.size();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@

import java.util.ArrayList;

public class ItemAdapter extends DragItemAdapter<ItemAdapter.ViewHolder> {
public class ItemAdapter extends DragItemAdapter<Pair<Long, String>, ItemAdapter.ViewHolder> {

private ArrayList<Pair<Long, String>> mItemList;
private int mLayoutId;
private int mGrabHandleId;

public ItemAdapter(ArrayList<Pair<Long, String>> list, int layoutId, int grabHandleId, boolean dragOnLongPress) {
super(dragOnLongPress);
mItemList = list;
mLayoutId = layoutId;
mGrabHandleId = grabHandleId;
setHasStableIds(true);
setItemList(list);
}

@Override
Expand All @@ -47,36 +46,6 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(view);
}

@Override
public Object removeItem(int pos) {
Object item = mItemList.remove(pos);
notifyItemRemoved(pos);
return item;
}

@Override
public void addItem(int pos, Object item) {
mItemList.add(pos, (Pair<Long, String>) item);
notifyItemInserted(pos);
}

@Override
public void changeItemPosition(int fromPos, int toPos) {
Pair<Long, String> pair = mItemList.remove(fromPos);
mItemList.add(toPos, pair);
notifyDataSetChanged();
}

@Override
public int getPositionForItemId(long id) {
for (int i = 0; i < mItemList.size(); i++) {
if (id == mItemList.get(i).first) {
return i;
}
}
return -1;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
Expand All @@ -85,17 +54,12 @@ public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setTag(text);
}

@Override
public int getItemCount() {
return mItemList != null ? mItemList.size() : 0;
}

@Override
public long getItemId(int position) {
return mItemList.get(position).first;
}

public class ViewHolder extends DragItemAdapter.ViewHolder {
public class ViewHolder extends DragItemAdapter<Pair<Long, String>, ItemAdapter.ViewHolder>.ViewHolder {
public TextView mText;

public ViewHolder(final View itemView) {
Expand Down

0 comments on commit 49ce656

Please sign in to comment.