From 1d995cf7bdd5b0c173a79e40b92ac3634a6da9b8 Mon Sep 17 00:00:00 2001 From: Paul Burke Date: Wed, 22 Jul 2015 11:54:57 -0400 Subject: [PATCH] Adds GridLayoutManager example --- .../itemtouchhelperdemo/MainActivity.java | 29 +++++++- .../itemtouchhelperdemo/MainFragment.java | 61 ++++++++++++++++ .../RecyclerGridFragment.java | 71 +++++++++++++++++++ .../RecyclerListAdapter.java | 26 ++----- .../RecyclerListFragment.java | 9 +-- .../helper/ItemTouchHelperAdapter.java | 3 +- .../helper/OnStartDragListener.java | 33 +++++++++ app/src/main/res/layout/activity_main.xml | 4 +- app/src/main/res/layout/item_main.xml | 8 ++- .../integers.xml} | 13 ++-- app/src/main/res/values-w820dp/integers.xml | 20 ++++++ app/src/main/res/values/integers.xml | 20 ++++++ app/src/main/res/values/strings.xml | 18 +++++ 13 files changed, 277 insertions(+), 38 deletions(-) create mode 100644 app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainFragment.java create mode 100644 app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerGridFragment.java create mode 100644 app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/OnStartDragListener.java rename app/src/main/res/{layout/fragment_main.xml => values-w600dp/integers.xml} (69%) create mode 100644 app/src/main/res/values-w820dp/integers.xml create mode 100644 app/src/main/res/values/integers.xml diff --git a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainActivity.java b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainActivity.java index 349660c..570cf32 100644 --- a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainActivity.java +++ b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainActivity.java @@ -17,13 +17,14 @@ package co.paulburke.android.itemtouchhelperdemo; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; /** * @author Paul Burke (ipaulpro) */ -public class MainActivity extends ActionBarActivity { +public class MainActivity extends ActionBarActivity implements MainFragment.OnListItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { @@ -31,6 +32,32 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); + + if (savedInstanceState == null) { + MainFragment fragment = new MainFragment(); + getSupportFragmentManager().beginTransaction() + .add(R.id.content, fragment) + .commit(); + } + } + + @Override + public void onListItemClick(int position) { + Fragment fragment = null; + switch (position) { + case 0: + fragment = new RecyclerListFragment(); + break; + + case 1: + fragment = new RecyclerGridFragment(); + break; + } + + getSupportFragmentManager().beginTransaction() + .replace(R.id.content, fragment) + .addToBackStack(null) + .commit(); } } diff --git a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainFragment.java b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainFragment.java new file mode 100644 index 0000000..10d3104 --- /dev/null +++ b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainFragment.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2015 Paul Burke + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package co.paulburke.android.itemtouchhelperdemo; + +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.app.ListFragment; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +/** + * @author Paul Burke (ipaulpro) + */ +public class MainFragment extends ListFragment { + + public interface OnListItemClickListener { + void onListItemClick(int position); + } + + private OnListItemClickListener mItemClickListener; + + public MainFragment() { + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + + mItemClickListener = (OnListItemClickListener) activity; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final String[] items = getResources().getStringArray(R.array.main_items); + final ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), + android.R.layout.simple_list_item_1, items); + setListAdapter(adapter); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + mItemClickListener.onListItemClick(position); + } +} diff --git a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerGridFragment.java b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerGridFragment.java new file mode 100644 index 0000000..fa1e5e6 --- /dev/null +++ b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerGridFragment.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Paul Burke + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package co.paulburke.android.itemtouchhelperdemo; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.helper.ItemTouchHelper; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import co.paulburke.android.itemtouchhelperdemo.helper.OnStartDragListener; +import co.paulburke.android.itemtouchhelperdemo.helper.SimpleItemTouchHelperCallback; + +/** + * @author Paul Burke (ipaulpro) + */ +public class RecyclerGridFragment extends Fragment implements OnStartDragListener { + + private ItemTouchHelper mItemTouchHelper; + + public RecyclerGridFragment() { + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return new RecyclerView(container.getContext()); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + final RecyclerListAdapter adapter = new RecyclerListAdapter(getActivity(), this); + + RecyclerView recyclerView = (RecyclerView) view; + recyclerView.setHasFixedSize(true); + recyclerView.setAdapter(adapter); + + final int spanCount = getResources().getInteger(R.integer.grid_columns); + final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), spanCount); + recyclerView.setLayoutManager(layoutManager); + + ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter); + mItemTouchHelper = new ItemTouchHelper(callback); + mItemTouchHelper.attachToRecyclerView(recyclerView); + } + + @Override + public void onStartDrag(RecyclerView.ViewHolder viewHolder) { + mItemTouchHelper.startDrag(viewHolder); + } +} diff --git a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListAdapter.java b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListAdapter.java index 0d06017..a63b3d1 100644 --- a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListAdapter.java +++ b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListAdapter.java @@ -16,6 +16,7 @@ package co.paulburke.android.itemtouchhelperdemo; +import android.content.Context; import android.graphics.Color; import android.support.v4.view.MotionEventCompat; import android.support.v7.widget.RecyclerView; @@ -33,6 +34,7 @@ import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperAdapter; import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperViewHolder; +import co.paulburke.android.itemtouchhelperdemo.helper.OnStartDragListener; /** * Simple RecyclerView.Adapter that implements {@link ItemTouchHelperAdapter} to respond to move and @@ -43,30 +45,13 @@ public class RecyclerListAdapter extends RecyclerView.Adapter implements ItemTouchHelperAdapter { - /** - * Listener for manual initiation of a drag. - */ - public interface OnStartDragListener { - - /** - * Called when a view is requesting a start of a drag. - * - * @param viewHolder The holder of the view to drag. - */ - void onStartDrag(RecyclerView.ViewHolder viewHolder); - } - - private static final String[] STRINGS = new String[]{ - "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" - }; - private final List mItems = new ArrayList<>(); private final OnStartDragListener mDragStartListener; - public RecyclerListAdapter(OnStartDragListener dragStartListener) { + public RecyclerListAdapter(Context context, OnStartDragListener dragStartListener) { mDragStartListener = dragStartListener; - mItems.addAll(Arrays.asList(STRINGS)); + mItems.addAll(Arrays.asList(context.getResources().getStringArray(R.array.dummy_items))); } @Override @@ -99,9 +84,10 @@ public void onItemDismiss(int position) { } @Override - public void onItemMove(int fromPosition, int toPosition) { + public boolean onItemMove(int fromPosition, int toPosition) { Collections.swap(mItems, fromPosition, toPosition); notifyItemMoved(fromPosition, toPosition); + return true; } @Override diff --git a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListFragment.java b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListFragment.java index bbe8344..75b05dc 100644 --- a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListFragment.java +++ b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListFragment.java @@ -26,12 +26,13 @@ import android.view.View; import android.view.ViewGroup; +import co.paulburke.android.itemtouchhelperdemo.helper.OnStartDragListener; import co.paulburke.android.itemtouchhelperdemo.helper.SimpleItemTouchHelperCallback; /** * @author Paul Burke (ipaulpro) */ -public class RecyclerListFragment extends Fragment implements RecyclerListAdapter.OnStartDragListener { +public class RecyclerListFragment extends Fragment implements OnStartDragListener { private ItemTouchHelper mItemTouchHelper; @@ -41,16 +42,16 @@ public RecyclerListFragment() { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - return inflater.inflate(R.layout.fragment_main, container, false); + return new RecyclerView(container.getContext()); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - RecyclerListAdapter adapter = new RecyclerListAdapter(this); + RecyclerListAdapter adapter = new RecyclerListAdapter(getActivity(), this); - RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); + RecyclerView recyclerView = (RecyclerView) view; recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); diff --git a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/ItemTouchHelperAdapter.java b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/ItemTouchHelperAdapter.java index 643734a..4a873c5 100644 --- a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/ItemTouchHelperAdapter.java +++ b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/ItemTouchHelperAdapter.java @@ -35,11 +35,12 @@ public interface ItemTouchHelperAdapter { * * @param fromPosition The start position of the moved item. * @param toPosition Then resolved position of the moved item. + * @return True if the item was moved to the new adapter position. * * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder) * @see RecyclerView.ViewHolder#getAdapterPosition() */ - void onItemMove(int fromPosition, int toPosition); + boolean onItemMove(int fromPosition, int toPosition); /** diff --git a/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/OnStartDragListener.java b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/OnStartDragListener.java new file mode 100644 index 0000000..73853b3 --- /dev/null +++ b/app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/OnStartDragListener.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2015 Paul Burke + * + * 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 + * + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package co.paulburke.android.itemtouchhelperdemo.helper; + +import android.support.v7.widget.RecyclerView; + +/** + * Listener for manual initiation of a drag. + */ +public interface OnStartDragListener { + + /** + * Called when a view is requesting a start of a drag. + * + * @param viewHolder The holder of the view to drag. + */ + void onStartDrag(RecyclerView.ViewHolder viewHolder); + +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7cd176f..303df0f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -31,8 +31,8 @@ android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> - diff --git a/app/src/main/res/layout/item_main.xml b/app/src/main/res/layout/item_main.xml index 2fd1964..21d3998 100644 --- a/app/src/main/res/layout/item_main.xml +++ b/app/src/main/res/layout/item_main.xml @@ -1,5 +1,4 @@ - - - + + + 3 + + \ No newline at end of file diff --git a/app/src/main/res/values-w820dp/integers.xml b/app/src/main/res/values-w820dp/integers.xml new file mode 100644 index 0000000..21e1ff3 --- /dev/null +++ b/app/src/main/res/values-w820dp/integers.xml @@ -0,0 +1,20 @@ + + + + 4 + + \ No newline at end of file diff --git a/app/src/main/res/values/integers.xml b/app/src/main/res/values/integers.xml new file mode 100644 index 0000000..8273bf6 --- /dev/null +++ b/app/src/main/res/values/integers.xml @@ -0,0 +1,20 @@ + + + + 2 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 579ce89..750c354 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,4 +15,22 @@ --> ItemTouchHelper Demo + + + List - Basic Drag and Swipe + Grid - Basic Swipe + + + + One + Two + Three + Four + Five + Six + Seven + Eight + Nine + Ten +