I have a list that uses a database to display some data. There are several operations that the user can perform on each list item and instead of implementing a context menu where the user uses a long press to bring up a list of possible operations I would like to add buttons to each item so the user can just tap the button and perform the operation. The list can be potentially large and attaching a listener to each button for each list item is overkill so I would like to do what Javascript programmers do with event bubbling, i.e. attach a single handler to a top level element like the entire list and let the click events bubble up to it. How would I go about doing this?
View.OnClickListener.onClick() does not bubble, so the solution you propose would not work.
OTOH, View.OnTouchListener.onTouch() does bubble so this could possibly be used, but it would require you to manually handle MotionEvent's down/up to detect a click.
Besides, if you create a lot of Buttons, than are you sure that adding onCLick handlers would be a lot of overhead, especially since you can register the same method for all of them.
What you are trying to do sounds like a premature optimization. Be sure there is real overhead affecting your users, before you try to deal with it.
Related
This is driving me crazy. Something that is implemented by default in ListView is missing from RecyclerView.
I want to have the exact behavior of a listview which is set to single choice mode. I tried the solution from this question but it doesn't fully solve the problem. When I touch the list with 2-3 fingers etc it may only highlight one row indeed, but if I lift my fingers rapidly everything gets triggered even tho only one row is highlighted each time. (I can also hear the sound effect from my phone that gets repeated 3 times very fast)
Basically I want to disable multi touch events from the phone so that the list is forced to select only 1 item each time no matter how many fingers the user uses
To disable multi touch in recyclerview, you can use
android:splitMotionEvents="false" in your recyclerview tag in layout file.
By that attribute, you will not receive multi touch in recyclerview.
The problem is that i start a new thread every time user clicks on a row so if i try to multi touch the recycler's items, even in your sample app more than one threads start simultaneously and unexpected results happen.
That has nothing to do with multi-touch. You would get the same effect with one finger, rapidly tapping on different rows. It may be easier for you to reproduce the effect via multi-touch, but there is no guarantee that the user will never tap a second row while the thread for your first row is still outstanding.
Your problem is with your implementation. Either:
Tapping on multiple rows in rapid succession is fine, and you need to fix the "unexpected results", such as serializing the threads, having a single thread and a LinkedBlockingQueue, or synchronizing access to shared data, or
Tapping on multiple rows in rapid succession is not fine, in which case you would need to track whether a thread is outstanding when the user taps on a row, then decide what to do (discard the event? queue the work to be done, so it is performed after the current thread is done? something else?).
Another possibility would be to get rid of the "fork a thread on a list row click" entirely, and require a more positive step (e.g., click some Done button) before you do whatever work it is that you are trying to do.
Are there any best practices on using the new RecyclerView animations together with a SQLite database?
In particular, I'm thinking about a pattern that's been around for a while now: sliding a list item off the screen to delete, and giving the user the option to undo.
Like in the Gmail app:
I don't think this is hard. My approach to solving this is in two parts, a custom view that shims around the adapter view, and a scroll listener on the recycler view.
The custom view is to handle the sliding item part. The key part is to mark the associated item for deletion when it is put into the slid state. I also like to allow for a second slide to dismiss the undo option.
The scroll listener on the recycler view simply deletes any marked items when onScrollStateChanged is called, you only need to care about changes away from SCROLL_STATE_IDLE. I prefer my deletion to be more lenient than the gmail implementation, so I post a delayed message on the scroll event rather than deleting straight away. You have to remember to cancel it if undo is pressed.
Oh, you also have to do any deletions if the screen is navigated away from.
You need to implements SwipeDismiss, use this library
SwipeDismissRecyclerViewTouchListener.java
On dismis you need implements a custom view or use visibility GONE from xml.
I have a ListView with some products names. What I have to do is to change the layout of one row when the user performs double-tap on that row, with another layout which has other data regarding the product. Is this possible? If yes, how can I do this?
Thanks in advance
I dont if I have understood clearly what you need but it is possible.
1. Make a custom listview (the listview must have all the components needed when double-tapped)
2. At start hide all the components except the textview which has the products name.
3. On double tap show the components of the tap row (visible = true)
Note: As for double tap it is another matter. You should consider Long Press instead.
See this question for a similar setup How to detect a double touch/taps on an Android ListView?
There an answer points to http://developer.android.com/guide/topics/ui/ui-events.html
If you really, really want to handle double-click you'll have to handle the OnClick() event and count the first click as part of the double click.
Register a delayed handler for the single click action (if there should be one).
When you next receive an OnClick() event then calculate the time between that click and the previous one. If it is below some arbitrary threshold then you count it as a double click. Cancel the delayed SingleClick handler and run your DoubleClick Handler.
But it'll be brittle; could confuse the user and will be more difficult to build and maintain.
So, in short, use OnLongClick() instead of OnClick(). The Twitter app is a good example of the use of this UX.
However, once you've decided how to capture the event take a look here for a blog post on replacing individual items in a ListView
I search for a good and handy user interface to put items from list 1
to a empty list 2 with drag&drop. I need this function for Android 2.2.
Have anybody heard about this feature?
EDIT
I found this website and i think it is also interesting.
http://techdroid.kbeanie.com/2010/04/simple-drag-n-drop-on-android.html
Look also at the comments.
It may also be helpful to reference my simple Drag and Drop list. You can find here
if u don't getting then use this
An example of this in github https://github.com/mtparet/Drag-And-Drop-Android
It could help you.All contribution are welcome
Take a look at this sample that Drag and drop inside a List View..
Refer to the drag and drop API launched in API Version 11
http://developer.android.com/guide/topics/ui/drag-drop.html
A drag and drop operation starts when the user makes some gesture that you recognize as a signal to start dragging data. In response, your application tells the system that the drag is starting. The system calls back to your application to get a representation of the data being dragged. As the user's finger moves this representation (a "drag shadow") over the current layout, the system sends drag events to the drag event listener objects and drag event callback methods associated with the View objects in the layout. Once the user releases the drag shadow, the system ends the drag operation.
(FYI, I'm targeting 2.2 Froyo in my project, so any solution would have to work at that build level.)
What I want to do seems quite simple; show a list of items in a ListView, and allow the user to tap to select multiple items before performing an operation on all of them at once.
To provide a little more detail, I am binding a ListView to an array of objects. The screen consists of other controls, with the ListView in the middle. Each list item has several components; two images, and a text label. NO checkbox. Instead, when the user taps an item, the background should change to indicate that it is checked. If the item is tapped again, the background should change back to indicate it is not checked. The user may tap one or more items. If the user scrolls the list off the screen and scrolls it back, the state of those items should be preserved.
From what I've researched, I gather that I need to:
use a Drawable as the background for the list items with selectors for checked, pressed and default states
create a custom class extending LinearLayout or RelativeLayout and implementing Checkable, then use this as the root View for the list item layout
I've found several tutorials online, but none work. Either they have runtime errors, or simple don't do anything... pressing the buttons does not change their appearance to checked.
I found an alternative approach in the O'Reilly "Android Cookbook." Rather than doing all of the above, they add a boolean to the objects the ListView is bound to, then manually add code to change that boolean when a list item is clicked and to change the background in the adapter for an item where the boolean is true. In other words, they don't use Checkable at all. This does not seem like an ideal solution to me; abandoning the Android API in favor of custom hacks often seems to cause bugs later on, and I'm uncomfortable with adding GUI information (whether or not an item is selected) to what should be a purely data-carrying POJO conceptually representing a chunk of information.
My question is this: does anyone have a WORKING tutorial to accomplish what I have described, using Android's Checkable functionality? Or is this so problematic that something like the O'Reilly hack always has to be used?
I have never read Android Cookbook but their strategy is exactly what I would do. I would decline to call this a hack and suggest this is the kind of thing that the Android framework intends to do. A selected state in my opinion is part of the model you wish to protect, I do see how it could be in a gray area as it removes the purity of your POJO's.
There is a second strategy you could use to protect the purity of your POJO's use the state of the background to find if something is selected or not. Additionally you could also use a plain color resource instead of a drawable background.