How to traverse a list with up and down buttons? - android

I have implemented one "up" button and one "down" button in my layout.xml which I want to use in order to navigate through a ListView of strings. How can I accomplish this?
I do not want to use the "scroll" property of the ListView nor to use a LinearLayout with vertical orientation within a Scrollview in order to traverse the list. I simply want to move up and down my ListView by pressing the corresponding buttons.
Any help is appreciated!

You could have a variable int i = 0; in your class body.
Then when the activity loads, the item at location 0 would be selected. If your user presses the down button, then do:
i++;
setSelection(i);
And similarly for going back up:
i--;
setSelection(i);
Of course you'd have to add logic to the code for when you're already at selection 0 or at the maximum selection, but that's the gist of how I'd do it.
To note, I've never used ListView as I've not needed to, but the above should work. I got all my information from The Android Documentation

Related

how to show and hide buttons in listView

As shown in the image when I click on an item in the list view. Buttons show up.
I want to hide buttons when I click on different items and show on current clicked item.
How can I make it work?
Need to preserve view into a variable like prevView. and then click on another item.
Check whether prevView is null or not. If not null. then get button id .
e.g. prevView.findViewById();
and Make their visibility.GONE. and also null prevView.
I hope this will help you.
You can achieve this by making a model class.
take variable for particular buttons in Model. after clicking on a different item set true/false tag in the model and show/hide view according to that tag.
You should be using an expandable ListView so that you can get the buttons shown when you tap on the list item and it get closed when you tap on the other item in the list.

how to set focus on a specific index in list view?

I am trying to set focus on a specific index on list view. I am trying a scenario where I have buttons for up and down when I press the button Up I want to set the focus on upper index of the list view and the same goes for the down button press. I have researched a lot on the internet but I have not been able to find any useful resource. Any suggestions to move the focus between the list view nodes/indexes will be really appreciated.
If there is no any selection on listview for first time you can set selected position as 0, on down button press you can increase position by 1 and decrease by 1 for UP button press.
listview.setSelection(position);
adapter.notifyDataSetChanged();
This will work for you.

Android - dynamically update GridView views

I've got a GridView with a custom Adapter, displaying thumbnails of pictures to the user. I can select and deselect pictures (I use setAlpha(0.25) on the views to notify the user of the change), and that's all well and fine.
Now what I want to do next, is have a button on top of the gridview that clears the whole selection, i.e. call setAlpha(1.0) on all views that were changed. So far I can reset my elements in the adapter, and I can setAlpha to 1, but the view doesn't update unless I scroll it out of the display, and then get back to it, or notify the adapter of changes, which redraws all my views, which does not look too pretty if only one element was selected.
I already dynamically set and reset individual elements through the GridView's onClickListener, but can't do this for more. I even tried calling performClick on all selected items through my button, but again it only displays the changes after the views have been out of the screen and are shown again.
Here's how I simulate the clicks:
for (int i = 0; i < mAdapter.getCount(); i++) {
PictureForSelection tempPic = (PictureForSelection) mAdapter.getItem(i);
if (tempPic.isPicSelected()) {
//tempPic.setIfSelected(false);
gridview.performItemClick(mAdapter.getView(i, null, null), i, i);
}
}
EDIT:
Conclusion - don't simulate clicks like this :)
So now I skip click simulation and use this:
gridview.getChildAt(i).setAlpha((float) 1.0);
In general it does exactly what I wanted it to do, except for one case:
If I have for example 3 pictures selected, one is off screen, one is partly shown, and one is fully shown, the ones shown (even partially) don't update their views.
If all the selected pictures are displayed it works flawlessly, but if some are out of the display, the rest do not update until the adapter's getView() gets called by Android. Still, thanks #mmlooloo for getting me so far.
If anyone knows a way around this, please do share :)
After setting your alpha you can call imageview.invalidate(); this causes your imageview to redraw itself.
if notifyDataSetChanged() is not working you can try like this.
i'm not sure whether this works,
1.onclick of the button you set the gridView.setAdapter(null);
2. Then set a new CustomAdapter object to gridview with default values.

Android LongClick in a ListView of Clickable Elements

If I have a fragment containing a ListView where each row is a series of clickable elements (say, 3 buttons) filling all available space on the row, can I possibly have a long click callback for the entire row?
I want the user to be able to click on any of the three elements with a quick click but the long click should select the entire row/entry.
I have tried hooking up the ListView with the setOnItemLongClickListener while setting android:longClickable on the individual elements in the layout, but I never see the callback get hit for the long press.
Is this feasible? Do I need to have each element listen for the long click and push it back to the ListView somehow?
Since these three elements(Button) filling up entire space of the , it is difficult to set long click listener for that row...
so good idea is set one same long click listener for these elements rather than row, if they don't have any long click events..
thank you
Put longclicklistener on list view and when that is performed some boolean make true and in your 3elements clicklisteners check if it is false than do other commands.

Android GridView Focus Issueon children?

I have a GridView to which a custom adapter is set. getView() method of the custom adapter returns a LinearLayout with two TextView and an EditText arranged horizontally. I have made a custom numeric keyboard for entering text in the EditText. Keyboard contains NEXT and PREV buttons as well which are creating problems. I want NEXT button to automatically focus the next EditText in the next row and similarly PREV button. NEXT button onKeyPress seems like:
View v=getWindow().getCurrentFocus().focusSearch(View.FOCUS_FORWARD);
if(v!=null)
v.requestFocus();
The code seems right. The problem is, suppose currently only three rows are visible of gridView,if the focus is on the third EditText and NEXT is pressed,it then focuses on nothing. I dont know how to solve this issue. If anyone knows how to solve it.
Thanx in advance.
Can you maintain a collection of ids of all the editText's in the same order, while adding them for rendering.
In that case, for clickHandler or NEXT/PREV button, u can check following:
clickHandler of PREV: if present selection is first in list, then you might want to set focus on last element in list.
clickHandler of NEXT: if present selection is last in list, then you might want to set focus on first element in list.
Alternatively, if you do not want a list roll back functionality, you can just check the position of element in list, and handle the brink elements any way u want them to be.
Hope I understood your problem correctly.
HTH

Categories

Resources