Got dragging working in tandem with clicking. So awesome.

pull/14/head
Tim Su 14 years ago
parent 108a4efbb8
commit 0db134e095

@ -35,19 +35,23 @@ import android.widget.ImageView;
import android.widget.ListView; import android.widget.ListView;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.utility.AndroidUtilities;
public class TouchListView extends ListView { public class TouchListView extends ListView {
private ImageView mDragView; private ImageView mDragView;
private View mOriginalView;
private WindowManager mWindowManager; private WindowManager mWindowManager;
private WindowManager.LayoutParams mWindowParams; private WindowManager.LayoutParams mWindowParams;
private int mDragPos; // which item is being dragged private int mDragPos; // which item is being dragged
private int mFirstDragPos; // where was the dragged item originally private int mFirstDragPos; // where was the dragged item originally
private int mDragPoint; // at what offset inside the item did the user grab it private int mDragPoint; // at what offset inside the item did the user grab it
private int mCoordOffset; // the difference between screen coordinates and coordinates in this view private int mCoordOffset; // the difference between screen coordinates and coordinates in this view
private int mDragStartX; private float mDragStartX, mDragCurrentX;
private long mDragStartTime;
private DragListener mDragListener; private DragListener mDragListener;
private DropListener mDropListener; private DropListener mDropListener;
private SwipeListener mSwipeListener; private SwipeListener mSwipeListener;
private GrabberClickListener mClickListener;
private int mUpperBound; private int mUpperBound;
private int mLowerBound; private int mLowerBound;
private int mHeight; private int mHeight;
@ -63,6 +67,7 @@ public class TouchListView extends ListView {
private int mItemHeightExpanded=-1; private int mItemHeightExpanded=-1;
private int grabberId=-1; private int grabberId=-1;
private int dragndropBackgroundColor=0x00000000; private int dragndropBackgroundColor=0x00000000;
private Thread longPressThread;
public TouchListView(Context context, AttributeSet attrs) { public TouchListView(Context context, AttributeSet attrs) {
this(context, attrs, 0); this(context, attrs, 0);
@ -74,15 +79,17 @@ public class TouchListView extends ListView {
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
if (attrs != null) { if (attrs != null) {
TypedArray a=getContext() TypedArray a = getContext().obtainStyledAttributes(attrs,
.obtainStyledAttributes(attrs, R.styleable.TouchListView, 0, 0);
R.styleable.TouchListView,
0, 0); mItemHeightNormal = a.getDimensionPixelSize(
R.styleable.TouchListView_normal_height, 0);
mItemHeightNormal=a.getDimensionPixelSize(R.styleable.TouchListView_normal_height, 0); mItemHeightExpanded = a.getDimensionPixelSize(
mItemHeightExpanded=a.getDimensionPixelSize(R.styleable.TouchListView_expanded_height, mItemHeightNormal); R.styleable.TouchListView_expanded_height,
mItemHeightNormal);
grabberId = a.getResourceId(R.styleable.TouchListView_grabber, -1); grabberId = a.getResourceId(R.styleable.TouchListView_grabber, -1);
dragndropBackgroundColor=a.getColor(R.styleable.TouchListView_dragndrop_background, 0x00000000); dragndropBackgroundColor = a.getColor(
R.styleable.TouchListView_dragndrop_background, 0x00000000);
mRemoveMode = a.getInt(R.styleable.TouchListView_remove_mode, -1); mRemoveMode = a.getInt(R.styleable.TouchListView_remove_mode, -1);
a.recycle(); a.recycle();
@ -154,6 +161,10 @@ public class TouchListView extends ListView {
mDragPos = itemnum; mDragPos = itemnum;
mFirstDragPos = mDragPos; mFirstDragPos = mDragPos;
mHeight = getHeight(); mHeight = getHeight();
mDragStartX = ev.getX();
mDragStartTime = System.currentTimeMillis();
mOriginalView = item;
int touchSlop = mTouchSlop; int touchSlop = mTouchSlop;
mUpperBound = Math.min(y - touchSlop, mHeight / 3); mUpperBound = Math.min(y - touchSlop, mHeight / 3);
mLowerBound = Math.max(y + touchSlop, mHeight * 2 /3); mLowerBound = Math.max(y + touchSlop, mHeight * 2 /3);
@ -310,7 +321,7 @@ public class TouchListView extends ListView {
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
Rect r = mTempRect; Rect r = mTempRect;
mDragView.getDrawingRect(r); mDragView.getDrawingRect(r);
stopDragging(); stopDragging(ev);
if (mDragPos == mFirstDragPos && ev.getX() > mDragStartX + 20) { if (mDragPos == mFirstDragPos && ev.getX() > mDragStartX + 20) {
if (mSwipeListener!= null) { if (mSwipeListener!= null) {
mSwipeListener.swipeRight(mFirstDragPos); mSwipeListener.swipeRight(mFirstDragPos);
@ -373,7 +384,7 @@ public class TouchListView extends ListView {
} }
private void startDragging(Bitmap bm, int x, int y) { private void startDragging(Bitmap bm, int x, int y) {
stopDragging(); stopDragging(null);
mWindowParams = new WindowManager.LayoutParams(); mWindowParams = new WindowManager.LayoutParams();
mWindowParams.gravity = Gravity.TOP|Gravity.LEFT; mWindowParams.gravity = Gravity.TOP|Gravity.LEFT;
@ -390,16 +401,34 @@ public class TouchListView extends ListView {
mWindowParams.windowAnimations = 0; mWindowParams.windowAnimations = 0;
ImageView v = new ImageView(getContext()); ImageView v = new ImageView(getContext());
// int backGroundColor = getContext().getResources().getColor(R.color.dragndrop_background);
v.setBackgroundColor(dragndropBackgroundColor); v.setBackgroundColor(dragndropBackgroundColor);
v.setImageBitmap(bm); v.setImageBitmap(bm);
mDragBitmap = bm; mDragBitmap = bm;
mWindowManager = (WindowManager)getContext().getSystemService("window"); mWindowManager = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(v, mWindowParams); mWindowManager.addView(v, mWindowParams);
mDragView = v; mDragView = v;
if(mClickListener != null) {
longPressThread = new Thread(longPressRunnable);
longPressThread.start();
}
} }
private final Runnable longPressRunnable = new Runnable() {
public void run() {
AndroidUtilities.sleepDeep(1000L);
if(Thread.currentThread().isInterrupted())
return;
if(mDragPos == mFirstDragPos &&
Math.abs(mDragCurrentX - mDragStartX) < 10) {
stopDragging(null);
mClickListener.onLongClick(mOriginalView);
}
}
};
private void dragView(int x, int y) { private void dragView(int x, int y) {
float alpha = 1.0f; float alpha = 1.0f;
int width = mDragView.getWidth(); int width = mDragView.getWidth();
@ -418,18 +447,38 @@ public class TouchListView extends ListView {
} }
mWindowParams.y = y - mDragPoint + mCoordOffset; mWindowParams.y = y - mDragPoint + mCoordOffset;
mWindowManager.updateViewLayout(mDragView, mWindowParams); mWindowManager.updateViewLayout(mDragView, mWindowParams);
mDragCurrentX = x;
}
private void stopDragging(MotionEvent ev) {
if (mDragBitmap != null) {
mDragBitmap.recycle();
mDragBitmap = null;
} }
private void stopDragging() {
if (mDragView != null) { if (mDragView != null) {
WindowManager wm = (WindowManager)getContext().getSystemService("window"); WindowManager wm = (WindowManager) getContext().getSystemService(
Context.WINDOW_SERVICE);
wm.removeView(mDragView); wm.removeView(mDragView);
mDragView.setImageDrawable(null); mDragView.setImageDrawable(null);
if (ev != null && mClickListener != null) {
// detect press & long press case
if(mDragPos == mFirstDragPos &&
Math.abs(mDragCurrentX - mDragStartX) < 10) {
long pressTime = System.currentTimeMillis() - mDragStartTime;
if(pressTime < 1000)
mClickListener.onClick(mOriginalView);
else
mClickListener.onLongClick(mOriginalView);
}
}
mDragView = null; mDragView = null;
} }
if (mDragBitmap != null) {
mDragBitmap.recycle(); if(longPressThread != null) {
mDragBitmap = null; longPressThread.interrupt();
longPressThread = null;
} }
} }
@ -445,6 +494,19 @@ public class TouchListView extends ListView {
mSwipeListener = l; mSwipeListener = l;
} }
public void setClickListener(GrabberClickListener listener) {
this.mClickListener = listener;
}
public void setDragndropBackgroundColor(int color) {
this.dragndropBackgroundColor = color;
}
public interface GrabberClickListener {
public void onClick(View v);
public void onLongClick(View v);
}
public interface DragListener { public interface DragListener {
void drag(int from, int to); void drag(int from, int to);
} }

@ -0,0 +1,109 @@
package com.todoroo.astrid.subtasks;
import java.util.concurrent.atomic.AtomicReference;
import android.database.Cursor;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import com.commonsware.cwac.tlv.TouchListView;
import com.commonsware.cwac.tlv.TouchListView.DropListener;
import com.commonsware.cwac.tlv.TouchListView.GrabberClickListener;
import com.timsu.astrid.R;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.adapter.TaskAdapter;
import com.todoroo.astrid.data.Task;
public class SubtasksListFragment extends TaskListFragment {
private final DisplayMetrics metrics = new DisplayMetrics();
public TouchListView getTouchListView() {
TouchListView tlv = (TouchListView) getListView();
return tlv;
}
@Override
protected View getListBody(ViewGroup root) {
return getActivity().getLayoutInflater().inflate(R.layout.task_list_body_subtasks, root, false);
}
@Override
protected void setUpUiComponents() {
super.setUpUiComponents();
TypedValue tv = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.asThemeTextColor, tv, false);
getTouchListView().setDragndropBackgroundColor(tv.data);
getTouchListView().setDropListener(dropListener);
getTouchListView().setClickListener(rowClickListener);
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
}
private final DropListener dropListener = new DropListener() {
@Override
public void drop(int from, int to) {
System.err.println("DROPPED TO " + to);
}
};
private final GrabberClickListener rowClickListener = new GrabberClickListener() {
@Override
public void onLongClick(final View v) {
System.err.println(v);
getListView().showContextMenuForChild(v);
}
@Override
public void onClick(View v) {
System.err.println(v);
((DraggableTaskAdapter) taskAdapter).getListener().onClick(v);
}
};
// --- task adapter
@Override
protected TaskAdapter createTaskAdapter(TodorooCursor<Task> cursor) {
return new DraggableTaskAdapter(this, R.layout.task_adapter_row,
cursor, sqlQueryTemplate, false, null);
}
private final class DraggableTaskAdapter extends TaskAdapter {
private DraggableTaskAdapter(TaskListFragment activity, int resource,
Cursor c, AtomicReference<String> query, boolean autoRequery,
OnCompletedTaskListener onCompletedTaskListener) {
super(activity, resource, c, query, autoRequery,
onCompletedTaskListener);
applyListeners = APPLY_LISTENERS_NONE;
}
@Override
protected ViewHolder getTagFromCheckBox(View v) {
return (ViewHolder)((View)v.getParent()).getTag();
}
@Override
public synchronized void setFieldContentsAndVisibility(View view) {
super.setFieldContentsAndVisibility(view);
view.getLayoutParams().height = Math.round(45 * metrics.density);
}
@Override
protected void addListeners(View container) {
super.addListeners(container);
}
public TaskRowListener getListener() {
return listener;
}
}
}

@ -0,0 +1,30 @@
package com.todoroo.astrid.subtasks;
import com.todoroo.andlib.data.Property.IntegerProperty;
import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.StringProperty;
import com.todoroo.astrid.data.Metadata;
/**
* Metadata entries for a GTasks Task
* @author Tim Su <tim@todoroo.com>
*
*/
public class SubtasksMetadata {
static final int VALUE_UNSET = -1;
/** metadata key */
public static final String METADATA_KEY = "subtasks"; //$NON-NLS-1$
/** list id */
public static final StringProperty LIST_ID = new StringProperty(Metadata.TABLE,
Metadata.VALUE1.name);
public static final IntegerProperty INDENT = new IntegerProperty(Metadata.TABLE,
Metadata.VALUE2.name);
public static final LongProperty ORDER = new LongProperty(Metadata.TABLE,
Metadata.VALUE3.name);
}

@ -26,6 +26,7 @@
android:smoothScrollbar="false" android:smoothScrollbar="false"
tlv:normal_height="45dip" tlv:normal_height="45dip"
tlv:grabber="@id/rowBody"
/> />
</FrameLayout> </FrameLayout>

@ -31,6 +31,7 @@ import com.todoroo.astrid.reminders.ReminderDialog;
import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.StartupService;
import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsConstants;
import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.subtasks.SubtasksListFragment;
import com.todoroo.astrid.ui.DateChangedAlerts; import com.todoroo.astrid.ui.DateChangedAlerts;
/** /**

@ -81,7 +81,7 @@ public class DraggableTaskListFragment extends TaskListFragment {
super(activity, resource, c, query, autoRequery, super(activity, resource, c, query, autoRequery,
onCompletedTaskListener); onCompletedTaskListener);
applyListenersToRowBody = true; applyListeners = APPLY_LISTENERS_ROW_BODY;
} }
@Override @Override

@ -1,35 +0,0 @@
package com.todoroo.astrid.activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import com.commonsware.cwac.tlv.TouchListView;
import com.timsu.astrid.R;
public class SubtasksListFragment extends TaskListFragment {
public TouchListView getTouchListView() {
TouchListView tlv = (TouchListView) getListView();
return tlv;
}
@Override
protected View getListBody(ViewGroup root) {
return getActivity().getLayoutInflater().inflate(R.layout.task_list_body_subtasks, root, false);
}
@Override
protected void setUpUiComponents() {
super.setUpUiComponents();
getTouchListView().setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
}
}

@ -59,7 +59,6 @@ import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.service.ExceptionService;
import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Pair; import com.todoroo.andlib.utility.Pair;
@ -150,12 +149,13 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
// --- instance variables // --- instance variables
@Autowired
private ExceptionService exceptionService;
@Autowired @Autowired
private TaskService taskService; private TaskService taskService;
public static int APPLY_LISTENERS_PARENT = 0;
public static int APPLY_LISTENERS_ROW_BODY= 1;
public static int APPLY_LISTENERS_NONE = 2;
protected final TaskListFragment fragment; protected final TaskListFragment fragment;
protected final HashMap<Long, Boolean> completedItems = new HashMap<Long, Boolean>(0); protected final HashMap<Long, Boolean> completedItems = new HashMap<Long, Boolean>(0);
protected OnCompletedTaskListener onCompletedTaskListener = null; protected OnCompletedTaskListener onCompletedTaskListener = null;
@ -165,7 +165,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
private DetailLoaderThread detailLoader; private DetailLoaderThread detailLoader;
private ActionsLoaderThread actionsLoader; private ActionsLoaderThread actionsLoader;
private int fontSize; private int fontSize;
protected boolean applyListenersToRowBody = false; protected int applyListeners = APPLY_LISTENERS_PARENT;
private long mostRecentlyMade = -1; private long mostRecentlyMade = -1;
private final ScaleAnimation scaleAnimation; private final ScaleAnimation scaleAnimation;
@ -595,10 +595,10 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
} }
}); });
if(applyListenersToRowBody) { if(applyListeners == APPLY_LISTENERS_ROW_BODY) {
viewHolder.rowBody.setOnCreateContextMenuListener(listener); viewHolder.rowBody.setOnCreateContextMenuListener(listener);
viewHolder.rowBody.setOnClickListener(listener); viewHolder.rowBody.setOnClickListener(listener);
} else { } else if(applyListeners == APPLY_LISTENERS_PARENT) {
container.setOnCreateContextMenuListener(listener); container.setOnCreateContextMenuListener(listener);
container.setOnClickListener(listener); container.setOnClickListener(listener);
} }
@ -994,7 +994,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
return (ViewHolder)((View)v.getParent()).getTag(); return (ViewHolder)((View)v.getParent()).getTag();
} }
private class TaskRowListener implements OnCreateContextMenuListener, OnClickListener { public class TaskRowListener implements OnCreateContextMenuListener, OnClickListener {
public void onCreateContextMenu(ContextMenu menu, View v, public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) { ContextMenuInfo menuInfo) {

Loading…
Cancel
Save