首页 / 更多教程 / ListView反弹
ListView反弹
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ListView反弹,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4154字,纯文字阅读大概需要6分钟。
内容图文
![ListView反弹](/upload/InfoBanner/zyjiaocheng/1070/33951b91402441bf80d221fc2a8b0cf0.jpg)
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.ListView;
/**
* listView反弹
*
*/
public class ListViewFantan extends LinearLayout {
private float y_down;
public ListViewFantan(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
float y2 = ev.getY();
float y_move = 0;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
y_down = y2;
break;
case MotionEvent.ACTION_MOVE:
y_move = y2 - y_down;
break;
case MotionEvent.ACTION_UP:
y_move = y2 - y_down;
break;
default:
break;
}
// listview到顶了,顶部反弹
if (listView != null && y_move > 0
&& listView.getFirstVisiblePosition() == 0) {
View childAt = listView.getChildAt(0);
if (childAt != null && childAt.getTop() == 0) {
move_top = true;
move_bottom = false;
return true;// 自己处理
}
}
// listview到底了,底部反弹
if (listView != null) {
// listView.
View childAt = listView.getChildAt(listView.getChildCount() - 1);
if (childAt != null) {
int top = childAt.getTop();
int bottom = childAt.getBottom();
int height = getHeight();
System.out.println("bottom:" + bottom + "height:" + height);
if (bottom <= height && y_move < 0) {
move_bottom = true;
move_top = false;
// 到底了
return true;// 自己处理
}
}
}
return false;
}
/**
* 向上移动(顶部反弹)
*/
private boolean move_top;
/**
* 向下移动(底部反弹)
*/
private boolean move_bottom;
@Override
public boolean onTouchEvent(MotionEvent ev) {
float y2 = ev.getY();
float y_move = 0;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
y_down = y2;
//System.out.println("down:" + y_down);
break;
case MotionEvent.ACTION_MOVE:
y_move = y2 - y_down;
//System.out.println("move:" + y_move);
if (move_top) {
//顶部反弹
this.setPadding(0, (int) y_move, 0, 0);
//System.out.println("padding_top:" + y_move);
} else if (move_bottom) {
//底部反弹
int abs = (int)Math.abs(y_move);
// System.out.println("padding_bottom:"+abs);
this.setPadding(0, 0, 0,abs );
MarginLayoutParams layoutParams = (MarginLayoutParams) listView
.getLayoutParams();
layoutParams.topMargin = (int) y_move;
listView.setLayoutParams(layoutParams);
}
invalidate();
break;
case MotionEvent.ACTION_UP:
//System.out.println("up/////////");
if (move_top) {
TranslateAnimation translateAnimation = new TranslateAnimation( 0, 0,y_move, 0);
translateAnimation.setDuration(200);
translateAnimation.setFillAfter(true);
translateAnimation.setInterpolator(new LinearInterpolator());
//translateAnimation.start();
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ListViewFantan.this.setPadding(0, 0, 0, 0);
}
});
listView.startAnimation(translateAnimation);
} else if (move_bottom) {
TranslateAnimation translateAnimation = new TranslateAnimation( 0, 0,y_move, 0);
translateAnimation.setDuration(200);
translateAnimation.setFillAfter(true);
translateAnimation.setInterpolator(new LinearInterpolator());
//translateAnimation.start();
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ListViewFantan.this.setPadding(0, 0, 0, 0);
MarginLayoutParams layoutParams = (MarginLayoutParams) listView
.getLayoutParams();
layoutParams.topMargin = 0;
listView.setLayoutParams(layoutParams);
}
});
listView.startAnimation(translateAnimation);
}
invalidate();
move_top = false;
move_bottom = false;
break;
default:
break;
}
return super.onTouchEvent(ev);
}
private ListView listView;
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View childAt = getChildAt(0);
if (childAt instanceof ListView) {
listView = (ListView) childAt;
}
}
}
原文:http://blog.csdn.net/wuxifu001/article/details/39576613
内容总结
以上是互联网集市为您收集整理的ListView反弹全部内容,希望文章能够帮你解决ListView反弹所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。