监听HorizontalScrollView滑动到最左/最右

本文介绍了如何定制HorizontalScrollView以监听其滑动停止后的位置,包括判断是否在最左侧或最右侧。通过自定义实现,可以精确掌握滚动状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

监听ScrollView滑动停止后,滚动条的位置,是在最左?最右?其它地方?

1,先定制HorizontalScrollView

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

public class ScrollViewCustom extends HorizontalScrollView
{
	private Runnable scrollerTask;
	private int intitPosition;
	private int newCheck = 100;
	private int childWidth = 0;

	public interface OnScrollStopListner
	{
		/**
		 * scroll have stoped
		 */
		void onScrollStoped();
		/**
		 * scroll have stoped, and is at left edge
		 */
		void onScrollToLeftEdge();
		/**
		 * scroll have stoped, and is at right edge
		 */
		void onScrollToRightEdge();
		/**
		 * scroll have stoped, and is at middle
		 */
		void onScrollToMiddle();
	}

	private OnScrollStopListner onScrollstopListner;

	public ScrollViewCustom(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		scrollerTask = new Runnable()
		{
			@Override
			public void run()
			{
				int newPosition = getScrollX();
				if (intitPosition - newPosition == 0)
				{
					if(onScrollstopListner == null)
					{
						return;
					}
					onScrollstopListner.onScrollStoped();
					Rect outRect = new Rect();
					getDrawingRect(outRect);
					if(getScrollX() == 0)
					{
						onScrollstopListner.onScrollToLeftEdge();
					}
					else if(childWidth + getPaddingLeft() + getPaddingRight() == outRect.right)
					{
						onScrollstopListner.onScrollToRightEdge();
					}
					else
					{
						onScrollstopListner.onScrollToMiddle();
					}
				} else
				{
					intitPosition = getScrollX();
					postDelayed(scrollerTask, newCheck);
				}
			}
		};
	}
	

	public void setOnScrollStopListner(OnScrollStopListner listner)
	{
		onScrollstopListner = listner;
	}

	public void startScrollerTask()
	{
		intitPosition = getScrollX();
		postDelayed(scrollerTask, newCheck);
		checkTotalWidth();
	}
	private void checkTotalWidth()
	{
		if(childWidth > 0)
		{
			return;
		}
		for(int i = 0; i < getChildCount(); i++)
		{
			childWidth += getChildAt(i).getWidth();
		}
	}
}

2,使用

    protected void onCreate(Bundle savedInstanceState)
    {
        final ScrollViewCustom scrollView;
        scrollView.setOnTouchListener(new OnTouchListener()
        {
            public boolean onTouch(View v, MotionEvent event)
            {
                if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    scrollView.startScrollerTask();
                }
                return false;
            }
        });
        scrollView.setOnScrollStopListner(new OnScrollStopListner()
        {
            public void onScrollToRightEdge()
            {
            }
            public void onScrollToMiddle()
            {
            }
            public void onScrollToLeftEdge()
            {
            }
            public void onScrollStoped()
            {
            }
        });
    } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值