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;
}
// Reset the first item
taskAdapter.add(task, 0);
taskAdapter.add(task, itemPosition);
((RecyclerView)view).scrollToPosition(0);
}
}).setCallback(new Snackbar.Callback() {

View File

@@ -37,15 +37,6 @@ public class ConfirmDialogFragment extends DialogFragment {
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() {
return taskAdapter;
}
@@ -57,7 +48,12 @@ public class ConfirmDialogFragment extends DialogFragment {
@Override
public void onCancel(DialogInterface 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
@@ -87,7 +83,7 @@ public class ConfirmDialogFragment extends DialogFragment {
.setNegativeButton(R.string.task_swipe_confirmation_no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
RefreshAdapter();
ConfirmDialogFragment.this.getDialog().cancel();
}
}).setNeutralButton(R.string.task_swipe_confirmation_never, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

View File

@@ -77,11 +77,11 @@ public class TasksFragment extends Fragment {
// Set total 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
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
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)) {
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