自定义ScrollView 滑动改变背景颜色

本文介绍了如何自定义一个ScrollView,使其在滑动过程中能够改变背景颜色。关键点在于确保ScrollView内容足够多以便滑动,并通过重写相关方法来实现颜色变化的效果。
摘要由CSDN通过智能技术生成


//实现自定义ScrollView 滑动改变背景颜色 

首先要自定义一个ScrollView

切记一定要能滑动,也就是说ScrollView 里面的东西要多,不然不好使.

自定义

public class JiChengShi extends ScrollView {
    public JiChengShi(Context context) {
        this(context,null);
    }

    public JiChengShi(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public JiChengShi(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public interface ScrollViewListener{
        void onScrollChanged(JiChengShi jiChengShi,int l, int t, int oldl, int oldt);
    }

    private ScrollViewListener mscrollViewListener;
    //向外提供一个方法
    public void setOnScrollChange(ScrollViewListener scrollViewListener){
        mscrollViewListener=scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(mscrollViewListener!=null){
            mscrollViewListener.onScrollChanged(this,l, t, oldl, oldt);
        }
    }
}
//布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <com.bwie.jd_2.Zidingyi.JiChengShi
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.stx.xhb.xbanner.XBanner
                android:id="@+id/banner"
                android:layout_width="match_parent"
                android:layout_height="250px"
                app:AutoPlayTime="2000"
                app:pointNormal="@drawable/dot_no_select"
                app:pointSelect="@drawable/dot_select"
                app:pointsContainerBackground="#44AAAAAA"
                app:pointsPosition="CENTER"
                app:tipTextColor="#F
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值