Skip to content

Commit d8d85c3

Browse files
committed
Basic drag-and-drop and swipe-to-dismiss with ItemTouchHelper
0 parents  commit d8d85c3

34 files changed

+762
-0
lines changed

.gitignore

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ndk generated files
2+
**/obj/
3+
4+
# built application files
5+
*.apk
6+
*.ap_
7+
8+
# lint files
9+
lint.xml
10+
11+
# files for the dex VM
12+
*.dex
13+
14+
# Java class files
15+
*.class
16+
17+
# generated files
18+
bin/
19+
gen/
20+
classes/
21+
gen-external-apklibs/
22+
23+
# maven output folder
24+
target
25+
26+
# Local configuration file (sdk path, etc)
27+
local.properties
28+
29+
# Eclipse project files
30+
.classpath
31+
.project
32+
.metadata
33+
.settings
34+
35+
# IntelliJ files
36+
.idea
37+
*.iml
38+
39+
# OSX files
40+
.DS_Store
41+
42+
# Windows files
43+
Thumbs.db
44+
45+
# vi swap files
46+
*.swp
47+
48+
# backup files
49+
*.bak
50+
51+
com_crashlytics_export_strings.xml
52+
53+
# Gradle files
54+
.gradle
55+
/build
56+
/captures

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 22
5+
buildToolsVersion "22.0.1"
6+
7+
defaultConfig {
8+
applicationId "co.paulburke.android.itemtouchhelperdemo"
9+
minSdkVersion 16
10+
targetSdkVersion 22
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
}
15+
16+
dependencies {
17+
compile fileTree(dir: 'libs', include: ['*.jar'])
18+
compile 'com.android.support:appcompat-v7:22.2.0'
19+
compile 'com.android.support:recyclerview-v7:22.2.0'
20+
}

app/proguard-rules.pro

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/paulburke/Sites/android-sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package co.paulburke.android.itemtouchhelperdemo;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

app/src/main/AndroidManifest.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="co.paulburke.android.itemtouchhelperdemo" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package co.paulburke.android.itemtouchhelperdemo;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.ActionBarActivity;
5+
import android.support.v7.widget.Toolbar;
6+
7+
/**
8+
* @author Paul Burke (ipaulpro)
9+
*/
10+
public class MainActivity extends ActionBarActivity {
11+
12+
@Override
13+
protected void onCreate(Bundle savedInstanceState) {
14+
super.onCreate(savedInstanceState);
15+
16+
setContentView(R.layout.activity_main);
17+
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package co.paulburke.android.itemtouchhelperdemo;
2+
3+
import android.graphics.Color;
4+
import android.support.v7.widget.RecyclerView;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.TextView;
9+
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
14+
import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperAdapter;
15+
import co.paulburke.android.itemtouchhelperdemo.helper.ItemTouchHelperViewHolder;
16+
17+
/**
18+
* @author Paul Burke (ipaulpro)
19+
*/
20+
public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder>
21+
implements ItemTouchHelperAdapter {
22+
23+
private static final String[] STRINGS = new String[]{
24+
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
25+
};
26+
27+
private final List<String> mItems = new ArrayList<>();
28+
29+
public RecyclerListAdapter() {
30+
mItems.addAll(Arrays.asList(STRINGS));
31+
}
32+
33+
@Override
34+
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
35+
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
36+
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
37+
return itemViewHolder;
38+
}
39+
40+
@Override
41+
public void onBindViewHolder(final ItemViewHolder holder, int position) {
42+
holder.textView.setText(mItems.get(position));
43+
}
44+
45+
@Override
46+
public void onItemDismiss(int position) {
47+
mItems.remove(position);
48+
notifyItemRemoved(position);
49+
}
50+
51+
@Override
52+
public void onItemMove(int fromPosition, int toPosition) {
53+
String prev = mItems.remove(fromPosition);
54+
mItems.add(toPosition > fromPosition ? toPosition - 1 : toPosition, prev);
55+
notifyItemMoved(fromPosition, toPosition);
56+
}
57+
58+
@Override
59+
public int getItemCount() {
60+
return mItems.size();
61+
}
62+
63+
public static class ItemViewHolder extends RecyclerView.ViewHolder implements
64+
ItemTouchHelperViewHolder {
65+
66+
public final TextView textView;
67+
68+
public ItemViewHolder(View itemView) {
69+
super(itemView);
70+
textView = (TextView) itemView;
71+
}
72+
73+
@Override
74+
public void onItemSelected() {
75+
itemView.setBackgroundColor(Color.LTGRAY);
76+
}
77+
78+
@Override
79+
public void onItemClear() {
80+
itemView.setBackgroundColor(0);
81+
}
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package co.paulburke.android.itemtouchhelperdemo;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.support.v7.widget.LinearLayoutManager;
7+
import android.support.v7.widget.RecyclerView;
8+
import android.support.v7.widget.helper.ItemTouchHelper;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
13+
import co.paulburke.android.itemtouchhelperdemo.helper.SimpleItemTouchHelperCallback;
14+
15+
/**
16+
* @author Paul Burke (ipaulpro)
17+
*/
18+
public class RecyclerListFragment extends Fragment {
19+
20+
private ItemTouchHelper mItemTouchHelper;
21+
22+
public RecyclerListFragment() {
23+
}
24+
25+
@Nullable
26+
@Override
27+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
28+
return inflater.inflate(R.layout.fragment_main, container, false);
29+
}
30+
31+
@Override
32+
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
33+
super.onViewCreated(view, savedInstanceState);
34+
35+
RecyclerListAdapter adapter = new RecyclerListAdapter();
36+
37+
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
38+
recyclerView.setHasFixedSize(true);
39+
recyclerView.setAdapter(adapter);
40+
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
41+
42+
ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
43+
mItemTouchHelper = new ItemTouchHelper(callback);
44+
mItemTouchHelper.attachToRecyclerView(recyclerView);
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package co.paulburke.android.itemtouchhelperdemo.helper;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
5+
/**
6+
* Interface to notify a {@link RecyclerView.Adapter} of moving and dismissal event from a {@link
7+
* android.support.v7.widget.helper.ItemTouchHelper.Callback}.
8+
*
9+
* @author Paul Burke (ipaulpro)
10+
*/
11+
public interface ItemTouchHelperAdapter {
12+
13+
/**
14+
* Called when an item has been dragged far enough to trigger a move. This is called every time
15+
* an item is shifted, and not at the end of a "drop" event.
16+
*
17+
* @param fromPosition The start position of the moved item.
18+
* @param toPosition Then end position of the moved item.
19+
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
20+
* @see RecyclerView.ViewHolder#getAdapterPosition()
21+
*/
22+
void onItemMove(int fromPosition, int toPosition);
23+
24+
25+
/**
26+
* Called when an item has been dismissed by a swipe.
27+
*
28+
* @param position The position of the item dismissed.
29+
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
30+
* @see RecyclerView.ViewHolder#getAdapterPosition()
31+
*/
32+
void onItemDismiss(int position);
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package co.paulburke.android.itemtouchhelperdemo.helper;
2+
3+
import android.support.v7.widget.helper.ItemTouchHelper;
4+
5+
/**
6+
* Interface to notify an item ViewHolder of relevant callbacks from {@link
7+
* android.support.v7.widget.helper.ItemTouchHelper.Callback}.
8+
*
9+
* @author Paul Burke (ipaulpro)
10+
*/
11+
public interface ItemTouchHelperViewHolder {
12+
13+
/**
14+
* Called when the {@link ItemTouchHelper} first registers an item as being moved or swiped.
15+
* Implementations should update the item view to indicate it's active state.
16+
*/
17+
void onItemSelected();
18+
19+
20+
/**
21+
* Called when the {@link ItemTouchHelper} has completed the move or swipe, and the active item
22+
* state should be cleared.
23+
*/
24+
void onItemClear();
25+
}

0 commit comments

Comments
 (0)