How to swipe from both side in ListView? - android

I am trying to do swipe from both side in Listview but now i only able to do from right side, but i want to do swipe from both left and right please can any one tell me how to do that.
private void swipe(int dis) {
if (dis > mMenuView.getWidth()) {
dis = mMenuView.getWidth();
}
if (dis < 0) {
dis = 0;
}
mContentView.layout(-dis, mContentView.getTop(), mContentView.getWidth() - dis, getMeasuredHeight());
mMenuView.layout(mContentView.getWidth() - dis, mMenuView.getTop(),
mContentView.getWidth() + mMenuView.getWidth() - dis,
mMenuView.getBottom());
}
/onSwipe Function/
public boolean onSwipe(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = (int) event.getX();
isFling = false;
break;
case MotionEvent.ACTION_MOVE:
// Log.i("byz", "downX = " + mDownX + ", moveX = " + event.getX());
int dis = (int) (mDownX - event.getX());
if (state == STATE_OPEN) {
dis += mMenuView.getWidth();
}
swipe(dis);
break;
case MotionEvent.ACTION_UP:
if (isFling
|| (mDownX - event.getX()) > (mMenuView.getWidth() / 1.5)) {
// open
smoothOpenMenu();
} else {
// close
smoothCloseMenu();
return false;
}
break;
}
return true;
}
Please go through the above code and suggest me some solution.

Related

Calling TextView setText() from onTouchListener stucks UI

Here is my sample code:
#Override
public boolean onTouchEvent(MotionEvent e) {
float center = mScrubberX + mScrubberWidth / 2;
float handleLeft = center - HANDLE_WIDTH;
float handleRight = center + HANDLE_WIDTH;
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
if (e.getX() > handleLeft && e.getX() < handleRight) {
mActionDown = true;
mActionDownX = e.getX() - mScrubberX;
}
break;
case MotionEvent.ACTION_MOVE:
float newLeft = e.getX() - mActionDownX;
float newRight = newLeft + mScrubberWidth;
if (mActionDown && !mIsStreaming) {
if (newLeft > mScrubberContainer.getLeft() &&
newRight < mScrubberContainer.getRight()) {
mScrubberX = e.getX() - mActionDownX;
textView.setText(""+mScrubberX)
} else if (newRight < mScrubberContainer.getRight()) {
mScrubberX = mScrubberContainer.getLeft();
} else if (newLeft > mScrubberContainer.getLeft()) {
mScrubberX = mScrubberContainer.getRight() - mScrubberWidth;
}
}
break;
case MotionEvent.ACTION_UP:
mActionDown = false;
break;
default:
//Do nothing
}
invalidate();
return true;
}
Please check line "textView.setText(""+mScrubberX)" struck the UI. I have tried putting this inside Handler, Asynctask.executor, runOnUIThread but nothing worked.
Please suggest some solution to get rid of this issue.

Android Google Maps: Disable Scroll on Zoom

I tried by creating custom class for mapview but it is not working. Can any one suggest me an approach. How to disable scroll when user tries to zoom. Scroll should be enable when zoom is not performed.
I Have solved the problem using below code.
Create variables in Class
private ScaleGestureDetector gestureDetector;
private long lastZoomTime = 0;
private float lastSpan = -1;
private long firstClick=0;
private long lastClick;
GoogleMap mMap;
In OnMapReady method add below code:
mMap = googleMap;
gestureDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleGestureDetector.OnScaleGestureListener() {
#Override
public boolean onScale(ScaleGestureDetector detector) {
Log.i("onScale ", " onScale ");
if (lastSpan == -1) {
lastSpan = detector.getCurrentSpan();
}
if (detector.getEventTime() - lastZoomTime >= 50) {
lastZoomTime = detector.getEventTime();
mMap.animateCamera(CameraUpdateFactory.zoomBy(getZoomValue(detector.getCurrentSpan(), lastSpan)), 50, null);
lastSpan = detector.getCurrentSpan();
}
return false;
}
#Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
lastSpan = -1;
return true;
}
#Override
public void onScaleEnd(ScaleGestureDetector detector) {
lastSpan = -1;
}
});
Add Below Methods in class
private float getZoomValue(float currentSpan, float lastSpan) {
Log.i("ongetZoomValue ", " getZoomValue ");
double value = (Math.log(currentSpan / lastSpan) / Math.log(1.55d));
return (float) value;
}
public boolean dispatchTouchEvent(MotionEvent ev) {
//Log.i("on dispatchTouchEvent ", " dispatchTouchEvent ");
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
fingers = fingers + 1;
break;
case MotionEvent.ACTION_POINTER_UP:
fingers = fingers - 1;
break;
case MotionEvent.ACTION_UP:
fingers = 0;
break;
case MotionEvent.ACTION_DOWN:
if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
Log.i("on count=0", " count=0 ");
count = 0;
}
count++;
if (count == 1) {
Log.i("on count ", " count=1 ");
firstClick = System.currentTimeMillis();
} else if (count == 2) {
Log.i("on count ", " count=2 ");
lastClick = System.currentTimeMillis();
if (lastClick - firstClick < 300) {
CameraUpdate getzoom = CameraUpdateFactory.zoomIn();
mMap.animateCamera(getzoom, 400, null);
Log.i("on double click ", " event ");
}
}
fingers=1;
break;
}
if (fingers >1) {
//Log.i("2fingersaction ", "2fingersaction ");
mMap.getUiSettings().setScrollGesturesEnabled(false);
//mMap.getUiSettings().setRotateGesturesEnabled(false); // disableScrolling()
}
else if (fingers < 1 )
{
//Log.i("onefinger ", "onefinger ");
mMap.getUiSettings().setScrollGesturesEnabled(true);
// enableScrolling();
}
if (fingers > 1) {
//Log.i("double if condition ", "doubleifcondition ");
return gestureDetector.onTouchEvent(ev);
} else
return super.dispatchTouchEvent(ev);
}

How can I use pinch zoom without calling my swipe function in webview?

When I use the pinch zoom gesture, in a horizontal way, the swipe function is always called when it reached a certain difference of the pressed coordinates and the released coordinates.
How can I fix this?
This are the settings for the webview
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setSupportZoom(true);
view.getSettings().setDisplayZoomControls(false);
The swipe function
protected void swipePage(View v, MotionEvent event, int book) {
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case (MotionEvent.ACTION_DOWN):
swipeOriginX = event.getX();
swipeOriginY = event.getY();
break;
case (MotionEvent.ACTION_UP):
int quarterWidth = (int) (screenWidth * 0.3);
float diffX = swipeOriginX - event.getX();
float diffY = swipeOriginY - event.getY();
float absDiffX = Math.abs(diffX);
float absDiffY = Math.abs(diffY);
if ((diffX > quarterWidth) && (absDiffX > absDiffY)) {
try {
navigator.goToNextChapter(index);
} catch (Exception e) {
errorMessage(getString(R.string.error_cannotTurnPage));
}
} else if ((diffX < -quarterWidth) && (absDiffX > absDiffY)) {
try {
navigator.goToPrevChapter(index);
} catch (Exception e) {
errorMessage(getString(R.string.error_cannotTurnPage));
}
}
break;
}
}
check if this work - put a if condition checking if single finger touch is there then only do the swipe. change "event" variable to your local variable.
if(event.getPointerCount() == 1)
{
switch (action) {
case (MotionEvent.ACTION_DOWN):
swipeOriginX = event.getX();
swipeOriginY = event.getY();
break;
case (MotionEvent.ACTION_UP):
int quarterWidth = (int) (screenWidth * 0.3);
float diffX = swipeOriginX - event.getX();
float diffY = swipeOriginY - event.getY();
float absDiffX = Math.abs(diffX);
float absDiffY = Math.abs(diffY);
if ((diffX > quarterWidth) && (absDiffX > absDiffY)) {
x = 1;
try {
navigator.goToNextChapter(index);
x = 0;
} catch (Exception e) {
x = 1;
errorMessage(getString(R.string.error_cannotTurnPage));
}
} else if ((diffX < -quarterWidth) && (absDiffX > absDiffY)) {
x = 1;
try {
navigator.goToPrevChapter(index);
x = 0;
} catch (Exception e) {
x = 1;
errorMessage(getString(R.string.error_cannotTurnPage));
}
break;
}
}
}
else if(event.getPointerCount()==2)
{
// code for pinch zoom
}

ACTION_UP doesn't fire when finger is up

So, I went through all the discussions on the issue here.
I return true, there is no other place where the ACTION_UP is handled .
I use a thread to handle graphics and calculations, although I can't figure out why this should matter.
this is my implementation of the onTouch method:
#Override
public boolean onTouch(View v, MotionEvent event) {
synchronized (this) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
int pointerIndex = event.getAction()
& MotionEvent.ACTION_POINTER_INDEX_MASK >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int pointerCount = event.getPointerCount();
TouchEvent touchEvent;
for (int i = 0; i < MAX_TOUCHPOINTS; i++) {
if (i >= pointerCount) {
isTouched[i] = false;
id[i] = -1;
continue;
}
int pointerId = event.getPointerId(i);
if ((event.getAction() != MotionEvent.ACTION_MOVE && i != pointerIndex)) {
// if its an up/down/cancel/out event mask the id to see if
// we should process it for this touch point
continue;
}
Log.i("Logged TouchEvents", "" + event.getAction());
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
touchEvent = touchEventPool.newObject();
touchEvent.type = TouchEvent.TOUCH_DOWN;
touchEvent.pointer = pointerId;
touchEvent.x = touchX[i] = (int) (event.getX() * scaleX);
touchEvent.y = touchY[i] = (int) (event.getY() * scaleY);
isTouched[i] = true;
id[i] = pointerId;
toucheventBuffer.add(touchEvent);
Log.d("MultiTouchHandler", "TouchEvent.Down ("
+ touchEvent.x + "," + touchEvent.y + ")");
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
touchEvent = touchEventPool.newObject();
touchEvent.type = TouchEvent.TOUCH_UP;
touchEvent.pointer = pointerId;
touchEvent.x = touchX[i] = (int) (event.getX() * scaleX);
touchEvent.y = touchY[i] = (int) (event.getY() * scaleY);
isTouched[i] = false;
id[i] = -1;
toucheventBuffer.add(touchEvent);
Log.d("MultiTouchHandler", "TouchEvent.TouchUp ("
+ touchEvent.x + "," + touchEvent.y + ")");
break;
case MotionEvent.ACTION_MOVE:
touchEvent = touchEventPool.newObject();
touchEvent.type = TouchEvent.TOUCH_DRAG;
touchEvent.pointer = pointerId;
touchEvent.x = touchX[i] = (int) (event.getX() * scaleX);
touchEvent.y = touchY[i] = (int) (event.getY() * scaleY);
isTouched[i] = true;
id[i] = pointerId;
toucheventBuffer.add(touchEvent);
Log.d("MultiTouchHandler", "TouchEvent.TouchDrag ("
+ touchEvent.x + "," + touchEvent.y + ")");
break;
default:
break;
}
}
}
return true;
}
Please note that the variable 'action'(checked before the switch/case) never equals 1(ACTION_UP)
Thanks in advance.

How to get the item in listview by touch event in android, so I set background color of that item when I move event on it

I want to move the figure on list view item that contains phone number.
When I move figure from right to left, make a call. I'm failing to get that particoular item in list view to get the phone number and make a call.
My code is
#Override
arg1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
float startX = 0;
float startY;
float endX;
float endY;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getX();
startY = (int) event.getY();
System.out.println("startX" + startX);
System.out.println("startY" + startY);
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = (int) event.getY();
System.out.println("endX" + endX);
System.out.println("endY" + endY);
float sub = endX - startX;
System.out.println("sub" + sub);
if ((endX - startX) < 100) {
arg0.setBackgroundColor(Color.GREEN);
}
if ((endX - startX) > 100) {
try {
TextView ph_tv = (TextView) arg0
.findViewById(R.id.textnum);
String Pho_no = ph_tv.getText().toString();
arg0.setBackgroundColor(Color.RED);
Intent intent = new Intent(
Intent.ACTION_CALL, Uri
.parse("tel:" + Pho_no));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
lv.setBackgroundColor(Color.BLACK);
} catch (ActivityNotFoundException e) {
Toast.makeText(
getApplicationContext(),
"Error in your phone call"
+ e.getMessage(),
Toast.LENGTH_LONG).show();
}
section(path_name, lv1);
}
break;
case MotionEvent.ACTION_UP:
endX = (int) event.getX();
endY = (int) event.getY();
}
return true;
}
});
float hX = Float.NaN, historicY = Float.NaN;
static final TRIGGER_DELTA = 50; // Number of pixels to travel till trigger
#Override public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
hX = e.getX();
historicY = e.getY();
break;
case MotionEvent.ACTION_UP:
if (e.getX() - hX > -TRIGGER_DELTA) {
onSlideComplete(Direction.LEFT);
return true;
}
else if (e.getX() - hX > TRIGGER_DELTA) {
onSlideComplete(Direction.RIGHT);
return true;
} break;
default:
return super.onTouchEvent(e);
}
}
enum Direction {
LEFT, RIGHT;
}
interface OnSlideCompleteListener {
void onSlideComplete(Direction dir);
}

Categories

Resources