Animation repeats on the images that were selected previously - android

I can't provide the code for the moment I'll update the question in the nearest future, but I'll explain the situation and you can tell me what to check first of all.
I have a Gallery with a set of ImageViews.
When ImageView is selected, view.startAnimation(grow); is cast. Everything works perfect.
Then I tap (onTouchEvent starts another animation on the unselected view: view.startAnimation(decrease);)
Everything works fine on the first element.
But when I choose the next imageview, grow animation works fine aswell, but decrease animation works on both of the imageViews. So it seems that startAnimation method runs the animation on all the imageViews that were choosen previously.
I had no luck finding the same question on stackoverflow. Will be very appreciate if you give me some ideas.

It may help someone.
Finally I've found the solution.
I have 2 methods:
animationShow(View view) {
View.startAnimation(animationShow);
}
and
animationHide(View view) {
View.startAnimation(animationHide);
}
I also have 2 views to work with: currently selected view (selectedView) and previously selected view (prevView), so I hide animation on prevView and show it on selectedView.
To avoid gallery issue with hiding animation, I changed animationShow() method:
animationShow(View view) {
View.startAnimation(animationShow);
if (prevView != null) {
prevView.cancelAnimation();
}
}
When new item is selected (and old item was hidden) I just cancel all the animations on the prevView. It helped me. Hope it will help someone else.

Related

Right way to iterate over GridLayout children with setOnClickListener in order to animate with stateListAnimator

I have a GridLayout (central_container) with 4 clickable images and animation defined in xml (animator/press_button_animation) that describes object behavior on click (press/release).
I try to connect this animation to the images in the code.
It works fine for one of four images if it is hardcoded:
private fun initAnimation() {
val stateListAnimator = loadStateListAnimator(
this,
R.animator.press_button_animation
)
central_container.getChildAt(0).setOnClickListener {
it.stateListAnimator = stateListAnimator
}
}
What is the right way to make it work on all GridLayout children?
I've tried with for loop but it stops on last image. For example if I click one by one and reach the last one, the previous images stop to react
private fun initAnimation() {
val stateListAnimator = loadStateListAnimator(
this,
R.animator.press_button_animation
)
for (n in 0 until central_container.size) {
val img = central_container.getChildAt(n)
img.setOnClickListener {
it.stateListAnimator = stateListAnimator
}
}
}
I'm sorry for being not clear enough. After number of trials and experiments I'll try to explain more exactly.
I have 4 clickable images and expect each one to be animated on click.
The problem is that inspite of each image responds to click rigth (I checked it in debugger) but the problem is with animation. For example, first image animates well, then second image animates well. But if after the second I click on first, it is clicking but the second image is the one who animates. And so on...
Or, for example if I click images one by one by order (1-2-3-4) each one animates well. Then, if I click on 1 or 2 or 3 so the one who animates is still 4.
Seems like there is a need to "reset" animation after each click somehow or anything like that.

Update View positions after TransitionManager.go(Scene,Transition)

I was creating animation between 2 Scenes using TransitionManager.go(Scene,Transition) and everything worked just fine but after the animation finished I was no longer capable of clicking on the button which was moved from the center of the screen to the left side.
Looking around the Internet I found that this animation actually doesn't even move the Views, just animates them so I have to update their positions to new ones.
Isn't there any better way to do this than manually .setLayoutParams for all the Views in the Scene?
I realized that my Views actually were in the right positions after the animation but I had to reset my OnClickListeners every time Scenes changed
Transition.TransitionListener listener = new Transition.TransitionListener() {
#Override
public void onTransitionEnd(Transition transition) {
//get view references from the new Scene and
//reassign on click listeners to them
}
}
I just assigned this listener to my Transition and everything worked fine.
Here is the post that helped me see my mistake.

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.

getView method setting the background of button

I am setting the background of my ImageButton in getView of adapter the problem I was facing was the background image wasn't properly set, i at first used this.
if(exist) {
bookmark.setBackgroundResource(R.drawable.ic_action_important);
}
but the button background always set the background image to important, then i used this and this solves my problem.
if(exist) {
bookmark.setBackgroundResource(R.drawable.ic_action_important);
} else {
bookmark.setBackgroundResource(R.drawable.ic_action_not_important);
}
I don't know the reason why i have to add the else because in the xml view i set the bookmark default image to ic_action_not_important, i was wondering that why we have to use else when the image ic_action_not_important is already in the adapter view, can anyone please explain. Thanks a lot in advance.
#Elduderino Answered your question in comments. The views get recycled.
Meaning the view currently holding position #10's data, might have been holding position #3's data before you scrolled, or opened and closed the view.
In the first example, you only set the background to "important" in your if-statement when the condition is true. Meaning if the background was set to important once, it will never be set to unimportant even after it gets recycled and is used to show data at a different position.
Hence your second example is correct.

android ListView ,setSelection() has no effect after setFlags(...FULLSCREEN)

Now I have this problem when:
In my Asynctask, I have a ProgressDialog over this Listview that is constantly updated and scrolling automatically (I use something like mListview.setSelection(mData.size()-1); to scroll it )
I want it to be fullscreen when it's not finished or canceled, which I think is pretty easy by Window dialogWindow = mProgressDialog.getWindow();
dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
I also want it to stop being fullscreen( i.e. I want the status bar shown) when it's finished. And I want the first item in the Listview shown as well.
-------------------- This 's when the problem occurrs.--------------------
I use like
dialogWindow.setFlags(0, WindowManager.LayoutParams.FLAG_FULLSCREEN);
and it's working well. The status bar appears.
then I use mListview.setSelection(0), which I suppose can bring the me first item.
HOWEVER, it has no effect at all. Any idea how this happens or how to solve it? Any help would be greatly appreciated. Thank you!
you can try this method, it maybe helpful to you..
http://developer.android.com/reference/android/widget/ListView.html#smoothScrollToPosition(int)
public void smoothScrollToPosition (int position)
Since: API Level 8
Smoothly scroll to the specified adapter position. The view will scroll such that the indicated position is displayed.
Parameters
position Scroll to this adapter position.
You have to use
mListview.clearFocus();
before calling
mListview.requestFocusFromTouch();
mListview.setSelection(0);
in a post method of mListView.

Categories

Resources