Animation resetting view state? - android

i am trying to do some simple animation, i have list with items, those items include also checkboxes
on a checkbox button click i want to show with animation some button from the buttom, something like this:
private int mPosition;
private CheckBox chkBox;
OnItemClickListener(CheckBox mChkBox, View v)
{
chkBox = mChkBox;
chkBox.setClickable(false);
chkBox.setChecked(false);
chkBox.setClickable(true);
}
#Override
public void onClick(View v)
{
if (chkBox.isChecked())
{
animation = AnimationUtils.loadAnimation(context,
R.layout.animation_slide_in);
animation.setDuration(500);
animation.setInterpolator(new AccelerateInterpolator());
btDeleteItms.startAnimation(animation);
btDeleteItms.setVisibility(btDeleteItms.VISIBLE);
}
now the wierd thing, that after i click the checkboxbutton, the animation does work fine,but the state of the triggering checkbox button, is being uncheck unexpectdly?
how come? does animation resetting states somehow?
Thaks,
ray.

If I understood you correctly you need to use animation.setFillAfter(true); after .startAnimation(animation) method.

Related

Recyclerview button state changing after onBindViewHolder

I have a Recyclerview where there is a button onclicklistener in the viewholder constructor:
public ViewHolder(View itemView)
{
super(itemView);
...
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
...
}
}
}
Each item also has a countdown, every second the countdown is decremented and a progressbar of the item is updated to display the time left.
The timer is done with a runnable:
countdownHandler.postDelayed(countdownRunnable, 1000);
which then iterates through all the items, decrements the countdown and notifies the adapter:
countdownRunnable = new Runnable()
{
#Override
public void run()
{
Iterator<Item> iterator = countdownTasks.iterator();
while (iterator.hasNext())
{
item.decrementCountdown();
adapter.notifyItemChanged(items.indexOf(item));
}
if (countdownTasks.size() > 0)
countdownHandler.postDelayed(countdownRunnable, 1000);
}
}
Here is my problem, it also occurs if there is only one item in the list.
I press the button and is in the pressed state, but when the countdown update triggers, the button is no longer pressed. Releasing the button doesn't activate the button as it should. If I remove the countdown, it works properly.
I don't change the button in onBindViewHolder and I am not scrolling since there is only one item. Is this expected bahavior, that all button presses get canceled as soon as notifyItemChanged is called or am I doing something wrong? Does this even work with the onClickListener or do I need an onTouchListener and save the touchdown state in the item and reset it every update?
Thank you!
Edit:
I found out, that if I set
recyclerView.setItemAnimator(null);
it works without problems. Does anyone know why that is the case and how I can still get animation without the hassle of remembering all the states?
When you call adapter.notifyItemChanged(items.indexOf(item)); it rebind view for that position, which is fresh view stored in ViewHolder & don't have a pressed state & that's why it loses button state.
You can do it by using OnTouchListener and storing states as you mentioned in question.
I am writing as anshwer since i don't have reputation for comment.

How to Pause an activity with Button Click?

I want to pause an animation activity with button click, here i have one ball which is falling from top to bottom animation. I want one button "stop" which pauses the fall in between and record its x,y position.
You can add a condition in your ball animation such that
while(buttonPressed == false && //whatever other code you need for the animation to run)
{
//button animation code here
}
and then have an if statement after it, with the condition being that if the buttonPressed is true, that will record the position of the ball. You can set the buttonPressed boolean to be true by using an action listener added to your stop button.
Put this first:
AnimatorSet set;
img_logo = (ImageView) findViewById(R.id.img_logo);
set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.flipping);
set.setTarget(img_logo);
set.start();
On Button Click:
set.stop();
I think this could also help you--->http://android-er.blogspot.in/2012/01/start-and-stop-frame-animation-with.html

Animated Refresh icon

The plan is for the refresh icon to starts animating when the user clicks on the refresh icon, however, there seems to be an error when I implement the animation function; "The method loadAnimation(Context, int) in the type AnimationUtils is not applicable for the arguments (new View.OnClickListener(){}, int)".
Below is the code snippet. How do I load the Animation from the Animation Source, without the above error while still retaining the onClickListener framework.
final ImageView refreshBtn= (ImageView) findViewById(R.id.spin_refresh);
Log.i("RootActivity:setupHeader","******ImageView refreshBtn******");
//Listening to Button Click by User
refreshBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//To set new INTENT on calling Methods in UPDATE & DOWNLOADSERVICE
.....
//Perform Refresh Animation
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ImageView iv = (ImageView)inflater.inflate(R.layout.header, null);
//ISSUE IS AT .loadAnimation not able to be implemented.
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_refresh);
rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);
item.setActionView(iv);
//ALERT DIALOG TO INFORM USER THAT REFRESH FUNCTION HAS BEEN CALLED
.......
}
});
Change
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_refresh);
to
Animation rotation = AnimationUtils.loadAnimation(YourActivity.this, R.anim.rotate_refresh);
The reason is that at that point in your code, this refers to View.OnClickListener, not to your activity.
this refers to OnclickListner there
change it to your context
Animation rotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_refresh);

Restart CountDownTimer on click

I have a simple CountDownTimer that displays its content in a textView. I am trying to accomplish the following:
during count down, the view is not clickable: textview.setClickable(false);
after count down finish, if the user clicks the textview, the count down should restart.
So I try a combination of
#Override
public void onFinish() {//inside CountDownTimer
view.setClickable(true);
}
and
textview.setOnClickListener(countAgain);
OnClickListener countAgain = new OnClickListener() {//inside activity
#Override
public void onClick(View v) {
// counter.cancel();
counter.start();
}
};
But this is not doing it. Any ideas?
Set onclick listener for that text view, make it clickable .
In onclicklistner, make textview as non-clickable( yourTextView.setclickable(false) ).
change the text view values as you want and every time check the value against 0. When it becomes zero, make the text view clickable( yourTextView.setclickable(true) ).

Stop Blinking of TextView

This is a code for blinking textview on a button click..
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
recordShow.startAnimation(anim);
}
i have to stop blinking on another button click...what to do..??
Another approach could be:
1. Declare the Animation and TextView objects globally (outside any methods) in your Activity.
private Animation mAnim;
private TextView mRecordShow;
2. Setup a class that sets your animation properties and starts it. Let this class expect a TextView widget as its parameter.
protected void setBlinkingText(TextView textView) {
mAnim = new AlphaAnimation(0.0f, 1.0f);
mAnim.setDuration(1000); // Time of the blink
mAnim.setStartOffset(20);
mAnim.setRepeatMode(Animation.REVERSE);
mAnim.setRepeatCount(Animation.INFINITE);
textView.startAnimation(mAnim);
}
3. Setup another class that stops your animation on a given text view. Let this class expect a TextView widget as its parameter as well.
protected void removeBlinkingText(TextView textView) {
textView.clearAnimation();
}
4. Now you can use your classes wherever desired, passing it the appropriate text views.
e.g.
(a) In your onClick() method where you want to start the animation, replace all your animation code with:
setBlinkingText(mRecordShow);
(b) wherever you want to stop the animation on that text view, just call:
removeBlinkingText(mRecordShow);
The following assumes you want to stop the blink by clicking the same button. If you want to stop the click using a different button, you can split the if-else in the onClick() below into separate click handlers.
First, move anim outside onClick() and make it a field of the containing class. You need anim to be stored somewhere so you can cancel it later.
Animation anim = new AlphaAnimation(0.0f, 1.0f)
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
Second, create a boolean field in the containing class to keep track of whether the TextView is currently blinking:
boolean mBlinking = false;
Then:
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
if(!mBlinking){
recordShow.startAnimation(anim);
mBlinking = true;
} else{
recordShow.clearAnimation(anim); // cancel blink animation
recordShow.setAlpha(1.0f); // restore original alpha
mBlinking = false;
}
}

Categories

Resources