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

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

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


//实现自定义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="#FFFFFFFF"
                app:pointTopBottomPadding="10dip"
                ></com.stx.xhb.xbanner.XBanner>
'

            <LinearLayout
                android:background="#F5F5F5"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="250px">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/xlv"
                    android:layout_width="match_parent"
                    android:layout_height="200px"></android.support.v7.widget.RecyclerView>

                 <include layout="@layout/f1_paomadeng"></include>
            </LinearLayout>

             <include layout="@layout/f1_daojishi"></include>

            <android.support.v7.widget.RecyclerView
                  android:layout_marginBottom="3dp"
                  android:id="@+id/miaosha"
                  android:layout_width="match_parent"
                  android:layout_height="150px"></android.support.v7.widget.RecyclerView>

            <include layout="@layout/f1_tuijian"></include>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/tese"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

        </LinearLayout>
    </com.bwie.jd_2.Zidingyi.JiChengShi>

    <RelativeLayout
        android:id="@+id/layout_title"
        android:layout_width="match_parent"
        android:layout_height="60px"
        android:layout_gravity="top"
        android:padding="5dp">

        <include layout="@layout/f1_sousuo"></include>
    </RelativeLayout>

</RelativeLayout>
//搜索框  f1_sousuo

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_alignParentTop="true"
    android:layout_marginTop="5px"
    android:id="@+id/f1_frag"
    android:layout_width="wrap_content"
    android:layout_height="50px">

    <View
        android:id="@+id/home_title_bar_bg_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:id="@+id/f1_erweima"
            android:layout_width="50dp"
            android:layout_height="wrap_content">

            <ImageView
                android:src="@drawable/sao1"
                android:layout_width="20dp"
                android:layout_height="20dp" />
            <TextView
                android:id="@+id/sou_tv1"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:text="扫啊扫"/>
        </LinearLayout>

        <LinearLayout
            android:layout_marginLeft="3px"
            android:layout_marginRight="3px"
            android:background="@drawable/home_title_bar_search_corner_bg"
            android:gravity="center_vertical"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="40dp">

            <ImageView
                android:layout_marginLeft="5dp"
                android:src="@drawable/order_find"
                android:layout_width="20dp"
                android:layout_height="20dp" />

            <TextView
                android:layout_marginLeft="5dp"
                android:gravity="center_vertical"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="30dp"
                android:textColor="@color/white"
                android:textSize="15dp"
                android:lines="1"
                android:ellipsize="end"
                android:text="运动户外超级品牌类日 跨店铺跨店铺"
                />
            <ImageView
                android:layout_marginRight="5dp"
                android:src="@drawable/root"
                android:layout_width="20dp"
                android:layout_height="20dp" />
        </LinearLayout>

        <LinearLayout
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:layout_width="50dp"
            android:layout_height="wrap_content">

            <ImageView
                android:src="@drawable/order_msg"
                android:layout_width="20dp"
                android:layout_height="20dp" />
            <TextView
                android:id="@+id/sou_tv2"
                android:textColor="@color/white"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:text="消息"/>
        </LinearLayout>
    </LinearLayout>
</FrameLayout>
//调用
public class F1 extends BaseFragment implements F1View,JiChengShi.ScrollViewListener{
    private CountdownView mCvCountdownView;
    private F1iPresenter fp;
    private XBanner banner;
    private LunBoBean lunBoBean;
    private RecyclerView xlv;
    private RecyclerView miaosha;
    VerticalMarqueeView vv;
    private RecyclerView tese;
    private List<JiuGongGeBean.DataBean> data;
    private JiuGongGeBean jiuGongGeBean;
    private JiChengShi scrollView;
    private RelativeLayout layout_title;
    private FrameLayout frag;
    private int height;
    private TextView sou_tv2;
    private TextView sou_tv1;

    @Override
    public int bindLayout() {
        return R.layout.f1;
    }

    @Override
    public void initViews() {
        //轮播图
        banner = findViewById(R.id.banner);
        //中间人
        fp = new F1iPresenterImpl();
        xlv = findViewById(R.id.xlv);
        //跑马灯
        vv=findViewById(R.id.vv);
        //倒计时
        mCvCountdownView =findViewById(R.id.cdm);
        //秒杀
        miaosha= findViewById(R.id.miaosha);
        //特色推荐
        tese = findViewById(R.id.tese);
        scrollView = findViewById(R.id.scrollView);
        layout_title = findViewById(R.id.layout_title);
        frag = findViewById(R.id.f1_frag);
        sou_tv1 = findViewById(R.id.sou_tv1);
        sou_tv2 = findViewById(R.id.sou_tv2);
    }
    //请求数据
    @Override
    public void initData() {
         initListener();
         scrollView.setOnScrollChange(this);
         fp.PaoMaDengGeRen(new LunBoiModelImpl(),this);
         fp.LunBoRen(new LunBoiModelImpl(),this);
         fp.JiuGongGeRen(new LunBoiModelImpl(),this);
    }

    private void initListener() {
        //获取图片的高度  重点...
        /*
        * 不能直接用控件.getHeight
        * 因为在oncreate()方法中,控件还没有绘制出来,获取的高度为0
        * */
        final ViewTreeObserver viewTreeObserver =  frag.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                //在每次监听前remove前一次的监听,避免重复监听。
                frag.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                height = frag.getHeight();
            }
        });
    }

    //初始化数据
    @Override
    public void SuccessInit(LunBoBean lunBoBean) {
        this.lunBoBean=lunBoBean;
        //为XBanner绑定数据
        setBanner();
        //设置京东秒杀
        setMiaoSha();
        //设置倒计时
        setDaojishi();
        //设置特色推荐
        setTese();
    }
    //设置特色推荐
    private void setTese() {
        final List<LunBoBean.TuijianBean.ListBean> list = lunBoBean.getTuijian().getList();
        TeseAdaper ta=new TeseAdaper(list,context);
        tese.setLayoutManager(new GridLayoutManager(context,2, OrientationHelper.VERTICAL,false));
        tese.setAdapter(ta);
        ta.setJump(new JumpActivity() {
            @Override
            public void jumpXiangQing(int i) {
                String detailUrl = list.get(i).getDetailUrl();
                EventBus.getDefault().postSticky(detailUrl);
                startActivity(new Intent(context,GoodsXiangQing.class));
            }
        });
    }

    //设置倒计时
    private void setDaojishi() {
        int time = lunBoBean.getMiaosha().getTime();
        mCvCountdownView.start(time);
    }

    //设置京东秒杀
    private void setMiaoSha() {
    miaosha.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false));
        final List<LunBoBean.MiaoshaBean.ListBeanX> list = lunBoBean.getMiaosha().getList();
        miaoshaAdaper miaoshaAdaper = new miaoshaAdaper(list, context);
        miaosha.setAdapter(miaoshaAdaper);
        miaoshaAdaper.setJump(new JumpActivity() {
            @Override
            public void jumpXiangQing(int i) {
                String detailUrl = list.get(i).getDetailUrl();
                EventBus.getDefault().postSticky(detailUrl);
                startActivity(new Intent(context,GoodsXiangQing.class));
            }
        });
    }

    //设置九宫格
    @Override
    public void Jiu_SuccessInit(JiuGongGeBean jiuGongGeBean) {
        this.jiuGongGeBean=jiuGongGeBean;
        data = jiuGongGeBean.getData();
        Jiu_Adaper ja=new Jiu_Adaper(context, data);
        //设置布局管理器
        xlv.setLayoutManager(new GridLayoutManager(context,2, OrientationHelper.HORIZONTAL,false));
        xlv.setAdapter(ja);
        ja.diao(new Jiu_Adaper.Ondianji() {
            @Override
            public void diji(View view, int position) {
        Toast.makeText(context, "点击了第" + (position + 1) + "张图片", Toast.LENGTH_SHORT).show();
            }
        });
    }
     //展示跑马灯
    @Override
    public void Pao_SuccessInit(String[] pao) {
           vv.color(getResources().getColor(android.R.color.black))
                .textSize(sp2px(getActivity(), 15))
                .datas(pao).commit();
           vv.startScroll();
    }

    //请求失败
    @Override
    public void Error(String error) {
        Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
    }

    //为XBanner绑定数据
    public void setBanner() {
        banner.setData(lunBoBean.getData(),null);
        banner.setmAdapter(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, View view, int position) {
        Glide.with(context).load(lunBoBean.getData().get(position).getIcon()).into((ImageView) view);
            }
        });
        // 设置XBanner的页面切换特效
        banner.setPageTransformer(Transformer.Default);
        // XBanner中某一项的点击事件
        banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, int position) {
        Toast.makeText(context, "点击了第" + (position + 1) + "张图片", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        banner.stopAutoPlay();
    }

    @Override
    public void onScrollChanged(JiChengShi jiChengShi, int l, int t, int oldl, int oldt) {
        if(t<=0){
            layout_title.setBackgroundColor(Color.argb(0,0,0,0));
            sou_tv1.setTextColor(Color.WHITE);
            sou_tv2.setTextColor(Color.WHITE);
        }else if(t>0 && t<height){
            //获取ScrollView向下滑动图片消失的比例
            float scale = (float) t / height;
            //更加这个比例,让标题颜色由浅入深
            float v = 255 * scale;
            //设置标题内容颜色
            sou_tv1.setTextColor(Color.argb((int)v,0,0,0));
            sou_tv2.setTextColor(Color.argb((int)v,0,0,0));
            //设置标题布局的颜色
            layout_title.setBackgroundColor(Color.argb((int)v,255,255,255));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值