Skip to content

Commit

Permalink
Added support to be able to insert a new column at a specific index
Browse files Browse the repository at this point in the history
  • Loading branch information
woxblom committed May 7, 2018
1 parent 00f158b commit 7d6c1b8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ YouTube demo video<br>
}

dependencies {
compile 'com.github.woxthebox:draglistview:1.6.0'
compile 'com.github.woxthebox:draglistview:1.6.1'
}

Add this to proguard rules, otherwise animations won't work correctly
Expand Down Expand Up @@ -216,14 +216,14 @@ List and Grid layouts are used as example in the sample project.
}
});
...
mBoardView.addColumnList(listAdapter, header, false);
mBoardView.addColumn(listAdapter, header, null, false);

To enable dragging and reordering of columns you need to provide a column drag view when adding the column. It is the view that will
start the column drag process when long pressed on. You can also implement a custom column drag item to control the visuals and animations.
Check out the sample app to see how it is done. If no custom drag item is used a screenshot of the column will be used instead.

mBoardView.setCustomColumnDragItem(new MyColumnDragItem(getActivity(), R.layout.column_drag_layout));
mBoardView.addColumnList(listAdapter, header, columnDragView, false);
mBoardView.addColumn(listAdapter, header, columnDragView, false);

For your adapter, extend DragItemAdapter and call setItemList() with a List<T> type. setItemList() can be called anytime later to change the list.

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.6.0
VERSION_CODE=47
VERSION_NAME=1.6.1
VERSION_CODE=48
GROUP=com.github.woxthebox

POM_DESCRIPTION=Drag and drop to re-order items in a list, grid or board.
Expand Down
45 changes: 36 additions & 9 deletions library/src/main/java/com/woxthebox/draglistview/BoardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.res.Resources;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
Expand Down Expand Up @@ -773,16 +774,39 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
}

/**
* Adds a column to the board.
* Inserts a column to the board at a specific index.
*
* @param adapter Adapter with the items for the column.
* @param header Header view that will be positioned above the column.
* @param columnDragView View that will act as handle to drag and drop columns.
* @param index Index where on the board to add the column.
* @param header Header view that will be positioned above the column. Can be null.
* @param columnDragView View that will act as handle to drag and drop columns. Can be null.
* @param hasFixedItemSize If the items will have a fixed or dynamic size.
*
* @return The created DragItemRecyclerView.
*/
public DragItemRecyclerView addColumnList(final DragItemAdapter adapter, final View header, View columnDragView, boolean hasFixedItemSize) {
final DragItemRecyclerView recyclerView = addColumnList(adapter, header, hasFixedItemSize);
public DragItemRecyclerView insertColumn(final DragItemAdapter adapter, int index, final @Nullable View header, @Nullable View columnDragView, boolean hasFixedItemSize) {
final DragItemRecyclerView recyclerView = insertColumn(adapter, index, header, hasFixedItemSize);
setupColumnDragListener(columnDragView, recyclerView);
return recyclerView;
}

/**
* Adds a column at the last index of the board.
*
* @param adapter Adapter with the items for the column.
* @param header Header view that will be positioned above the column. Can be null.
* @param columnDragView View that will act as handle to drag and drop columns. Can be null.
* @param hasFixedItemSize If the items will have a fixed or dynamic size.
*
* @return The created DragItemRecyclerView.
*/
public DragItemRecyclerView addColumn(final DragItemAdapter adapter, final @Nullable View header, @Nullable View columnDragView, boolean hasFixedItemSize) {
final DragItemRecyclerView recyclerView = insertColumn(adapter, getColumnCount(), header, hasFixedItemSize);
setupColumnDragListener(columnDragView, recyclerView);
return recyclerView;
}

private void setupColumnDragListener(View columnDragView, final DragItemRecyclerView recyclerView ) {
if (columnDragView != null) {
columnDragView.setOnLongClickListener(new OnLongClickListener() {
@Override
Expand All @@ -792,10 +816,13 @@ public boolean onLongClick(View v) {
}
});
}
return recyclerView;
}

public DragItemRecyclerView addColumnList(final DragItemAdapter adapter, final View header, boolean hasFixedItemSize) {
private DragItemRecyclerView insertColumn(final DragItemAdapter adapter, int index, final @Nullable View header, boolean hasFixedItemSize) {
if (index > getColumnCount()) {
throw new IllegalArgumentException("Index is out of bounds");
}

final DragItemRecyclerView recyclerView = (DragItemRecyclerView) LayoutInflater.from(getContext()).inflate(R.layout.drag_item_recycler_view, this, false);
recyclerView.setId(getColumnCount());
recyclerView.setHorizontalScrollBarEnabled(false);
Expand Down Expand Up @@ -876,8 +903,8 @@ public boolean isDragging() {
}
layout.addView(recyclerView);

mLists.add(recyclerView);
mColumnLayout.addView(layout);
mLists.add(index, recyclerView);
mColumnLayout.addView(layout, index);
return recyclerView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.woxthebox.draglistview.BoardView;
import com.woxthebox.draglistview.DragItem;
Expand Down Expand Up @@ -139,11 +138,11 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Board");

addColumnList();
addColumnList();
addColumnList();
addColumnList();
addColumnList();
addColumn();
addColumn();
addColumn();
addColumn();
addColumn();
}

@Override
Expand Down Expand Up @@ -171,7 +170,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
getActivity().invalidateOptionsMenu();
return true;
case R.id.action_add_column:
addColumnList();
addColumn();
return true;
case R.id.action_remove_column:
mBoardView.removeColumn(0);
Expand All @@ -183,7 +182,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

private void addColumnList() {
private void addColumn() {
final ArrayList<Pair<Long, String>> mItemArray = new ArrayList<>();
int addItems = 15;
for (int i = 0; i < addItems; i++) {
Expand All @@ -209,7 +208,7 @@ public void onClick(View v) {
((TextView) header.findViewById(R.id.item_count)).setText(String.valueOf(mItemArray.size()));
}
});
mBoardView.addColumnList(listAdapter, header, header, false);
mBoardView.addColumn(listAdapter, header, header, false);
mColumns++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class ItemAdapter extends DragItemAdapter<Pair<Long, String>, ItemAdapter.ViewHo
setItemList(list);
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
return new ViewHolder(view);
}
Expand Down

0 comments on commit 7d6c1b8

Please sign in to comment.