Skip to content

Instantly share code, notes, and snippets.

@nightbear1009
Created September 19, 2014 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nightbear1009/5892579a95b7a1d484b9 to your computer and use it in GitHub Desktop.
Save nightbear1009/5892579a95b7a1d484b9 to your computer and use it in GitHub Desktop.
listview header animation
package com.example.tedliang.myapplication;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.method.CharacterPickerDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MyActivity extends Activity {
private ListView listview;
private TextView textview;
int height;
private class MyAdapter extends BaseAdapter{
LayoutInflater inflater;
public MyAdapter(Context context){
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return 100;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.adapter_layout,null);
TextView textivew = (TextView)view.findViewById(R.id.textview);
textivew.setText(String.valueOf(i));
return view;
}
}
boolean isUp = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
textview = (TextView) findViewById(R.id.textview);
listview = (ListView)findViewById(R.id.listview);
listview.setAdapter(new MyAdapter(this));
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(isUp) {
height = listview.getHeight();
ObjectAnimator animX = ObjectAnimator.ofFloat(listview, "translationY", 0f, -textview.getHeight());
ObjectAnimator animY = ObjectAnimator.ofFloat(textview, "translationY", 0f, -textview.getHeight());
AnimatorSet set = new AnimatorSet();
set.playTogether(animX, animY);
set.start();
ValueAnimator.AnimatorUpdateListener listUpdater = new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.d("Ted", "listview height" + listview.getHeight());
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) listview.getLayoutParams();
p.bottomMargin = -textview.getHeight();
listview.setLayoutParams(p);
listview.requestLayout();
isUp = false;
}
};
animX.addUpdateListener(listUpdater);
}else{
ObjectAnimator animX = ObjectAnimator.ofFloat(listview, "translationY", -textview.getHeight(),0f);
ObjectAnimator animY = ObjectAnimator.ofFloat(textview, "translationY", -textview.getHeight(),0f);
AnimatorSet set = new AnimatorSet();
set.playTogether(animX, animY);
set.start();
ValueAnimator.AnimatorUpdateListener listUpdater = new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams) listview.getLayoutParams();
p.bottomMargin =0;
listview.setLayoutParams(p);
listview.requestLayout();
isUp = true;
}
};
animX.addUpdateListener(listUpdater);
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment