Skip to content

Commit 1d995cf

Browse files
committed
Adds GridLayoutManager example
1 parent 7c0f57a commit 1d995cf

File tree

13 files changed

+277
-38
lines changed

13 files changed

+277
-38
lines changed

app/src/main/java/co/paulburke/android/itemtouchhelperdemo/MainActivity.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,47 @@
1717
package co.paulburke.android.itemtouchhelperdemo;
1818

1919
import android.os.Bundle;
20+
import android.support.v4.app.Fragment;
2021
import android.support.v7.app.ActionBarActivity;
2122
import android.support.v7.widget.Toolbar;
2223

2324
/**
2425
* @author Paul Burke (ipaulpro)
2526
*/
26-
public class MainActivity extends ActionBarActivity {
27+
public class MainActivity extends ActionBarActivity implements MainFragment.OnListItemClickListener {
2728

2829
@Override
2930
protected void onCreate(Bundle savedInstanceState) {
3031
super.onCreate(savedInstanceState);
3132

3233
setContentView(R.layout.activity_main);
3334
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
35+
36+
if (savedInstanceState == null) {
37+
MainFragment fragment = new MainFragment();
38+
getSupportFragmentManager().beginTransaction()
39+
.add(R.id.content, fragment)
40+
.commit();
41+
}
42+
}
43+
44+
@Override
45+
public void onListItemClick(int position) {
46+
Fragment fragment = null;
47+
switch (position) {
48+
case 0:
49+
fragment = new RecyclerListFragment();
50+
break;
51+
52+
case 1:
53+
fragment = new RecyclerGridFragment();
54+
break;
55+
}
56+
57+
getSupportFragmentManager().beginTransaction()
58+
.replace(R.id.content, fragment)
59+
.addToBackStack(null)
60+
.commit();
3461
}
3562

3663
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2015 Paul Burke
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package co.paulburke.android.itemtouchhelperdemo;
18+
19+
import android.app.Activity;
20+
import android.os.Bundle;
21+
import android.support.v4.app.ListFragment;
22+
import android.view.View;
23+
import android.widget.ArrayAdapter;
24+
import android.widget.ListView;
25+
26+
/**
27+
* @author Paul Burke (ipaulpro)
28+
*/
29+
public class MainFragment extends ListFragment {
30+
31+
public interface OnListItemClickListener {
32+
void onListItemClick(int position);
33+
}
34+
35+
private OnListItemClickListener mItemClickListener;
36+
37+
public MainFragment() {
38+
}
39+
40+
@Override
41+
public void onAttach(Activity activity) {
42+
super.onAttach(activity);
43+
44+
mItemClickListener = (OnListItemClickListener) activity;
45+
}
46+
47+
@Override
48+
public void onActivityCreated(Bundle savedInstanceState) {
49+
super.onActivityCreated(savedInstanceState);
50+
51+
final String[] items = getResources().getStringArray(R.array.main_items);
52+
final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
53+
android.R.layout.simple_list_item_1, items);
54+
setListAdapter(adapter);
55+
}
56+
57+
@Override
58+
public void onListItemClick(ListView l, View v, int position, long id) {
59+
mItemClickListener.onListItemClick(position);
60+
}
61+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (C) 2015 Paul Burke
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package co.paulburke.android.itemtouchhelperdemo;
18+
19+
import android.os.Bundle;
20+
import android.support.annotation.Nullable;
21+
import android.support.v4.app.Fragment;
22+
import android.support.v7.widget.GridLayoutManager;
23+
import android.support.v7.widget.RecyclerView;
24+
import android.support.v7.widget.helper.ItemTouchHelper;
25+
import android.view.LayoutInflater;
26+
import android.view.View;
27+
import android.view.ViewGroup;
28+
29+
import co.paulburke.android.itemtouchhelperdemo.helper.OnStartDragListener;
30+
import co.paulburke.android.itemtouchhelperdemo.helper.SimpleItemTouchHelperCallback;
31+
32+
/**
33+
* @author Paul Burke (ipaulpro)
34+
*/
35+
public class RecyclerGridFragment extends Fragment implements OnStartDragListener {
36+
37+
private ItemTouchHelper mItemTouchHelper;
38+
39+
public RecyclerGridFragment() {
40+
}
41+
42+
@Nullable
43+
@Override
44+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
45+
return new RecyclerView(container.getContext());
46+
}
47+
48+
@Override
49+
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
50+
super.onViewCreated(view, savedInstanceState);
51+
52+
final RecyclerListAdapter adapter = new RecyclerListAdapter(getActivity(), this);
53+
54+
RecyclerView recyclerView = (RecyclerView) view;
55+
recyclerView.setHasFixedSize(true);
56+
recyclerView.setAdapter(adapter);
57+
58+
final int spanCount = getResources().getInteger(R.integer.grid_columns);
59+
final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), spanCount);
60+
recyclerView.setLayoutManager(layoutManager);
61+
62+
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
63+
mItemTouchHelper = new ItemTouchHelper(callback);
64+
mItemTouchHelper.attachToRecyclerView(recyclerView);
65+
}
66+
67+
@Override
68+
public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
69+
mItemTouchHelper.startDrag(viewHolder);
70+
}
71+
}

app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListAdapter.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package co.paulburke.android.itemtouchhelperdemo;
1818

19+
import android.content.Context;
1920
import android.graphics.Color;
2021
import android.support.v4.view.MotionEventCompat;
2122
import android.support.v7.widget.RecyclerView;
@@ -33,6 +34,7 @@
3334

3435
import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperAdapter;
3536
import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperViewHolder;
37+
import co.paulburke.android.itemtouchhelperdemo.helper.OnStartDragListener;
3638

3739
/**
3840
* Simple RecyclerView.Adapter that implements {@link ItemTouchHelperAdapter} to respond to move and
@@ -43,30 +45,13 @@
4345
public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder>
4446
implements ItemTouchHelperAdapter {
4547

46-
/**
47-
* Listener for manual initiation of a drag.
48-
*/
49-
public interface OnStartDragListener {
50-
51-
/**
52-
* Called when a view is requesting a start of a drag.
53-
*
54-
* @param viewHolder The holder of the view to drag.
55-
*/
56-
void onStartDrag(RecyclerView.ViewHolder viewHolder);
57-
}
58-
59-
private static final String[] STRINGS = new String[]{
60-
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
61-
};
62-
6348
private final List<String> mItems = new ArrayList<>();
6449

6550
private final OnStartDragListener mDragStartListener;
6651

67-
public RecyclerListAdapter(OnStartDragListener dragStartListener) {
52+
public RecyclerListAdapter(Context context, OnStartDragListener dragStartListener) {
6853
mDragStartListener = dragStartListener;
69-
mItems.addAll(Arrays.asList(STRINGS));
54+
mItems.addAll(Arrays.asList(context.getResources().getStringArray(R.array.dummy_items)));
7055
}
7156

7257
@Override
@@ -99,9 +84,10 @@ public void onItemDismiss(int position) {
9984
}
10085

10186
@Override
102-
public void onItemMove(int fromPosition, int toPosition) {
87+
public boolean onItemMove(int fromPosition, int toPosition) {
10388
Collections.swap(mItems, fromPosition, toPosition);
10489
notifyItemMoved(fromPosition, toPosition);
90+
return true;
10591
}
10692

10793
@Override

app/src/main/java/co/paulburke/android/itemtouchhelperdemo/RecyclerListFragment.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
import android.view.View;
2727
import android.view.ViewGroup;
2828

29+
import co.paulburke.android.itemtouchhelperdemo.helper.OnStartDragListener;
2930
import co.paulburke.android.itemtouchhelperdemo.helper.SimpleItemTouchHelperCallback;
3031

3132
/**
3233
* @author Paul Burke (ipaulpro)
3334
*/
34-
public class RecyclerListFragment extends Fragment implements RecyclerListAdapter.OnStartDragListener {
35+
public class RecyclerListFragment extends Fragment implements OnStartDragListener {
3536

3637
private ItemTouchHelper mItemTouchHelper;
3738

@@ -41,16 +42,16 @@ public RecyclerListFragment() {
4142
@Nullable
4243
@Override
4344
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
44-
return inflater.inflate(R.layout.fragment_main, container, false);
45+
return new RecyclerView(container.getContext());
4546
}
4647

4748
@Override
4849
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
4950
super.onViewCreated(view, savedInstanceState);
5051

51-
RecyclerListAdapter adapter = new RecyclerListAdapter(this);
52+
RecyclerListAdapter adapter = new RecyclerListAdapter(getActivity(), this);
5253

53-
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
54+
RecyclerView recyclerView = (RecyclerView) view;
5455
recyclerView.setHasFixedSize(true);
5556
recyclerView.setAdapter(adapter);
5657
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

app/src/main/java/co/paulburke/android/itemtouchhelperdemo/helper/ItemTouchHelperAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ public interface ItemTouchHelperAdapter {
3535
*
3636
* @param fromPosition The start position of the moved item.
3737
* @param toPosition Then resolved position of the moved item.
38+
* @return True if the item was moved to the new adapter position.
3839
*
3940
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
4041
* @see RecyclerView.ViewHolder#getAdapterPosition()
4142
*/
42-
void onItemMove(int fromPosition, int toPosition);
43+
boolean onItemMove(int fromPosition, int toPosition);
4344

4445

4546
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2015 Paul Burke
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package co.paulburke.android.itemtouchhelperdemo.helper;
18+
19+
import android.support.v7.widget.RecyclerView;
20+
21+
/**
22+
* Listener for manual initiation of a drag.
23+
*/
24+
public interface OnStartDragListener {
25+
26+
/**
27+
* Called when a view is requesting a start of a drag.
28+
*
29+
* @param viewHolder The holder of the view to drag.
30+
*/
31+
void onStartDrag(RecyclerView.ViewHolder viewHolder);
32+
33+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
3232
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
3333

34-
<fragment
35-
class="co.paulburke.android.itemtouchhelperdemo.RecyclerListFragment"
34+
<FrameLayout
35+
android:id="@+id/content"
3636
android:layout_width="match_parent"
3737
android:layout_height="match_parent" />
3838

app/src/main/res/layout/item_main.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
1+
<?xml version="1.0" encoding="utf-8"?><!--
32
~ Copyright (C) 2015 Paul Burke
43
~
54
~ Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,7 +16,10 @@
1716
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
1817
android:id="@+id/item"
1918
android:layout_width="match_parent"
20-
android:layout_height="?listPreferredItemHeight">
19+
android:layout_height="?listPreferredItemHeight"
20+
android:clickable="true"
21+
android:focusable="true"
22+
android:foreground="?selectableItemBackground">
2123

2224
<TextView
2325
android:id="@+id/text"
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
1+
<?xml version="1.0" encoding="utf-8"?><!--
32
~ Copyright (C) 2015 Paul Burke
43
~
54
~ Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,8 +13,8 @@
1413
~ See the License for the specific language governing permissions and
1514
~ limitations under the License.
1615
-->
17-
<android.support.v7.widget.RecyclerView
18-
xmlns:android="http://schemas.android.com/apk/res/android"
19-
android:id="@+id/recycler_view"
20-
android:layout_width="match_parent"
21-
android:layout_height="match_parent" />
16+
<resources>
17+
18+
<integer name="grid_columns">3</integer>
19+
20+
</resources>

0 commit comments

Comments
 (0)