标签:android xml android listview java
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">因为ScrollView本身没有带onScrollListener的监听回调事件,所以,首先要重写ScrollView,下面给出代码</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">MyScrollView.java</span></span>
<span style="font-size:14px;">import android.content.Context; import android.os.Handler; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ScrollView; public class MyScrollView extends ScrollView { private OnScrollListener onScrollListener; //用在用户手指离开MyScrollView,MyScrollView还在继续滑动,用来保存Y的距离 private int lastScrollY; public MyScrollView(Context context) { this(context, null); } public MyScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } //写一个onScrollListener的监听回调方法 public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } //用于用户手指离开MyScrollView的时候获取MyScrollView滚动的Y距离,然后回调给onScroll方法中 private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { int scrollY = MyScrollView.this.getScrollY(); //此时的距离和记录下的距离不相等,在隔5毫秒给handler发送消息 if(lastScrollY != scrollY){ lastScrollY = scrollY; handler.sendMessageDelayed(handler.obtainMessage(), 5); } if(onScrollListener != null){ onScrollListener.onScroll(scrollY); } }; }; /** * 重写onTouchEvent, 当用户的手在MyScrollView上面的时候, * 直接将MyScrollView滑动的Y方向距离回调给onScroll方法中,当用户抬起手的时候, * MyScrollView可能还在滑动,所以当用户抬起手我们隔5毫秒给handler发送消息,在handler处理 * MyScrollView滑动的距离 */ @Override public boolean onTouchEvent(MotionEvent ev) { if(onScrollListener != null){ onScrollListener.onScroll(lastScrollY = this.getScrollY()); } switch(ev.getAction()){ case MotionEvent.ACTION_UP: handler.sendMessageDelayed(handler.obtainMessage(), 5); break; } return super.onTouchEvent(ev); } //滚动的回调接口 public interface OnScrollListener{ //返回滑动的Y的距离 public void onScroll(int scrollY); } } </span>下面是主程序中的部分代码
首先在XML文件中加入MyScrollView控件
<span style="font-size:14px;"><com.scnu.yxp.travelapp.view.MyScrollView <span style="white-space:pre"> </span>android:id="@+id/scrollView" <span style="white-space:pre"> </span>android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" /> </span>第二步写一个类继承MyScrollView里头的回调接口
<span style="font-size:14px;">class onMyScrollListener implements com.scnu.yxp.travelapp.view.MyScrollView.OnScrollListener { @Override public void onScroll(int scrollY) { //当滑动的距离大于多少时执行相应的动作 if(scrollY >= homepage_btn_window.getHeight()) { upBtn.setVisibility(View.VISIBLE); }else{ upBtn.setVisibility(View.GONE); } } }</span>
第三步,设置监听器
<span style="font-size:14px;"><span style="white-space:pre"> </span>onMyScrollListener listener = new onMyScrollListener(); myScrollView.setOnScrollListener(listener);</span>到这里就可以了
标签:android xml android listview java
原文地址:http://blog.csdn.net/u010360371/article/details/41209729