Better swipe handling on RecyclerView items

Implement left swipe to mark any task as done
This commit is contained in:
2015-12-20 16:34:40 -05:00
parent 239e34d9fd
commit df32a42951
4 changed files with 15 additions and 16 deletions

View File

@@ -323,7 +323,7 @@ public class MainActivity extends AppCompatActivity implements
break; break;
} }
// Reset the first item // Reset the first item
taskAdapter.add(task, 0); taskAdapter.add(task, itemPosition);
((RecyclerView)view).scrollToPosition(0); ((RecyclerView)view).scrollToPosition(0);
} }
}).setCallback(new Snackbar.Callback() { }).setCallback(new Snackbar.Callback() {

View File

@@ -37,15 +37,6 @@ public class ConfirmDialogFragment extends DialogFragment {
return fragment; return fragment;
} }
/** Allows refreshing the first item of the adapter */
private void RefreshAdapter()
{
Bundle args = getArguments();
int itemPosition = args.getInt("ItemPosition");
getTaskAdapter().notifyItemChanged(itemPosition);
}
public TaskAdapter getTaskAdapter() { public TaskAdapter getTaskAdapter() {
return taskAdapter; return taskAdapter;
} }
@@ -57,7 +48,12 @@ public class ConfirmDialogFragment extends DialogFragment {
@Override @Override
public void onCancel(DialogInterface dialog) { public void onCancel(DialogInterface dialog) {
super.onCancel(dialog); super.onCancel(dialog);
RefreshAdapter();
// Allows refreshing the first item of the adapter
Bundle args = getArguments();
int itemPosition = args.getInt("ItemPosition");
getTaskAdapter().notifyItemChanged(itemPosition);
} }
@Override @Override
@@ -87,7 +83,7 @@ public class ConfirmDialogFragment extends DialogFragment {
.setNegativeButton(R.string.task_swipe_confirmation_no, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.task_swipe_confirmation_no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog // User cancelled the dialog
RefreshAdapter(); ConfirmDialogFragment.this.getDialog().cancel();
} }
}).setNeutralButton(R.string.task_swipe_confirmation_never, new DialogInterface.OnClickListener() { }).setNeutralButton(R.string.task_swipe_confirmation_never, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {

View File

@@ -77,11 +77,11 @@ public class TasksFragment extends Fragment {
// Set total cycles // Set total cycles
TextView totalCyclesView = (TextView) view.findViewById(R.id.total_task_cycles); TextView totalCyclesView = (TextView) view.findViewById(R.id.total_task_cycles);
totalCyclesView.setText(String.valueOf(taskDataAccess.getTotalCycles(taskListId))); totalCyclesView.setText(String.valueOf(taskDataAccess.getTotalCycles(taskListId) + " cycles"));
// Set total count // Set total count
TextView totalTasksView = (TextView) view.findViewById(R.id.total_task_count); TextView totalTasksView = (TextView) view.findViewById(R.id.total_task_count);
totalTasksView.setText(String.valueOf(taskDataAccess.getTaskCount(taskListId))); totalTasksView.setText(String.valueOf(taskDataAccess.getTaskCount(taskListId) + " tasks"));
// Set RecyclerView Adapter // Set RecyclerView Adapter
final TaskAdapter taskAdapter = new TaskAdapter(taskDataAccess.getAllTasks(taskListId), mListener); final TaskAdapter taskAdapter = new TaskAdapter(taskDataAccess.getAllTasks(taskListId), mListener);

View File

@@ -37,8 +37,11 @@ public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListen
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView)); mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
} }
// Allows swipe moves only on first element of the list
return childId != 0; // 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);
} }
@Override @Override