messing with touch / click events - android

This doesn't seem like a typical view layout which I'm having trouble with.
I have two listViews.
Each listView has touchListener
(whose purpose is to synchronize scrolling by calling dispatchTouchEvent() to another listView)
ListView also has onItemClickListener to handle clicks on the row of listView.
Everything works as intended up to here.
I'd like to add another clickListener to subview-group of the listView's row to handle click event on the subview.
After attaching this clickListener, I see listView's scroll doesn't always work.
I suspect its because the clickListener of this child view is inspecting touch events (to see if its indeed a click) before the parent(listView)'s touchListener.
I can think of two workarounds to this problem.
attach touchListener instead of clickListener to child, and make it return false for all touch event except FINGER_UP event.
on FINGER_UP event, I execute the method which I initially had in onClickListener
public boolean onInterceptTouchEvent (MotionEvent ev)
Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
(ok... I'm confused, I thought touch events goes to child views first and propagate to parents if children don't handle the touches..)
.. How do I implement the method 1?
.. Please help me to figure out #2 as well and to grasp the touch delivery mechanism.
EDIT -
This is where I add OnClickListener to my subview.
viewHolder.layout_author.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent profileIntent = new Intent(ImageListAdapter.this.activity, ProfileActivity.class);
profileIntent.putExtra("JsonUser", jsonAlbumImage.jsonUser);
ImageListAdapter.this.activity.startActivity(profileIntent);
}
});

I'd like to add another clickListener to subview-group of the listView's row to handle click event on the subview.
You simply need to write a custom adapter and assign the OnClickListener in getView(). You can look at the question: Android GridView Button Click Handler for example code.
Also awhile back I answered Android. Scrolling 2 listviews together did you use a similar approach to synchronize your ListViews? When I combine both of the answers, my app functions the way you want. I hope this helps.

Related

Handling multiple clickable areas in listview

I'm using a ListView to show some images and names in my application. I can make the ListView tiles clickable and start new activities as well . But now i want to do different actions for the different items that are on one listview tile. If user clicks on image, image opens and the textView will show some toast or something. The ImageView and TextView are on the same ListView Tile. How can i achieve this? Any Help?
Define OnClickListener events in your adapter for the button and textview as required and you are done. They will precede the click event for the list item by default.
if the contained views in your custom row layout does not have a click event it will fall back to the click event of the list view item.
You can implement click event in adapter, but you also implement it in activity.
You can find solution in :http://www.migapro.com/click-events-listview-gridview/
hope it can help you.
Some note: should not use view holder in gridview like above tuturial, it can make some bug, can implement direct like this:
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0); // Let the event be handled in onItemClick()
}
});
Another way to implement this elegantly is to add secondary views called 'accessories' to the item views associated with your ListView. This is explaned quite well in the link below by Cyril Mottier: A Google Developer Expert on the Android platform:
http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/

Prevent WebView from grabbing onClickListener

I know this got asked often, but all solutions(, I found,) don't work for me.
What I have is a CardView with an OnClickListener making a Toast (#toast1).
Inside the CardView there are multiple views as also a WebView.
As mentioned elsewhere, to pass through the click through the WebView to the CardView I have done following:
Set android:clickable="false" in WebView XML
insert following under CardView.setOnClickListener(...)
WebView.setOnTouchListener( (view, event) -> true);
I also replaced the lambda with an anonymous method, to see if its just this. No change.
What happens now is:
At the border and over the other views, the clickListener is triggered and the toast appears
Over the webView the clickListener isn't triggered.
Also put a toast (#toast2) in touchLstener of WebView before returning true, and it gets triggered.
What I expect:
Click will passed through WebView
With #toast2 added: First show #toast2 then #toast1
What is a bit confusing, that in documentation of OnTouchListener, the return is following:
True if the listener has consumed the event, false otherwise.
For me that means:
true: Don't pass click to below views, as listener consumed it
false: Pass click to below views, as listener didn't consumed it
But setting to false didn't change anything.
First of all I would suggest you to get familiar with android touch handling system - you can find a really good description in this answer. To sum it up: touch event propagation starts on top level of hierarchy, but actual handling of the touch event starts on the lowest level of view hierarchy. As for solution of your problem I may suggest to sublcass the parent of your WebView and override onInterceptTouchEvent in the following way:
#Override
public boolean onInterceptTouchEvent(MotionEvent e) {
return true;
}
This will instruct this parent view to intercept all touch events that would otherwise go to its children views, thus limiting the first level of touch processing to this view.

ExpandableListView - OnChildTouchListener

I'm trying to create a drag-drop interface for a ExpandableListView.
I wanted to know if there is a function similar to OnChildClickListener for OnTouch?
If not, what would be the best way of listening for a touch event on a child inside the expandable list?
I realized we could add an OnTouchListener() in getChildView(). That would listen for a touch event every time the user touches the child item.

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.

Items in ListView not long clickable after setting click listener in getView()

I've searched around and have not come out with a solution (maybe not using the correct keywords).
So, I've a custom ListView which its item can be dragged around when the item is long clicked. Within its item, there's an ImageView and LinearLayout containing two TextViews. Actions are done when the LinearLayout or ImageView is clicked.
To do this, I've use setOnItemLongClickListener on my DragListView which extends ListView, to initiate drag action, and onInterceptTouchEvent to manage the drag action.
Then, I have built a custom adapter extending BaseAdapter and overrided its getView() to implement the child items in the row. The LinearLayout and ImageView have been setOnClickListener.
The problem is, the LinearLayout and ImageView are able to do their stuff, but the onItemLongClick isn't called.
The listener inside getView();
holder.delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
For item long click (drag initiator)
setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
//Do something
}
Thank you very much!
I think that a gesture detector is one of ways to handle events.
Usually, however, a gesture detector is used when we want to detect a gesture not a long-press.
The reason why onItemLongClick isn't called is that onClickListener might consume a touch event.
In that reason, if you want to handle onItemLongClick, intercept touch event and dispatch it to views you want to handle.
You can find more details following link.
http://developer.android.com/guide/topics/ui/ui-events.html
Ok, just found out the solution myself.
Instead of using onItemLongClickListener, I create a gesture detector to detect for long press. Then I override dispatchTouchEvent and force to scan for long press first, then return super.dispatchTouchEvent and the other following touch events.
Suggestions are still welcomed!

Categories

Resources