这里写代码片
![这里写图片描述](http://img.blog.csdn.net/20160722181258519)
ListView 头部缩小放大的效果
package com.example.draglistview;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
public class MyLayout extends LinearLayout {
private int mDefaultHeadHeight;
private ImageView headView;
private ListView twoChildView;
private int height, translationY;
private LayoutParams params;
public MyLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
}
private float downY;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
float moveY = ev.getY();
float distanceY = moveY - downY;
if (distanceY > 0 && twoChildView.getFirstVisiblePosition() == 0) {
return true;
}
if (distanceY < 0
&& twoChildView.getTranslationY() > -mDefaultHeadHeight) {
return true;
}
break;
}
return false;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
twoChildView = (ListView) getChildAt(1);
twoChildView.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
headView = (ImageView) getChildAt(0);
mDefaultHeadHeight = headView.getMeasuredHeight();
height = mDefaultHeadHeight;
params = (LayoutParams) headView.getLayoutParams();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
float moveY = event.getY();
float distanceY = moveY - downY;
if (distanceY > 0) {
if (twoChildView.getTranslationY() >= 0) {
height += distanceY / 2.5f;
params.height = height;
headView.setLayoutParams(params);
} else {
translationY += distanceY;
if (translationY > 0) {
translationY = 0;
}
twoChildView.setTranslationY(translationY);
}
} else {
if (height > mDefaultHeadHeight) {
height += distanceY / 2.5f;
params.height = height;
headView.setLayoutParams(params);
} else {
// 等于0 的情况也包括在内
if (twoChildView.getTranslationY() >= -mDefaultHeadHeight) {
translationY += distanceY;
if (translationY < -mDefaultHeadHeight) {
translationY = -mDefaultHeadHeight;
}
twoChildView.setTranslationY(translationY);
}
}
}
downY = moveY;
break;
case MotionEvent.ACTION_UP:
if (height > mDefaultHeadHeight) {
ValueAnimator animator = ValueAnimator.ofInt(height,
mDefaultHeadHeight).setDuration(1000);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
height = (Integer) animation.getAnimatedValue();
params.height = height;
headView.setLayoutParams(params);
}
});
animator.start();
}
break;
}
return true;
}
}
<com.example.draglistview.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="match_parent"
android:layout_height="150dip"
android:src="@drawable/banner1" android:scaleType="fitXY"/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:background="@android:color/white">
</ListView>
</com.example.draglistview.MyLayout>
Android ListView 头部缩小放大的效果
最新推荐文章于 2019-04-18 18:35:32 发布