Android Listview Refresh animation - android

I have simple ListView that displays data from database. Every after 1 min data are getting refresh automatically. Here is the code snippet I am using to do that :
DataAdapter adp = (DataAdapter) DataList.getAdapter();
adp.UpdateDataList(DataAdapter.DATA_LIST);
adp.notifyDataSetChanged();
DataList.invalidateViews();
DataList.scrollBy(0, 0);
I have created ListView i.e. DataView and dataAdapter that simply extends baseAdapter.
UpdateDataList is simply get data from database and creates an arrayList.
And Adapter notifies view to refresh the Data.
Everything is working perfect.
Now one thing I am trying to do here is when data refresh I need to add some kind of animation so that it becomes eyecaching. And people mark that something happened.
Similar to iPhone application. I do not want to add spinner because data update is synchronize process so data change is quickly without making any new changes in view. Simply numbers got flipped.
Any help will be appreciated....

Here is the code I tried and worked well for me....
/* Setting up Animation */
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(400);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(400);
set.addAnimation(animation);
LayoutAnimationController controller =
new LayoutAnimationController(set, 0.25f);
parent.setLayoutAnimation(controller);
/* Animation code ends */

Maybe you can attach an animation to your ListView, since ListView extends ViewGroup you can do it, read the LayoutAnimationController on android reference: http://developer.android.com/reference/android/view/animation/LayoutAnimationController.html
cheers

Related

How to apply animations to entire Android Listview

I am trying to have a Listview fade out/in under certain circumstances. I'm running into issues when running the animation, the Listview just becomes invisible and no animation is performed.
I've tried running the fade_out animation:
Animation out = AnimationUtils.loadAnimation(mainActivity, R.anim.fade_out);
out.setInterpolator(new LinearInterpolator());
myListView.startAnimation(out);
As well as an AlphaAnimation
AlphaAnimation animation1 = new AlphaAnimation(1f, .5f);
animation1.setDuration(2000);
animation1.setFillAfter(false);
myListView.startAnimation(animation1)
and finally simply setting the alpha
myListView.setAlpha(.5f)
The best I can tell is that alpha transparencies are not supported with a list view and only values of 0 and 1 are available as alpha. I can't find any documentation confirming or denying either way.
Is there some kind of known issue with ListViews? Am I doing something wrong? Or is this some kind of limited functionality on ListViews?
Just try it's.If you want to do visible :
public void viseb_show(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.VISIBLE);
}
or if non visible
public void viseb_gone(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(3500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.GONE);
}
anim it's you object who mast be visible.
I figured out my issue. emigrantdd sent me in almost the right direction, how ever there were a few problems I needed to overcome.
First I had to alter emigrantdd's code to:
public void viseb_gone()
{
AnimationSet set = new AnimationSet(false);
Animation fadeout= new AlphaAnimation(1.0f, 0.0f);
fadeOut.setInterpolator(new LinearInterpolator());
fadeOut.setDuration(3500);
set.addAnimation(animation);
myListView.startAnimation(fadeOut);
myListView.setVisibility(View.GONE);
}
As there was some problem with the interpolator.
The other issue I had was that the ListView I was testing on was still in a ScrollView as a part of an old design, once it was taken out it worked as was intended with the changes above.

How to animate individual elements in ListView

I'm doing a small Android app based around a ListView. When the user selects one or more elements in the list and subsequently selects a menu item from the ActionBar I would like to do a small animation on the selected elements in the list, and this is where things go wrong.
Nothing animates - nor does anything fail. The following code piece is a simplified version of what I'm doing:
private void animateListViewItem()
{
TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f);
anim.setDuration(2000);
View v = fragment.getListAdapter().getView(fragment.getListView().getFirstVisiblePosition(), null, null);
v.startAnimation(anim);
}
When I messed around with it, trying to figure out what was wrong, I at one point substituted the item with the entire ListView to rule out the animation as the source of the problem - like this.
private void animateListViewItem()
{
TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f);
anim.setDuration(2000);
fragment.getListView().startAnimation(anim);
}
To my amazement, that worked perfect!
So my question is - why can't I animate the individual elements in a ListView? Or is there something I am doing wrong?
Thanks!
P.S. For the record the ListView is populated with custom views (LinearLayouts), and I have checked that I get the right item before animating.
I found out what the issue was:
View v = fragment.getListAdapter().getView(fragment.getListView().getFirstVisiblePosition(), null, null);
This line was the problem. It returns a new View to display the underlying data at the specified position in the list not the existing View. So the returned View has nothing to do with the list.
Instead doing this:
View v = fragment.getListView().getChildAt(fragment.getListView().getFirstVisiblePosition());
Got me the View that the list was using and the animation worked as expected.

Shading sub Layout in android

I am stuck in a condition that I can't keep the sub layout faded after using the fade animation
FrameLayout mapLayout = (FrameLayout)findViewById(R.id.mapLayout);
Animation fadeAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
mapLayout.startAnimation(fadeAnim);
Please guide how can I achieve this in android. This animation and shaded effect occurs when I click the 'Pause' Button and to reverse the effect I will press the 'Resume' Button(Sorry I made the image with 'Pause' button in shaded view, in actual it is 'Resume')
Try
fadeAnim.fillAfter(true);
fadeAnim.execute();
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
hii makki you can set the below code at your onclick event
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(100);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
l.setLayoutAnimation(controller);
then fade out animation
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
Both above answers helped me in solving the issue, but both answers weren't the actual solution to my problem. The solution I made is
I defined two animations according to my need
Animation fadeOut = new AlphaAnimation(0f,0.5f);
Animation fadeIn = new AlphaAnimation(0.5f,1f);
fadeOut.setFillAfter(true);
fadeIn.setFillAfter(true);
When I clicked 'Pause' button I executed following line
mapLayout.startAnimation(fadeOut);
Then to resume from paused state, I executed following line
mapLayout.startAnimation(fadeIn);
Thanks Simon and Deepak!

Android initialize Animation

I couldn't find examples of how to initialize the animation object.
example Animation ticketAnim;
well new Animation(); is not a valid object it seems so I can't just do Animation ticketAnim = new Animation(); but I would like to. I take the suggested initialization route that the IDE offers which is Animation ticketAnim = null;
of course, accessing this will result in a null pointer exception
what is the right way to do this?
When declaring a new animation, you need to use the constructor of an animation type. Here's some sample code for one of the animation controllers I use in my code:
private void addDeleteDropAnimation() {
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(150);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(300);
set.addAnimation(animation);
controllerDel = new LayoutAnimationController(set, 0.5f);
vw_delLinearLayout.setLayoutAnimation(controllerDel);
}
The Animation class itself is just an abstraction. To use an animation, implement one of Animation's direct know subclasses (also specified in the link to the Animation API).
These include:
AlphaAnimation
TranslateAnimation
RotateAnimation
ScaleAnimation
If you want, you can also create your own custom animation by extending the Animation class. A good example of creating a custom animation can be found here.

Listview and animation

I'm using listview animation from API DEMOS, example 2. here's the snippet from OnCreate method:
ListView listview = (ListView) findViewById(android.R.id.list);
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(200);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
listview.setLayoutAnimation(controller);
At some point in the future, notifyDataSetInvalidated() is called upon list's adapter, and my list is refreshed. but the items are not shown in animation any more.
Please help.
If you want to reanimate your LayoutController after your data set changes, call the method startLayoutAnimation() of the view.
You should check out this thread about difference between notifyDataSetChanged() and notifyDataSetInvalidated() (Link updated to point to Romain Guy's answer!)
In short the difference is:
notifyDataSetChanged: The items in the data set are changed (added / removed / updated / reordered, whatever).
notifyDataSetInvalidated: The data source of the adapter is no longer available.
Here you can find a working sample with notifyDataSetInvalidated function in use. Probably it will do the trick with your animation problem as well.

Categories

Resources