Android ListView Change one Row on double tap - android

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

Related

Edit button in listview item depending on other listview items

I would like to start by saying if you can think of a better title for this problem, feel free to change it since I have no clue how to explain this in a short way.
So here is my problem:
For the application I am trying to make I have these schedules, one schedule for today, and one for upcoming days. Both of these are a listview inside a fragment.
(I used those fragments to create tabs to seperate the two.)
Each game (let's call them games because calling them activities would be confusing) on the schedule has a status, and here is where the annoying part comes. I have been asked to check if any game has been started, and if so I need to disable the buttons to start any other game than the one that is already ongoing.
EDIT: The main problem is that I cannot use findViewById on the listview item because apparently it is a null object reference
I made a little "paint"ing to give you more of a graphical representation.
So long story short, I need a way to check the status inside of every listview item inside of the listview inside of the fragment inside the activity to use said status to disable every button except for the one of the ongoing game.
Little side note: If no games have been started yet, all buttons are enabled.
I hope this made sense.
If you want some code to go with this, feel free to ask but be warned, I am making this inside a testing app so a lot of useless testing data and sloppy code.
EDIT:
This is where I am stuck in a more clear example:
The start buttons are enabled but should be disabled.
Scrolling further down the list, there is a started 'game' and right below it, a game with the same status as in the previous picture where the button is disabled as it should be.
This is because the "isStartable" boolean in my code goes to false after the game with status "start" has passed and the following items are disabled.
When I scroll back up, it is how it should be, the items are all disabled but I need them to be like this when the listview gets filled. Any way to refresh the data in this listview and taking the "isStartable" boolean with it?
This is what it looks like after I scroll back up.
create a model class for your listview data items. Set a boolean variable in model class like isGameStarted. Set that variable as per your result.Then in your listview adapter, put a condition as below
if(isGameStarted){
holder.startButton.setEnable(true);
else
holder.startButton.setEnable(false);

Access Adapter items of Recyclerview

I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.

Click button and that button waits for second button on activity to be pushed. Game board. How to accomplish?

I wasn't exactly sure how to briefly paraphrase my question in the title, so please forgive me as I never post questions until now. I am new to Android/Java for starters, as the main language I have used so far is C++. My question is that I have a game board layout (similar to checkers/chess). When it is the user’s turn, they are to click the piece they want to move on the board and then the blank location they would like to move it to. How can this be accomplished? Up until this point I have implemented onClickListeners that never rely on another button in the activity to be clicked afterwards and wait for the user to do so.
Brief information on my project currently (unsure if needed):
I am currently using an ImageButtons array (of size 36) and a two dimensional integer array to hold the information of each of the buttons, as they are displayed in a GridLayout in a 6 X 6 fashion. In my MainActivity class I have implemented the OnClickListener and created a switch statement in onClick() for each of the button ids.
I am not sure how much more information is needed on my code for help or if it is completly irrelevant. I tried looking on the internet before choosing to ask finding nothing. It is always possible though I was not correctly phrasing my issue. Thank you to everyone in advance!!! :)
Without any code it is hard to say what you are doing wrong/right. But if you have the images stored in an Array then when they click on one image that can be put into a variable which is used to place on the View that they click on next. If you have a more precise question then please post relevant code and error messages from what you have tried, if any. Hope this helps
after a user click's a piece store that info in a member variable like a current piece and then on second click check if the previous is not null, the current clicked space is empty and start your animation by posting to a runnable implementation. this is the simple logic part. where are you having problem exactly
To optimize your code to change images what you can do is custom animations on the imageview. You can change the position of the image with animation without having to create or store an image. (If you use images bitmap a lot you have a very good chance of running into OutOfMemory exceptions and this will happen on top end phones very easily
How to Move a Button and Change the Button Image
I would put a single Button on every GridView position.
Then, I would use button.setBackground() and button.setVisibility() to display and hide the buttons with various images. In this way, the buttons will appear to move, but you are really just displaying a different button.
I recommend this because it is easier to change the visibility and image properties of a button than it is to actually move the button, although both methods are possible.
You will maintain a 1 dimensional array of images[n] and a 2 dimensional array of buttons[6,6].
As an example, suppose you want to move image[5] from grid position (1,2) to position (3,4):
// Hide the button at (1,2);
button[1,2].setVisibility(Button.INVISIBLE);
// Display the button at (3,4) with image #5.
button[3,4].setBackground(image[5]);
button[3,4].setVisibility(Button.VISIBLE);
Additionally, if your button images are stored in your resources, you could efficiently use button.setBackgroundResource(R.drawable.image-5-id);
The instructions above discuss how to move a Button, but now how to trigger one Button to be moved and then trigger the location into which to move the Button.
To accomplish this, you will have to define two states, such as:
private final static int STATE_PICK_BUTTON = 0;
private final static int STATE_PICK_LOCATION = 1;
private int state;
Initialize state = STATE_PICK_BUTTON;
When the system is in the first state STATE_PICK_BUTTON, all button
presses identified in your onClick() function memorize a grid
position to move from, and in some cases transition the system into
the second state: STATE_PICK_LOCATION.
When the system is in the second state STATE_PICK_LOCATION, all
button presses actually move the button from the memorized grid
position to the grid position of this button press.
Of course, you will have to do all sorts of error checking to make
sure you are allowed to move a button before triggering the state
transition.
Finally, the above suggestion may not work, because it may be impossible to click an invisible button. If this is the case, instead of changing the visibility of Buttons in empty grid locations, leave all the buttons Button.VISIBLE and use a fully transparent Button image for the buttons representing empty grid spaces.

Read, display and update Android table values?

This is more of a question about getting my head around the process I need to follow really.
I short:
I need to read the exisiting table (a rugby squad) and display it on screen.
The user has the option of tapping any entry and updating it - and repeating as necessary.
The user can then tap "Accept" or "Cancel"
If "Accept" is tapped I obviously want to update the table with the new values.
The squad can be from 10 to 22 players, so I need the display to be fairly dynamic and ideally I need a shirt number (static), a name (text, updateable) and a "starting" checkbox (updateable)
I've kind of got my head around each on the indiviual components, but when I try and tie them together it all goes pear-shaped!
Can anyone point me in the right direction, please?
It sounds to me like a simple datbabase backing a ListView would be the way to go. That way you can offer different options for sorting the list, etc.
EDIT
In your main xml have the listview and two buttons at the bottom (accept and cancel).
Create a Listview rowlayout.xml that contains a textview, an edittext and a checkbox.
Populate them like normal (you can use setText() on an EditText just like a TextView). Put the shirt number in the textview and the player name in the edittext and the starting state to set the checkbox.
Then you simply wire up the accept button to cycle through the list and commit any changes you made and the cancel button to reset them all to the last saved values.

Android list tap event delegation

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.

Categories

Resources