From 7d6c1b8c0a8a20dc60f341499772c0b3a6535338 Mon Sep 17 00:00:00 2001 From: Magnus Woxblom Date: Mon, 7 May 2018 11:32:16 +0200 Subject: [PATCH] Added support to be able to insert a new column at a specific index --- README.md | 6 +-- library/gradle.properties | 4 +- .../com/woxthebox/draglistview/BoardView.java | 45 +++++++++++++++---- .../draglistview/sample/BoardFragment.java | 17 ++++--- .../draglistview/sample/ItemAdapter.java | 3 +- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index f3c949b..0db545a 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ YouTube demo video
} 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 @@ -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 type. setItemList() can be called anytime later to change the list. diff --git a/library/gradle.properties b/library/gradle.properties index e95f46d..2ac6f7b 100644 --- a/library/gradle.properties +++ b/library/gradle.properties @@ -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. diff --git a/library/src/main/java/com/woxthebox/draglistview/BoardView.java b/library/src/main/java/com/woxthebox/draglistview/BoardView.java index de7d706..3ca2477 100644 --- a/library/src/main/java/com/woxthebox/draglistview/BoardView.java +++ b/library/src/main/java/com/woxthebox/draglistview/BoardView.java @@ -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; @@ -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 @@ -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); @@ -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; } diff --git a/sample/src/main/java/com/woxthebox/draglistview/sample/BoardFragment.java b/sample/src/main/java/com/woxthebox/draglistview/sample/BoardFragment.java index 1e18d05..797806a 100644 --- a/sample/src/main/java/com/woxthebox/draglistview/sample/BoardFragment.java +++ b/sample/src/main/java/com/woxthebox/draglistview/sample/BoardFragment.java @@ -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; @@ -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 @@ -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); @@ -183,7 +182,7 @@ public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } - private void addColumnList() { + private void addColumn() { final ArrayList> mItemArray = new ArrayList<>(); int addItems = 15; for (int i = 0; i < addItems; i++) { @@ -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++; } diff --git a/sample/src/main/java/com/woxthebox/draglistview/sample/ItemAdapter.java b/sample/src/main/java/com/woxthebox/draglistview/sample/ItemAdapter.java index fa7c756..7737433 100644 --- a/sample/src/main/java/com/woxthebox/draglistview/sample/ItemAdapter.java +++ b/sample/src/main/java/com/woxthebox/draglistview/sample/ItemAdapter.java @@ -41,8 +41,9 @@ class ItemAdapter extends DragItemAdapter, 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); }