自定义ScrollView 实现滚动时切换标题内容

本文介绍如何在Android中为ScrollView自定义滚动监听接口,实现根据滚动位置改变顶部标题的功能。通过创建SwitchScrollViewListener接口并结合Activity布局实现,适用于需要根据不同滚动位置显示不同标题的应用场景。

项目中有一个这样的要求 一个页面是要求嵌套在ScrollView中 比如某公司的详情页面 

 

刚刚打开时顶部标题显示公司详情。当上拉到一定程度即公司职位已经滑出屏幕。此时顶部标题显示职位详情

 

 

代码说明

由于 ScrollView 只提供了一个这样的监听方法 

protected void onScrollChanged(int x, int y, int oldx, int oldy)

显然这个方法是不能被外界调用的,因此就需要把它暴露出去。

 

 

自定义接口

public interface SwitchScrollViewListener {

    void onScrollChanged(SwitchScrollView scrollView, int x, int y, int oldx, int oldy);

}

 

 

Activity布局

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

    <TextView
        android:id="@+id/activity_switchscrollview_titletextview"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:background="@color/colorPrimary"
        android:text="职位详情"/>

    <com.wjn.myview.view.SwitchScrollView
        android:id="@+id/activity_switchscrollview_switchscrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/activity_switchscrollview_contexttextview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:layout_margin="10dp"
                android:text=""/>

        </LinearLayout>

    </com.wjn.myview.view.SwitchScrollView>

</LinearLayout>

 

 

效果

 

 

 

完整代码:https://github.com/wujianning/CustomView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值