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)
Related
I have a recycler view of image views and upon longpress I try to give the image view a border. When I scrolldown and come back to the same object the boder is found on someother image.(It is ok coz... the recycler view deletes itself and creates back and so the position changes).
So what I did is I stored the image URl on long press and after scrolling back I drew the Border on the image based on the image URL.
Now the problem is the border that came previously(wrong position) is also drawn....how to get rid of it.
In simple terms. How to make the recyclerview forget the data changes?
(Notifydatasetchanged....Something like that)....
thankz in advance....
This is the problem caused by the position at which the boarder was made is not the same position after the scroll on RecyclerView you can easily fix this by allowing your object to remember if it was selected by long-press or not
Assume this is the class you have
class Item {
// here you already have your instance variables
// add another one
boolean isSelected;
}
when you long-press on an item make this instance variable of respective Item, true and provide it a boarder.
when you populate your List in RecyclerView's adapter what you can check is if the isSelected is true than make respective imageView have a boarder. otherwise don't
by doing so you will be independent of the position of the item with respective of the scroll. So your Item will retain the boarder which was actually selected.
I am creating a application which contains a listView on lefthand side that display different images now i want to show that particular image in the center of the screen when user clicks on any image in listView.
You can add a custom adapter for your listview and add an onitemselectedlistener where inside it you can get the image you put in the view, and set the imageview.
You should keep the list of path of images and when user taps you will get the postion from OnItemClickListener. Using the position you can load the image from path.
The link has good content on creating custom adapters , You should use that then just map your path and positons. try that and post when you are stuck
In the native Gallery app on Android, when Select item via Option Menu action, you can select multiple items, each grid item which will have a colored-border overlay on top signify as selected when clicked.
Question:
How to do this dynamically using getView() in the ImageAdapter
class? (since there is setBackgroundColor() but not setBorder()
Is there a better way to accomplish this? (such as creating a padding
of some sort for the image within the gridview cell, then set the
background color which will look like a border)
I finally decided to use the 2nd method in the original post: creating a padding for ImageView within the gridview cell and set the background color.
I used ImageView.ScaleType.CENTER_CROP with setCropToPadding(true); for the ImageView.
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.
I really need horizontal scrolling in my app, so I overrode Gallery and I'm trying to do something with an OnItemSelectedListener, and a GestureDetector I'm calling from an OnTouchListener I've set on the Gallery. What I want is to remove the auto-selection of Gallery items as I scroll, but I want to be able to click an item to select it.
If I set an OnClick listener on a view that is populated in the adapter, the Gallery fails to scroll. Also, the OnItemClicked event is never called for the listener I set for that on the Gallery.
To remove selection from all items you should do this:
youGallery.setUnselectedAlpha(1);
Maybe my answer to this question will help you. It's something which wasn't intended to be ever realized...
For your case why dont you just use a HorizontalScrollView. Just put a linear layout inside and add as many imageview's as you need if your using images, or dynamicaly add it in java code.
This will elemenate the selection of gallery by default. This will be easier than trying to Override the Gallery widget default methods.
Why not you set some tag while you click on an item in the Gallery to select.
Now once again when you click after the item is selected just check for the tag that you had set, and based on the condition you can move to new screen and whatever task you want to perform.
But keep one thing in mind, you have to set the tag with every gallery item while building the gallery view.
I hope you will get what I want to say.
If still you have any problem then let me know.
I had the same scenario and I solved that.