Is there any way to dispatch touch event only to specific child in custom view group with stacked children.
For as long as you have reference on both, the child you want to receive the event and the parent, you can implement the parent to pass the touch to the child you wish to receive it.
can you post the code of the View?
So you can make something like:
#Override
public boolean onTouchEvent(Event e){
childToGetTheEvent.onTouchEvent(e);
}
Or is there any reason you cant do something like this?
Related
You can, of course, assign onclick to all child views through a loop, but is there such an opportunity to hang the click event on the parent element, and then receive the event on the desired child view, as, for example, in html/js through the event target?
You can setup an onClickListener() for the parent and then inside that you can delegate the click using child.performClick(). if you cant to know more, you can reference https://developer.android.com/reference/android/view/View.html#performClick()
The code would look something like this:
parent.setOnClickListener {
child.performClick()
}
I am trying to create an item in a ListView that has multiple options; view and edit. I would like to create it in exactly the same way as android's contact system - see below:
I have added the red boxes to illustrate the behaviour I want. If you press within the left red-box, you call the contact. If you press within the right red-box, you send a text message to the contact. I have already created a similar layout in XML, but I am having trouble implementing this functionality in code.
I have tried to create custom android:onClick function calls for the separate layouts within the item, but calling an onClick method only allows you to pass in the View as a parameter, but not the position. Needing the position to use listview.getItemAtPosition function, I tried to use listview.getPositionForView to return the position but found this was extremely unstable and was very easy to return incorrect positioning due to recycling of views.
What is the best way of implementing a list populated by items with multiple buttons/layouts on each item?
One way to accomplish this while still using a single android:onClick function would be to utilize the setTag() method for each of the clickable views to store an Integer that is the position for the item. For example in your adapter's getView():
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
...
ImageView textMessageView = (ImageView)convertView.findViewById(R.id.whatever);
textMessageView.setTag(new Integer(position));
...
}
Then in your onClick() method you could do something like the following:
#Override
public void onClick(View clickedView) {
int position = clickedView.getTag() instanceof Integer ? ((Integer)clickedView.getTag()).intValue() : -1;
...
}
Don't know that it's ideal, but it allows you to use the XML android:onClick facility and doesn't involve creating separate onClick listeners for each list view item.
I also worked on a similar kind of project.
For doing it, i created my own layout having two linear layouts as linearLayoutLeft and linearLayoutRight.
I then added this layout to a listView.
And then i mentioned the onClickListener of the Listview, i used to getThe view being clicked at the position in the listView.
So for each row in listView, i had two linearLayouts and they have their own onClickListeners.
can we have ListView inside ExpandableListView so that I can have group level first and then array of child (listView). i already finish from listview classes and it works perfectly!
but how to call each group in expandable list view to display list view (layout)! and get the correct child !
please can any one help !
thanks.
Actually you can put a scrollable view (e.g. your ListView) inside another scrollable view (e.g. your ExpandableListView) and make the former scrollable in the following way:
listView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// disallow the onTouch for your scrollable parent view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Yes you can! But the real thing is to create a listView in a expandableView.
Here is the trick: https://www.captechconsulting.com/blogs/android-expandablelistview-magic
Please take a look at this question which was asked two days ago. The user there was trying to put a custom view, derived from ListView into ExpandableListView. My answer to that question applies to your case as well, so I'll quote it:
You can't do that because you can't put a scrollable view(ListView)
into another scrollable view(ExpandableListView). The reason is that
the parent will consume all the touch events and they will never reach
the child. ExpandableListView will scroll, but the ListView will never
know that scrolling took place.
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.
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!