Listview with onTouchListener - android

I have a listView. In that listView i have implemented onTouchListener. Every things work fine, but when list is Scrolling , on tapping on the list , the list should stop(like normal android listView).No action can be done while scrolling like stopping the scroll.This happen because of the onTouchListener. I am returning false from onTouchListener. I don't know why this behavior happens?Can any one help.

try returning super.onTouchListener, or true, when you don't want to consume the event (i'm thinking you implemented gestures). returning false means you're discarding the TouchEvent, and you don't want to do that if you want to stop the scroll.

Related

android: click event that's not being consumed

I need to create a click event, that's not being consumed, so that an underlying adapter receives the touch event.
I cant use an OnClickListener, because that would consume the event.
I tried my best with OnTouchListener, but if I return true the event gets consumed and if I return false the listener only registers ACTION_DOWN events.
How can I react on a click, but if its not a click the adapter will receive the move?
EDIT:
I'm using a lorentzos.swipecards SwipeFlingAdapterView, which uses touch-move-events. I put a layout over half of the view of the items of the adapter, which need to react on click events. But I either can't implement the click event, since I can't catch two events in a row without consuming them, or I can't have the SwipeFlingAdapterView react on the touch-move-event, since I, well, consumed it in the click event.
Don't know what sourcecode to share, since I tried many different ways and nothing works.

capturing touch events without consuming them

I've got a big headache and spent a few hours to solve my problem withou any reasonable results.
I use a custom adapter (extending ArrayAdapter) to show listview items. This adapter implements OnTouchListener. There's also a background selector which color is changing when an item is touched and OnItemClickListener in ListViewActivity.
What I need is capture touch events (all of them, not only ACTION_DOWN) in the adapter. When onTouch returns false, the consecutive events (ACTION_CANCEL, ACTION_UP etc.) aren't captured. On the other hand returning true stops dispatching all other events (e.g. click) so that the ListViewActivity's onItemClick is never triggered.
I tried hard to find any working solution in SO and other resources, but with no success.
One idea was not worry about click events and background. I may set the background programatically and trigger click event the same way when the onTouch action is ACTION_UP, but view.performClick() does nothing (view is the 1st argument of onTouch method).
[EDIT]
To make it clear, I want to handle touch events in the adapter beacuse I need the textview in the item to marquee when the user touches it. Thus, in onTouch, when the action equals ACTION_DOWN, I assign true to setSelected property of the textview and, consequently, false when ACTION_CANCEL or ACTION_UP.
Any suggestions?
Thanks in advance.

Is there a way to enter touchmode programatically?

So I have an interesting problem. I have a ListView that I want to maintain the exact row selection and yoffset on an orientation change. As such I call:
list.postDelayed(new Runnable() {
#Override
public void run() {
list.requestFocusFromTouch();
list.setSelectionFromTop(finalPosition, yoffset);
}
},100);
Seems pretty straightforward and it works well. The problem is that when I change the orientation twice (get back to the original orientation, ie landscape->portrait->landscape) than the list has exited TouchMode, ie list.isInTouchMode() returns false (On the first orientation change, list.isInTouchMode() returns true and everything is great, clicking works etc). As such when you scroll the listview programatically using setSelectionFromTop() and click on an item in the list without touching somewhere else in the app first, the list jumps and scrolls to where I clicked, highlighting the wrong item. It's very annoying and I can't seem to find a fix for it. Any ideas and suggestions are most welcome.
Calling list.requestFocusFromTouch() seems to have no effect on setting TouchMode.
More info:
Android:To set an item as selected when the ListView opens?
I found a post from Romain Guy "Whenever you touch the screen, anywhere in the phone, you enter touch
mode for all of the applications. This is the desired and expected
behavior. " http://groups.google.com/group/android-developers/browse_thread/thread/590b02939d14675b/077ad77e4217e56e?lnk=gst&q=[android]+scroll+listview+not+in+touchmode#077ad77e4217e56e
All i want to do is somehow get into TouchMode without forcing the user to actually touch the device on the second orientation change. I have tried list.dispatchTouchEvent() multiple times. Its possible I am sending the wrong motion events:
list.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN ,
list.getWidth(), list.getHeight(), 0));
list.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE ,
list.getWidth(), list.getHeight(), 0));
list.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP ,
list.getWidth(), list.getHeight(), 0));
Another option would be to figure out why we are no longer in TouchMode. I have not overriden onConfigurationChanged() so the layout is completely rebuilt on each orientation change.
Thanks in advance!
Jared
Try using setItemChecked(...) method of the Listview. I've seen that used a fair bit when the Listview is set to CHOICE_MODE_SINGLE, as that mode really makes each listview item a checkable object.
Also look at the following QA where someone was trying to do something similar. It appears that the easiest solution was to create a different background for 'checked' items.
Android listview array adapter selected

how to differentiate between programmatically scrolling list view and manually doing so?

I need to differentiate between programmatically scrolling a list view and manually scrolling a list view. I am not quite sure how to do it. Any idea ?
Thanks.
I guess a way would be to do something like add onscrollchanged listener and onTouch listener to the listview. And keep a boolean like fromUser.
In the onTouch listener you can set the flag to true on MotionEvent.ActionDown. When the MotionEvent is ActionUp you can set the flag to false.
So whenever the scroll listener is fired you can check the flag and see whether its from the user or not.

Android ArrayAdapter tell all other views to refresh themselves

I need all of the other views in my arrayadapter to check their attributes and return to a default attribute at certain times
use case: listview items (a)(b)(c)(d) , when you touch (a) it's background turns black. This is done in the ontouchListener. But when you touch view (b) it's background turns black BUT (a)'s needs to turn back to the default which is not black
I dabbled around with recalling the arrayadapter at the end of the view.OnClickListener but the problem with this is that it also resets the scroll position of the view, so if this list was much longer - which it is - and user touched item (r) , then it would reset the list and put the user back at item (a) at the top
I was looking at notifydatasetchangedbut I am not sure how to use that and where it should work
insight appreciated
notifyDataSetChanged will do the job. You can call that at any time in your code and this will trigger the adapter to call getView for the visible list items again and so update their content. So basically the only thing you have to do is to update the list item states or information before calling that method.

Categories

Resources