ViewPager control touch event - android

I have got viewpager.
At the my viewpagers first item i used lock pattern view (from google code) my problem is when i try to use lockview my view pager start to swipe how can i fix my problem.
I tried lots of thing but I cant do it I hope anyone help me
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/viewPager" />

Add this to your view:
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
pager.requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
pager.requestDisallowInterceptTouchEvent(false);
break;
}
}
requestDisallowInterceptTouchEvent:
Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).

Related

Android multitouch View.OnTouchListener

Im trying to play around with some multitouch in Android.
I have two RelativeLayout on screen where I would like to get touch events.
fooLayout = (RelativeLayout)findViewById(R.id.foo);
fooLayout.setOnTouchListener(onTouchListener);
barLayout = (RelativeLayout)findViewById(R.id.bar);
barLayout.setOnTouchListener(onTouchListener);
...
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
...
I tried having a OnTouchListener for each layout but that didn't seem to work at all.
I figured since the onTouch method gets an View I would be able to make out with view the current touch event came from and save away the pointer id.
int pointerId = event.getPointerId(event.getActionIndex());
int action = event.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
switch (v.getId()) {
case R.id.foo:
fooPointerId = pointerId;
break;
case R.id.bar:
barPointerId = pointerId;
break;
}
This does not work as I thought it would.
When my first finger touches in one of the layouts I will get the correct id from that layout. But when I put down a second finger in the other layout it will get the id of the layout where I put down my first finger.
Am I going about this the wrong way?
Is this possible to do or do I have to put the OnTouchListener on the top view and figure out which layout Im touching by the x/y values of the event?
I think you should extend relative layout and implement onTouchEvent(MotionEvent ev) over there to figure out which pointers are touching which child views(Put foo and bar inside your main relative layout/view group that implements onTouchEvent).
IMO onTouch(View v, MotionEvent ev) only lets you register one pointer at a time so it may not be possible using this approach. This link might help out

Android - OnTouchListener not firing if already pressing screen

I have a RelativeLayout with a single ImageView inside of it. The ImageView has an OnTouchListener attached to it which is working perfectly fine if I just press the ImageView. The problem I'm having though is that if I press and hold in the empty space outside of the ImageView and then try to press the ImageView the touch listener doesn't fire. It seems like the layout is absorbing the touch events somehow. Any ideas on how I could fix this?
The action defined by you here is a Multi-touch gesture. Go through this doc to know how to handle this type of actions.
Also take a look at the ACTION_POINTER_DOWN action define in MotionEvent. This is the event that will get called when additional fingers come down.
you mout use this code:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//your code
break;
case MotionEvent.ACTION_UP:
//your code
break;
case MotionEvent.ACTION_MOVE:
//your code
break;
default:
//your code
break;
}
return true;
}
});

Disable vertical scroll when user scrolling horizontally in android

I have a vertical custom list and each item of vertical list contains a horizontal list-view. When I scroll horizontally then list also moves little vertically. Which makes it less user friendly. Can I disable vertical scrolling while scrolling horizontally. I am using
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/hlistview"
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_margin="6dp"
android:background="#fff"
android:fitsSystemWindows="true"
/>
for horizontal list view
The idea is to disable the parent listview to intercept touch event. This might work :
HorizontalListView hv = (HorizontalListView)findViewById(R.id.hlistview);
hv.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ListView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ListView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle HorizontalScrollView touch events.
v.onTouchEvent(event);
return true;
}
});
Reference : https://stackoverflow.com/a/22609646/1239966
Best way to do this.
put given code on your horizontal listview onScroll method it work perfact
ViewParent view_parent = getParent();
if (view_parent != null)
{
view_parent.requestDisallowInterceptTouchEvent(true);
}

GridView problems with touchlisteners

I'm trying to create a custom GridView but i'm having troubles with the touch listeners.
What i want to do:
Create a GridView with custom Views.
Longpress on an item so it becomes 'editable'.
Drag the view horizontal or vertical to move it's position in the GridView.
Here's where i'm having trouble:
I'm implementing GestureDetector.OnGestureListener for the longpress functions, because for some reason using the gridview.setOnItemLongClickListener() isn't working when overriding the onTouchEvent() of the GridView itself (Which i need for the dragging part). So everything is fine at this point. Now i only need to know when the longpress is finished. So i though: "Well this shouldn't be hard." I couldn't have be more wrong. I've fiddled around for quite some time with this and it looks like using different touch events isn't helping me :/
When stepping through the onTouchEvent() i noticed that only 1 action is given: MotionEvent.ACTION_DOWN. So what am i doing wrong? i need the MotionEvent.ACTION_UP...
Found the culprit:
i was doing something like this
#Override
public boolean onTouchEvent(MotionEvent event) {
// Give everything to the gesture detector
boolean retValue = gestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE :
onMove();
break;
case MotionEvent.ACTION_UP :
onUp();
break;
case MotionEvent.ACTION_CANCEL:
onCancel();
break;
}
return retValue;
}
i think retValue was always returning false so no other events were triggered.
this fixed the issue:
#Override
public boolean onTouchEvent(MotionEvent event) {
// Give everything to the gesture detector
gestureDetector.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE :
onMove();
break;
case MotionEvent.ACTION_UP :
onUp();
break;
case MotionEvent.ACTION_CANCEL:
onCancel();
break;
}
return true;
}

Android how to tell a finger is down

I understand calling onTouchEvent from views to get the location of the last touch as a motion event. How the heck can I tell that a fingure is down on the screen and has not been lifted and when the finger is lifted?
For instance there is onKeyDown and onKeyUp for use when you are dealing with keyboard input. So how can I find out when on fingureUp happens?
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_MOVE:
// Etc...
}
}
Then just fill in the cases with what you want to happen on these events.
onTouchListener got a parameter which is typed MotionEvent. Where you can getAction() to know if it is UP or DOWN or else.

Categories

Resources