水平滚动条+箭头提示

public class ExtendedHorizontalScrollView extends HorizontalScrollView {

private IScrollStateListener scrollStateListener;

public HorizontalScrollViewForMenu(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public HorizontalScrollViewForMenu(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public HorizontalScrollViewForMenu(Context context) {
    super(context);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    prepare();
}

private void prepare() {
    if (scrollStateListener != null) {
        View content = this.getChildAt(0);
        if (content.getLeft() >= 0)
            scrollStateListener.onScrollMostLeft();
        if (content.getLeft() < 0)
            scrollStateListener.onScrollFromMostLeft();

        if (content.getRight() <= getWidth())
            scrollStateListener.onScrollMostRight();
        if (content.getLeft() > getWidth())
            scrollStateListener.onScrollFromMostRight();
    }
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    super.onScrollChanged(l, t, oldl, oldt);
    if (scrollStateListener != null) {
        if (l == 0)
            scrollStateListener.onScrollMostLeft();
        else if (oldl == 0)
            scrollStateListener.onScrollFromMostLeft();
        int mostRightL = this.getChildAt(0).getWidth() - getWidth();
        if (l >= mostRightL)
            scrollStateListener.onScrollMostRight();
        if (oldl >= mostRightL && l < mostRightL)
            scrollStateListener.onScrollFromMostRight();
    }
}

public void setScrollStateListener(IScrollStateListener listener) {
    scrollStateListener = listener;
}

public interface IScrollStateListener {
    void onScrollMostLeft();

    void onScrollFromMostLeft();

    void onScrollMostRight();

    void onScrollFromMostRight();
}
}


布局

<LinearLayout
      .....>
    <ImageView
        android:id="@+id/navigation_left"
        ..... />

    <your.custom.view.package.ExtendedHorizontalScrollView
        android:id="@+id/scroller"
        android:layout_width="0px"
        android:layout_weight="1"
        android:fadingEdge="none"
                ....>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </your.custom.view.package.ExtendedHorizontalScrollView>

    <ImageView
        android:id="@+id/navigation_right"
        ..... />

</LinearLayout>


当不能水平滚动的时候,给箭头添加以下逻辑

((ExtendedHorizontalScrollView)findViewById(R.id.scroller)).setScrollStateListener(new IScrollStateListener() {
        public void onScrollMostRight() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.INVISIBLE);
        }

        public void onScrollMostLeft() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.INVISIBLE);
        }

        public void onScrollFromMostLeft() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_left).setVisibility(View.VISIBLE);
        }

        public void onScrollFromMostRight() {
            ((View) scroller.getParent()).findViewById(R.id.navigation_right).setVisibility(View.VISIBLE);
        }

    });

此文转载: http://ask.csdn.net/questions/546?sort=latest
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值