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.
Related
I am building a memory game with RecyclerView grid layout. When user clicks 2 time and didn't find the matched pictures, I want to change image view resource only in the clicked positions.
I built the logic sucessfully, but I don't know how to change imageview's resource in only certain positions.
You might have used the OnClickListener interface for card items click events.
So on click event, get the position of that clicked image tile using the getLayoutPosition() method and change its Drawable resource programmaticaLly through captured tile position.
I solved my problem with recyclerView.layoutManager?.findViewByPosition(position)
I have a recycler adapter in which there are 3 image buttons, one of them is for favorite and unfavorite. when the recyclerviews loads its loads proper also when I click on favorite it stores in my favorite list. But the problem is that when I scrolled it the states of image button changes to load time images.
Make sure you are setting favorite/unfavorite at onBindViewHolder.
RecyclerView efficiently recycles views while you scroll.
when you maintain this kind of scenario, where image view can have Fav or UnFav images. you need to maintain both in onBindVIewHolder() method such as
if (isFav) {
viewHolder.yourImage.setBackgroundDrawable(FavImage);
} else {
viewHolder.yourImage.setBackgroundDrawable(UnFavImage);
}
Try setting the image on the #onBindViewHolder on every case (don't assume that you know the current state).
I have a situation involving animation of listview's item appearances.
I have a few views in a ScollView , the last of which is the listview. I have attached an appearence animation of the row (a fade in animation).
The problem I have is that when the screen is loaded , the getView() of listview already executes for the initial items , even though the listview is not currently in view.
Hence when a user scroll downs , he sees the list plainly.
I am unsure how to go about this situation . Is there any callback that can be invoked when a row from a listview becomes visible on screen ? .
Yes there is a callback (OnScrollChangeListener), first visible index and last visible index etc. You can achieve what you are trying to using combination of these.
But you need to try that yourself first. No one can simply write a code for you.
You can learn about listview here
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.
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.