Android ListView onTouchEvent doesn't give ACTION_DOWN - android

In order to have list reordering functionality I turned to this implementation.
My problem is that when I try to drag an item in my ListView, I don't get the ACTION_DOWN event. Instead, for a single smear down motion I get 2 ACTION_MOVE events (action=0x00000002) and a single ACTION_UP event (action=0x00000001) in this order.
I've looked at similar questions but it seems like everyone has the opposite problem, getting only ACTION_DOWN events.
Can anyone think of why this is happening?
Thanks,
Yoel

I was using the same code.
My problem was also that something was consuming the event and I didn't managed to found what was it... but i managed to solve it using onInterceptTouchEvent to return true on the events i needed on onTouchEvent.
Problem solved :-)

It turns out I needed to add this small piece of code:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev);
}
Now I get the ACTION_DOWN events in the OnTouchEvent function and it all works fine.

Related

What is the best way to use startSwipe function of ItemTouchHelper

I created a custom LayoutManager and TouchHelper.Callback to have a tinder-like card stack. I want my layout manager to allow only the first item to have a touch event. Firstly I override the isItemViewSwipeEnabled() of ItemTouchHelper.Callback to disable touch event for each child. As the last step, I need to allow the first item to have a touch event only. However, I am failed to do so. Is anybody know how to overcome it?
StackLayoutManager to create child views.
StackTouchHelperCallback to disallow default touch events.
StackView a custom RecyclerView.
This is the custom view hierarchy I developed. If anybody developed some kind of views, can them help me? Thanks in advance.
You can override the getMovementFlags method and return 0 in case you dont want to allow any movement, otherwise return appropriate direction flags.
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder:RecyclerView.ViewHolder): Int {
return if(viewHolder.adapterPosition == 0) /* return appropriate direction flags */ else 0
}

onChartFling never called in MPAndroidChart

I have problem that onChartFling method is never called in my LineChart or BarChart. All other methods from OnChartGestureListener are called correctly, but these not. I put a breakpoint alo int BarLineChartTouchListener. Not called also.
Or is they any other way how to detect end of scrollin on fling ?
onChartTranslate is called multiple times, is it possible to find out if onChartTranslate was called when fling stopped ?
The author says, the way to solve this is to modify the library:
"The reason therefore is, that I do not want to permanently recognize a fling-gesture or single tap when the chart is zoomed in and panned / dragged.
Therefore, gestures do not work in zoomed mode.
If you want to disable this, you will have to modify the library."
These lines should be modified:
if(mTouchMode == NONE) { // remove the if
mGestureDetector.onTouchEvent(event);
}
Code for changing is here: https://github.com/PhilJay/MPAndroidChart/issues/405

How does one disable/ re-enable touch events in Android?

Quick, probably simple, question. I have a view whose background is animating, and during that time, I want to disable the user from interacting with the view. My view is a FrameLayout, and I'm capturing touch events with onTouchEvent(). Solutions I've tried:
1) First setOnTouchListener(null), then setOnTouchListener(this). Problem is, my view only calls onTouchEvent(MotionEvent), and not onTouch(View, MotionEvent), so I can't pump through the Events there.
2) First setEnabled(false), then setEnabled(true). The source code says: A disabled view that is clickable still consumes the touch events, it just doesn't respond to them. Problem is, MotionEvents still get pumped through in onTouchEvent().
3) requestDisallowInterceptTouchEvent(false), then (true). This only handles touch events from the parent.
The solution I have working is using a boolean variable isAnimating, and checking the value of that in onTouchEvent(). I'd rather not do this, because it looks ugly to me and I'd rather use the API for it than reinvent the wheel, sooo... anyone got any ideas? Thanks.
Try:
setFocusable() and setFocusableInTouchMode()
Try:
#Override
public boolean onTouchEvent(MotionEvent event) {
//Check if event is on your view
if(event.getY()>view.getTop()&&event.getY()<view.getBottom()&&event.getX()>view.getLeft()&&event.getX()<view.getRight()){
//event occured inside your view
}
//here return super or as your logic prefer
return super.onTouchEvent(event);
}

Android Motion Event Hold Down Button

I have a seemingly simple question that I need help with. I have a button. I want it so when I click on the button quickly it adds one to a total. I also want it to be so that when I hold down this same button for about 2 seconds, it removes one from the total. The only part I am having trouble with is the motion event part. I have been experimenting with ACTION_UP and ACTION_DOWN with no luck. Is there an easy way to do this?
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
total ++;
return true;
}
case MotionEvent.ACTION_UP:
{
if(total >0){
total--;
}
return true;
}
}
Thanks for the help!
Why not use the Button's setOnClickListener and setOnLongClickListener?
The only limitation with this approach is that you cannot set the the timeout value, which is somewhat over 1,5 seconds, if I remember correctly.
If you're persistent about the two seconds (or some other value), then I suppose you could use an onTouchListener and keep track of the pressed time yourself. In that case, the MotionEvent's getDownTime might be of interest to you.

apply general onkeylistener

New to programming, now to android. So I hope I dont annoy you to much.
How would I go about setting an onkeylistener at the top level of the app that captured the keyevent no matter what.
Basically what i have is a linear layout with dynamically added edittexts.
I want to capture the Enter key event and have it get the current edittext, perform some tests then create a new edittext and add it to the layout.
I know I can (and have) implement an onkeylistener to individual child views, but not being a programmer, the logic seems weird to create an edittext that listens for input to create another edittext that listens for input to create another.... (you see where this goes)
Can anyone point me in the right direction?
I have lots more info about what Im trying to do, I just dont know what is pertinent and what is not, so let me know if you need more.
Thanks for your time in advance,
Chris
Take a look at http://developer.android.com/reference/android/app/Activity.html#dispatchKeyEvent%28android.view.KeyEvent%29
What you want is to intercept all the events before they are processed by any View in the window. Return true if the event was handled or false if you want the childs to process the event further.
Like Ben said your activity can implement OnKeyListener then for each EditText you create, set the OnKeyListener to be the activity.
editText1.setOnKeyListener(this);
And then in your implementation of onKey you can handle the key press event.
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(v == editText1) {
// do something
} else if( v == editText2 ) {
// do something
}
return true; // return true if you handled the keypress
}
Your activity can implement OnKeyListener.

Categories

Resources