看看ishijiuijing
为ListView增加Header
ListView提供了Header,让我们可以轻松地建立起标题,以实现美化。
写个简单的例子说明这个事情。先写个最简单的。
这里的header就是一个TextView生成的ListView Header部分。在布局的Listview部分:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
在代码中在ListView中加入Header:
listView = (ListView) this.findViewById(R.id.list);
TextView textView = new TextView(this);
textView.setText("header");
listView.addHeaderView(textView);
Header部分是可以添加多个的,比如:
对Header再做进一步定制。见效果:
这里的Header部分使用了自定义视图。
首先要增加一个小房子的图:
然后,创建一个针对Header的layout:
该文件内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:src="/blog_article/@drawable/ic_menu_home/index.html"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
然后在代码中通过inflate的方式把layout加入到header。
listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.table_title, null));
首先,我来穿件一个存放效果的类,命名为WindowAnimation,
public class WindowAnimation extends Animation {
private int halfWidth;
private int halfHeight;
private int duration;
public WindowAnimation(int duration){
this.duration = duration;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
Matrix matrix = t.getMatrix();
matrix.preScale(interpolatedTime, interpolatedTime); //进行缩放,此时的interpolatedTime表示缩放的比例,interpolatedTime的值时0-1,开始时是0,结束时是1
matrix.preRotate(interpolatedTime * 360); //进行旋转
matrix.preTranslate(-halfWidth, -halfHeight); //改变动画的起始位置,把扩散点和起始点移到中间
matrix.postTranslate(halfWidth, halfHeight);
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
this.setDuration(duration); //设置动画播放的时间
this.setFillAfter(true); //设置为true,动画结束的时候保持动画效果
this.halfHeight = height / 2; //动画对象的中点坐标
this.halfWidth = width / 2;
this.setInterpolator(new LinearInterpolator()); //线性动画(速率不变)
}
}
然后在Activity中,要跳转Activity的时候实现上面这个类中的方法就行:
/**
* 切换到指定activity
* @param activityId
* @param intent
*/
public void toActivity(String activityId,Intent intent) {
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
View view = getLocalActivityManager().startActivity(activityId, intent).getDecorView();
//切换activity时显示的动画效果
view.setAnimation(new WindowAnimation(500));
mViewFlipper.removeAllViews();
mViewFlipper.addView(view);
mViewFlipper.showNext();
}
下面是我直接摘抄他人的实例,作为参考: