I apply the expand animation in ListView using,
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.bottomMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();
if (mIsVisibleAfter) {
mAnimatedView.setVisibility(View.GONE);
}
mWasEndedAlready = true;
}
}
Here, when I try to click the last item of the ListView, the last item of my Listview expand downward so, I cannot see the whole contents of the last one without scrolling upward.
In other words, If I click the last one, I want my focus on the bottom side of the last item (automatically scrolling) so I can see the full contents of the last one immediately.
Is there any way to solve it?
Maybe this will help:(First check if the user has clicked the last item)
listView.setSelection(ListAdapter.getCount() - 1);
Or sometimes you have to do something like this:
listView.post(new Runnable()
{
#Override
public void run()
{
listView.setSelection(ListAdapter.getCount() - 1);
}
});
Related
I have a recyclerview in my app, and each row contains a button which shows a text in the same cell, under the button. I have used ChangeBounds transition to make the text appear with a smooth animation, increasing the row height until the text is completely shown. So when a button is clicked, I do:
TransitionManager.beginDelayedTransition(row, transition)
holder.hiddenText.setVisibility(View.VISIBLE)
It works well, but whith a problem. The hidden text appears with an animation which increases its height, as expected. But the row height is not animated, and it jumps from the original height until the final height without any animation.
Is there any way to achieve a height transition over the row, to increase at the same time as the text?
If you want animation to take effect for row also, then you need to perform beginDelayedTransition() on a one layer higher of the row, which in your case maybe is the actual RecyclerView.
Try this on your row's layout
public static void expand(final View v) {
v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation()
{
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? LayoutParams.WRAP_CONTENT
: (int)(targetHeight * interpolatedTime);
v.requestLayout();
}
#Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
Linear or Relative layout of row's will animate and expand. Hope this solves your problem
when I click on button then an animation goes to left to right and right to left only for menu section layout and according to this animation the width of other layout (head of family should be expand or collapse..
my problem is that the animation for menu layout is working properly but the width of other layout not to collapse or expand simultaneously ,how can i do this.
my code is this
if(flagmenu)
{
//menu layout set animation
lpmenu.startAnimation(animationFallout);
Thread t=new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2500);
runOnUiThread(new Runnable() {
public void run() {
lpmenu.setVisibility(View.GONE);
}
});
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
// lpmenu.setVisibility(View.GONE);
flagmenu = false;
}
else
{
lpmenu.startAnimation(animationFalling);
lpmenu.setVisibility(View.VISIBLE);
flagmenu = true;
}
Use this animation code here v is view group mean layout interpolatedTime of animation work fine for me . if code for collapse if u want expend then use + sine
Animation a = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().width= initialwidth
- (int) (initialwidth * interpolatedTime);
// replace - to + for expend
v.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
First, I would suggest you to use ObjectAnimators to achieve this instead of Animations, since Animation does not actually change the View's position. Then, in order to perform simultaneous animations with Animators you may use AnimatorSet class (playTogether method). If you need to support old Android versions there is a NineOldAndroids library which backports Animators. Another way (without animators) is to use AnimationSet (again you should add Animations for each view you want to move) class and implement AnimationListener changing the Layoutparams of your views (in order to update layout)
I have a listview, and wanna change the height of the item when clicking it and the clicked item goes to the center of the screen. When clicking the top ones, the item inreasing it's height downward, it looks ok, but the problem is, when clicking the bottom ones, the view is increased also downward, how can it upward when the item is below the screen center?
The main code is:
Animation a = new Animation(){
#Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
v.getLayoutParams().height = (int)(0.5*mHeight * interpolatedTime);
v.requestLayout();
}
#Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(2000);
v.startAnimation(a);
}
Anyone has any ideas? Thanks in advance.
You should check smoothScrollBy, smoothScrollToPosition and other methods which is used for scrolling. The main idea is to use scroll down, to make your view completely visible. To do this you have to compute how much you need to scroll to make item visible.
This is so weird, I've this animation code:
public class ExpandAnimation extends Animation {
private View mAnimatedView;
private MarginLayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mWasEndedAlready = false;
/**
* Initialize the animation
* #param view The layout we want to animate
* #param duration The duration of the animation, in ms
*/
public ExpandAnimation(View view, int duration) {
setDuration(duration);
mAnimatedView = view;
mViewLayoutParams = (MarginLayoutParams) view.getLayoutParams();
mMarginStart = mViewLayoutParams.rightMargin;
mMarginEnd = (mMarginStart == 0 ? (0- view.getWidth()) : 0);
view.setVisibility(View.VISIBLE);
mAnimatedView.requestLayout();
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.rightMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.rightMargin = mMarginEnd;
mAnimatedView.requestLayout();
mWasEndedAlready = true;
}
}
}
And I use this Animation:
View parent = (View) v.getParent();
View containerMenu = parent.findViewById(R.id.containerMenu);
ExpandAnimation anim=new ExpandAnimation(containerMenu, 1000);
containerMenu.startAnimation(anim);
This animation toggle a layout hidding / showing it.
By default, its hidden. When I click, animation works and it's shown. When I click again, it shrinks correctly. But the 3rd time, it does nothing. I've debugged and I found out that the constructor is called but not applyTransformation.
Somehow, if I click any layout around the screen, the animation suddenly starts.
Any idea?
Edit
Does anyone know WHEN is applyTransformation triggered?
I can't understand why, but when I click or do any action to any layout, the animation finally starts. So I programatically added a workaround. I've a scrollview in my layout, so I move the scroll position:
hscv.scrollTo(hscv.getScrollX()+1, hscv.getScrollY()+1);
This just after containerMenu.startAnimation(anim);
This just works, I can't understand why.
Also, I found out that some animations worked flawless on android > 4, but on 2.3 for instance, it had the same issue, worked to expand, and to shrink, but not to expand for the second time.
parent.invalidate();
Did the trick.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I make a cell in a ListView in Android expand and contract vertically when it’s touched?
I would like to see a good example of a cell view expanding and pushing list items below down like on twitter application. What I want is user clicks on button in list view item and then the item_view expands gracefully to reveal a EditText and button.
Thanks
You need to do something like this..
public class ExpandAnimation extends Animation {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
// End animation sometimes calls with exactly 1.0f
if (interpolatedTime <= 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.bottomMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
}
}
The code above changes the view's margins in every animation frame. That makes it appear like the your new view is pushing the rest of the items downwards. You can also try changing the subitem's height instead of its margins..