getFirstVisiblePosition() returns wrong value in Gallery View - android

I have a Gallery view created with a SimpleAdapter that extends BaseAdapter. It contains more items than what it can fit in the screen.
So I'm using Gallery.getFirstVisiblePosition() and Gallery.getLastVisiblePosition() to get the first and last visible position. But these functions doesn't always return the correct values especially after setting the selected position by calling mGallery.setSelection() followed by a SimpleAdapter.notifyDataChanged() call. Most of the time it getFirstVisiblePosition() returns 0 even if the first element is not visible.
Also note that the no. of visible items in Gallery is different in portrait and landscape mode. The value returned by getFirstVisiblePosition() look correct in landscape but is returned wrong in portrait mode.
Anyone has any thoughts/ideas?
Thanks!

The first visible position will be updated only after a layout. You cannot call notifyDatasetChanged() and right away get the first visible position, you must wait for the next layout pass.

Related

Not able to scroll till end in android

I have TextView+webview as ListItem of ListView. When I change orientation. I am reloading all files as well setting adapter again and I am calling following code to select item from list programtically so I can get onItemClick call same ways as user would have clicked.
if(position!=-1 && getAdapter()!=null){
requestFocusFromTouch();
performItemClick(
getAdapter().getView(position,
null, null), position,
getAdapter().getItemId(position));
}
Now when I change orientation of screen, the selected item's webview is not scrolling till the end. It seems that it is not changing its scrolled position when we change orientation (Even though we reset adapter).
I tried requestlayout(), forcelayout(), invalidate(),reload() method of webview but none of them seems to be working.
You probably have your WebViews heights set to wrap_contents. This means that their heights are 0 when the list is loaded and they expand to a > 0 height at some point. One way to solve your issue would be to wait for all of the webviews to call onSizeChanged before scrolling, another option would be to re-position the list every time the lists contents change size.

Android listView performItemClick calls click listener but does not change highlighted row after orientation changed

listView.performItemClick(listView, 0, listView.getItemIdAtPosition(0));
I tried this and it works in most cases. There is one strange case: when you rotate the device, I call a performClick to e.g. Position zero, the onClickListener is called and the DetailView for the position zero is called. But if the last activated item was position 3, position 3 still continues to be highlighted. This works when I start the activity and set the first row to be activated, but not after orientation changes.
Situation: I am in Activity A in tablet mode, right site Detail view, left site list view.
Step 1: I touch the 3. list item -> the row is highlighted and the Detail view is shown.
Step 2: I rotate the tablet and I do not save anything, I do not catch orientation changes, nada-> 3. item continues highlighted.
Next step: I try to call listView.performItemClick to Position cero in the onActivityCreated Method -> effect: when the activity first starts the first row is highlighted and the Detail view is shown, when the orientation changes, onActivityCreated() is called again, performItemClick too, the onClickListener is called and the Detail view updated, but list item number 3 still continues highlighted.
This happens just because view is refreshing whenever the Orientation changes. If you clicked on the listItem on the perticular orientation then it is high lighted as you wanted. But after change the orientation the view is refreshed
IF you want save the state then take a look at here:
https://stackoverflow.com/a/151940/1395259

knowing what elements of a list view are currently displayed on screen

I would know if there is a way to know if an element of a list view gets out of the list, (i want to know if the fist element is seen on screen or isn't)
in fact what I want to do with that, is that i want the second element of my list (which is the title of the fragment where the list is) to stay blocked as a header when it goes up (like an letter in the contact app).
So what i thought about is to add a hidden clone of the second element above the listview (in the relative layout parent), and set it to visible when the first element goes away of the screen (when the second element is the first element of the seen list).
But maybe is it a native way to do so !
Thanks,
Renaud
You can obtain the first and last visible position in the list at any time using the methods getFirstVisiblePosition() and getLastVisiblePosition() on ListView
HTH

Maintain Scroll Position of GridView through Screen Rotation

How can I maintain the scroll position of a GridView (it's filled with search results) throughout a screen reorientation?
I have the GridView inside a Fragment, and when I rotate the screen it jumps back up to the top of the list.
To make it even more tricky, in landscape mode my GridView has 3 columns.. in portrait mode it has 2 columns.
I can't seem to figure out how to keep the fragment's GridView scrolled to anywhere near where it should be when the screen orientation changes.
You can use the following method, it will work for all Android versions.
To save the current scrolling position:
int index = gridView.getFirstVisiblePosition();
Then after orientation change, use the following code to set the gridView position to the saved index:
gridView.setSelection(index);
You can try this:
Initially you have to get the current scrolling position of the GridView by calling:
int index = gridview.getFirstVisiblePosition();
Then you have to save this value while orientation changes and when the GridView is created again you have to move your gridview to this index.
I suppose this could work:
gridview.smoothScrollToPosition(int index)
Hope this helps!
Using of getFirstVisiblePosition() is good, but I've found a nice solution, just to save a state of view.
//state of view (includes scroll position) as a Parceble
Parcelable mobileGalleryState = gridView.onSaveInstanceState();
//just restore previous state including all changes are made before
gridView.onRestoreInstanceState(mobileGalleryState);
By the way, you can use it for ListView also.

Continuously scrolling Gallery?

I have a Gallery object that scrolls from left to right and back again. However, I would like to make this Gallery circle back on itself, that way when I get to my last View, the very first one is next and I can just keep scrolling. Any ideas? Thanks.
Galleries use an Adapter to back the data that they display. You can create a custom adapter where getCount() returns Integer.MAX_VALUE and getView() does a modulos the position with the number of images you have. This way it always returns the appropriate image for a given position.

Categories

Resources