Add RecyclerView Manager: finally correct swiping and scrolling handling

Add Delete Task confirmation
This commit is contained in:
2015-12-23 23:27:43 -05:00
parent df32a42951
commit ea6c23ae38
14 changed files with 208 additions and 72 deletions

View File

@@ -34,6 +34,14 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity" />
</activity>
<activity
android:name=".activities.TaskActivity"
android:label="@string/task_details_activity_title"
android:theme="@android:style/Theme.Holo.DialogWhenLarge" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.MainActivity" />
</activity>
</application>
</manifest>

View File

@@ -27,17 +27,24 @@ public class TaskTouchHelper extends ItemTouchHelper.SimpleCallback {
public TaskTouchHelper(TaskAdapter taskAdapter, TaskDataAccess taskDataAccess,
FragmentManager fragmentManager, RecyclerView recyclerView){
// No drag moves, only swipes
super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
// No drag moves, only left swipes (except for 1st element, see getSwipeDirs method)
super(0, ItemTouchHelper.LEFT);
this.taskAdapter = taskAdapter;
this.taskDataAccess = taskDataAccess;
this.fragmentManager = fragmentManager;
this.recyclerView = recyclerView;
}
@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
// Allow both directions swiping on first item, only left on the others
if (viewHolder.getAdapterPosition() == 0)
return ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
else return super.getSwipeDirs(recyclerView, viewHolder);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
//TODO: Not implemented here
return false;
}
@@ -45,19 +52,19 @@ public class TaskTouchHelper extends ItemTouchHelper.SimpleCallback {
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(viewHolder.itemView.getContext());
int itemPosition = viewHolder.getAdapterPosition();
String title = "Confirm";
String title = "";
boolean showDialog = false;
switch (direction)
{
// Mark item as Done
case ItemTouchHelper.LEFT:
title = "Done";
title = "Mark task as done?";
showDialog = sharedPref.getBoolean("pref_conf_done", true);
break;
// Increase task cycle count
case ItemTouchHelper.RIGHT:
title = "Next";
title = "Go to next task?";
showDialog = sharedPref.getBoolean("pref_conf_next", true);
break;
}

View File

@@ -140,7 +140,7 @@ public class MainActivity extends AppCompatActivity implements
super.onDestroy();
taskDataAccess.close();
}
// TODO: change add methods to add or update
@Override
public void onNewTaskDialogPositiveClick(DialogFragment dialog) {
// Get the dialog fragment
@@ -163,8 +163,6 @@ public class MainActivity extends AppCompatActivity implements
taskList.getId());
taskDataAccess.close();
// Update the corresponding tab adapter
//TasksFragment taskFragment = (TasksFragment) mSectionsPagerAdapter.getRegisteredFragment(listSpinner.getSelectedItemPosition());
//TaskAdapter taskAdapter = ((TaskAdapter)((RecyclerView)taskFragment.getView().findViewById(R.id.task_list_view)).getAdapter());
TaskAdapter taskAdapter = ((TaskDialogFragment)dialog).getTaskAdapter();
// Add the task
if (id == 0)
@@ -177,40 +175,27 @@ public class MainActivity extends AppCompatActivity implements
@Override
public void onNewTaskDialogNeutralClick(DialogFragment dialog) {
// TODO: add confirm dialog
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String title = getResources().getString(R.string.task_confirmation_delete_text);
boolean showDialog = sharedPref.getBoolean("pref_conf_del", true);
TaskDialogFragment taskDialogFragment = (TaskDialogFragment) dialog;
Bundle args = dialog.getArguments();
final long id = args.getLong("id");
// Delete task from Adapter
final int position = args.getInt("position");
final int itemPosition = args.getInt("position");
final TaskAdapter taskAdapter = taskDialogFragment.getTaskAdapter();
final Task task = taskAdapter.getItem(position);
taskAdapter.remove(position);
final RecyclerView view = taskDialogFragment.getRecyclerView();
// Setup the snack bar
final View view = taskDialogFragment.getRecyclerView();
Snackbar.make(view, "Task deleted", Snackbar.LENGTH_LONG)
.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
// Undo adapter changes
taskAdapter.add(task, position);
((RecyclerView)view).scrollToPosition(position);
}
}).setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
super.onDismissed(snackbar, event);
// When clicked on undo, do not write to DB
if (event == DISMISS_EVENT_ACTION) return;
// Commit the changes to DB
taskDataAccess.open();
taskDataAccess.deleteTask(id);
taskDataAccess.close();
}
}).show();
if (showDialog) {
ConfirmDialogFragment confirmDialogFragment =
ConfirmDialogFragment.newInstance(taskAdapter, title, view);
Bundle confirmArgs = new Bundle();
confirmArgs.putInt("ItemPosition", itemPosition);
confirmArgs.putInt("Direction", -1);
confirmDialogFragment.setArguments(confirmArgs);
confirmDialogFragment.show(getSupportFragmentManager(), title);
}
else PerformSwipeAction(taskDataAccess, taskAdapter, itemPosition, -1, view);
}
/** Called when user clicks on the New Task floating button */
@@ -251,7 +236,7 @@ public class MainActivity extends AppCompatActivity implements
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("pref_conf_next", false);
//editor.putBoolean("pref_conf_next", false);
switch (direction)
{
@@ -263,6 +248,10 @@ public class MainActivity extends AppCompatActivity implements
case ItemTouchHelper.RIGHT:
editor.putBoolean("pref_conf_next", false);
break;
// TODO: add delete action
case -1:
editor.putBoolean("pref_conf_del", false);
break;
}
editor.apply();
TaskAdapter taskAdapter = ((ConfirmDialogFragment)dialog).getTaskAdapter();
@@ -288,32 +277,37 @@ public class MainActivity extends AppCompatActivity implements
final View view) {
final long itemId = taskAdapter.getItemId(itemPosition);
final Task task = taskAdapter.getItem(itemPosition);
String title = "";
String action = "";
taskAdapter.remove(itemPosition);
switch (direction)
{
// Mark item as Done
case ItemTouchHelper.LEFT:
title = "Done";
action = "done";
break;
// Increase task cycle count
case ItemTouchHelper.RIGHT:
title = "Nexted";
action = "nexted";
task.setCycle(task.getCycle() + 1);
taskAdapter.add(task, taskAdapter.getItemCount());
break;
// TODO: add delete action
case -1:
action = "deleted";
taskAdapter.remove(itemPosition);
break;
}
// Setup the snack bar
Snackbar.make(view, "Task marked as " + title, Snackbar.LENGTH_LONG)
Snackbar.make(view, "Task " + action, Snackbar.LENGTH_LONG)
.setAction("Undo", new View.OnClickListener() {
@Override
public void onClick(View v) {
// Undo adapter changes
switch (direction)
{
// Nothing special to do yet
// Nothing special to do for done
case ItemTouchHelper.LEFT:
break;
// Remove the last item
@@ -321,6 +315,10 @@ public class MainActivity extends AppCompatActivity implements
taskAdapter.remove(taskAdapter.getItemCount() - 1);
task.setCycle(task.getCycle() - 1);
break;
// TODO: add delete action
// Nothing special to do for delete
case -1:
break;
}
// Reset the first item
taskAdapter.add(task, itemPosition);
@@ -346,6 +344,10 @@ public class MainActivity extends AppCompatActivity implements
case ItemTouchHelper.RIGHT:
taskDataAccess.increaseCycle(task.getCycle(), itemId);
break;
// TODO: add delete action
case -1:
// Commit the changes to DB
taskDataAccess.deleteTask(itemId);
}
taskDataAccess.close();
}

View File

@@ -0,0 +1,15 @@
package com.wismna.geoffroy.donext.activities;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.wismna.geoffroy.donext.R;
public class TaskActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task);
}
}

View File

@@ -48,6 +48,7 @@ public class TaskDataAccess {
values.put(DatabaseHelper.TASKS_COLUMN_NAME, name);
values.put(DatabaseHelper.TASKS_COLUMN_DESC, description);
values.put(DatabaseHelper.TASKS_COLUMN_PRIORITY, priorities.indexOf(priority));
//values.put(DatabaseHelper.TASKS_COLUMN_PRIORITY, priority);
values.put(DatabaseHelper.TASKS_COLUMN_LIST, taskList);
long insertId;
if (id == 0)

View File

@@ -74,18 +74,18 @@ public class ConfirmDialogFragment extends DialogFragment {
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getResources().getString(R.string.settings_confirm_message) + " " + message + "?")
.setPositiveButton(R.string.task_swipe_confirmation_yes, new DialogInterface.OnClickListener() {
builder.setMessage(message)
.setPositiveButton(R.string.task_confirmation_yes_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
confirmDialogListener.onConfirmDialogPositiveClick(ConfirmDialogFragment.this);
}
})
.setNegativeButton(R.string.task_swipe_confirmation_no, new DialogInterface.OnClickListener() {
.setNegativeButton(R.string.task_confirmation_no_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
ConfirmDialogFragment.this.getDialog().cancel();
}
}).setNeutralButton(R.string.task_swipe_confirmation_never, new DialogInterface.OnClickListener() {
}).setNeutralButton(R.string.task_confirmation_never_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
confirmDialogListener.onConfirmDialogNeutralClick(ConfirmDialogFragment.this);
}

View File

@@ -159,7 +159,7 @@ public class TaskDialogFragment extends DialogFragment {
mListener.onNewTaskDialogNeutralClick(TaskDialogFragment.this);
}
});
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light);
return builder.create();
}
}

View File

@@ -6,7 +6,6 @@ import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
@@ -20,6 +19,7 @@ import com.wismna.geoffroy.donext.adapters.TaskAdapter;
import com.wismna.geoffroy.donext.dao.Task;
import com.wismna.geoffroy.donext.database.TaskDataAccess;
import com.wismna.geoffroy.donext.listeners.RecyclerItemClickListener;
import com.wismna.geoffroy.donext.widgets.NoScrollingLayoutManager;
/**
* A fragment representing a list of Items.
@@ -70,7 +70,8 @@ public class TasksFragment extends Fragment {
// Set the Recycler view
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.task_list_view);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
//recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setLayoutManager(new NoScrollingLayoutManager(context));
TaskDataAccess taskDataAccess = new TaskDataAccess(view.getContext());
taskDataAccess.open();
@@ -90,14 +91,12 @@ public class TasksFragment extends Fragment {
taskDataAccess.close();
// Set ItemTouch helper in RecyclerView to handle swipe move on elements
// TODO: conflicts with ItemTouchListener, see why
ItemTouchHelper.Callback callback = new TaskTouchHelper(
taskAdapter, taskDataAccess, getFragmentManager(), recyclerView);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(recyclerView);
// Implements touch listener to add click detection
// TODO: conflicts with ItemTouchHelper (maybe add swipe detection there with onFling?)
//final Toast mToast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
@@ -106,8 +105,8 @@ public class TasksFragment extends Fragment {
//tasksFragmentListener.onItemClick(view, position);
TextView idTextView = (TextView) view.findViewById(R.id.task_id);
/*mToast.setText("Item " + idTextView.getText() + " clicked!");
mToast.show();*/
//mToast.setText("Item " + idTextView.getText() + " clicked!");
//mToast.show();
FragmentManager manager = getFragmentManager();
TaskDialogFragment taskDialogFragment = TaskDialogFragment.newInstance(taskAdapter, recyclerView);

View File

@@ -9,7 +9,6 @@ import android.view.View;
/**
* Created by geoffroy on 15-12-02.
* Listener class on RecyclerView to intercept touch events
* This allows disabling swipe on any other element than the first one
*/
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
@@ -33,15 +32,12 @@ public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListen
@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
int childId = view.getChildAdapterPosition(childView);
//int childId = view.getChildAdapterPosition(childView);
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
return true;
}
// Allows right swipe moves only on first element of the list, left everywhere
return e.getAction() != MotionEvent.ACTION_MOVE ||
((e.getY() - e.getHistoricalY(0) > 0 || (e.getY() - e.getHistoricalY(0)) < 0) ||
childId != 0 && e.getX() - e.getHistoricalX(0) > 0);
return false;
}
@Override

View File

@@ -0,0 +1,20 @@
package com.wismna.geoffroy.donext.widgets;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
/**
* Created by geoffroy on 15-12-21.
* Custom Layout Manager that disables vertical scrolling for the RecyclerView
*/
public class NoScrollingLayoutManager extends LinearLayoutManager {
public NoScrollingLayoutManager(Context context) {
super(context);
}
@Override
public boolean canScrollVertically() {
return false;
}
}

View File

@@ -7,6 +7,7 @@ import android.view.MotionEvent;
/**
* Created by geoffroy on 15-12-04.
* Custom ViewPager to forbid vertical swiping between tabs
*/
public class NonSwipeableViewPager extends ViewPager {

View File

@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/new_task_list"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="@+id/new_task_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/new_task_name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/new_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/new_task_description"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/new_task_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/new_task_priority"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/new_task_priority"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/new_task_priority_low"
android:text="@string/new_task_priority_low"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RadioButton>
<RadioButton
android:id="@+id/new_task_priority_normal"
android:text="@string/new_task_priority_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true">
</RadioButton>
<RadioButton
android:id="@+id/new_task_priority_high"
android:text="@string/new_task_priority_high"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RadioButton>
</RadioGroup>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<Button
android:id="@+id/new_task_save"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/new_task_save" />
<Button
android:id="@+id/new_task_cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/new_task_cancel"/>
</LinearLayout>
</LinearLayout>

View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context=".activities.MainActivity">
<TextView

View File

@@ -27,16 +27,20 @@
<string name="new_task_cancel">Cancel</string>
<string name="new_task_delete">Delete</string>
<!-- String related to task details activity -->
<!-- Strings related to task details activity -->
<string name="task_details_activity_title">Details</string>
<!-- String related to task list Recycler View -->
<string name="task_swipe_confirmation_title">Confirm</string>
<string name="task_swipe_confirmation_done">Mark task as Done?</string>
<string name="task_swipe_confirmation_next">Mark task as Nexted?</string>
<string name="task_swipe_confirmation_yes">Yes</string>
<string name="task_swipe_confirmation_no">No</string>
<string name="task_swipe_confirmation_never">Never ask again</string>
<!-- Strings related to the confirmation dialog -->
<string name="task_confirmation_done_text">Mark task as Done?</string>
<string name="task_confirmation_next_text">Go to next task?</string>
<string name="task_confirmation_delete_text">Delete this task?</string>
<string name="task_confirmation_done_button">Done</string>
<string name="task_confirmation_next_button">Next</string>
<string name="task_confirmation_delete_button">Delete</string>
<string name="task_confirmation_yes_button">Yes</string>
<string name="task_confirmation_no_button">No</string>
<string name="task_confirmation_never_button">Never ask again</string>
<!-- Strings related to Settings -->
<string name="settings_confirm_message">Mark task as</string>