android listview下滑出现回到顶部的按钮

<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>
到这里就可以了
下面上个图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值