Skip to content

Commit

Permalink
Adds GridLayoutManager example
Browse files Browse the repository at this point in the history
  • Loading branch information
iPaulPro committed Jul 22, 2015
1 parent 7c0f57a commit 1d995cf
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,47 @@
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) {
super.onCreate(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();
}

}
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -43,30 +45,13 @@
public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder>
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<String> 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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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);

}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<fragment
class="co.paulburke.android.itemtouchhelperdemo.RecyclerListFragment"
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/res/layout/item_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2015 Paul Burke
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,7 +16,10 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeight">
android:layout_height="?listPreferredItemHeight"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground">

<TextView
android:id="@+id/text"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2015 Paul Burke
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,8 +13,8 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<resources>

<integer name="grid_columns">3</integer>

</resources>
Loading

0 comments on commit 1d995cf

Please sign in to comment.