自定义 scrollview 标头部分的滑动速度慢(scrollview 子控件滑动速度不一致)

2023-10-27

scrollview 子控件滑动速度不一致
先来个布局图:
这里写图片描述
向上滑动时,图片向上划出的速度较下面的蓝色view慢。
现在我们先来看布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context=".MainActivity" >

    <com.example.testscrollview.Myscrollview
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

            <RelativeLayout
                android:id="@+id/layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent" >

                <ImageView

                    android:layout_width="match_parent"
                    android:layout_height="220dp"
                    android:background="@drawable/img" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_below="@id/layout"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:background="#acfcfc" />
            </LinearLayout>
        </RelativeLayout>
    </com.example.testscrollview.Myscrollview>

</LinearLayout>

注意要想实现imageview滑动速度比下面的慢 imageview要在外面套一层布局,因为scrollto()方法只是让布局里面的内容滑动。
再来看自定义的的scrollview:

package com.example.testscrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class Myscrollview extends ScrollView {

     private ScrollviewListener scrollViewListener = null;  

    public Myscrollview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public Myscrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public Myscrollview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void computeScroll() {
        // TODO Auto-generated method stub
        super.computeScroll();
    }

    @Override
    public void fling(int velocityY) {

        super.fling(velocityY);
    }

      public void setScrollViewListener(ScrollviewListener scrollViewListener) {  
            this.scrollViewListener = scrollViewListener;  
        }  


      @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        // TODO Auto-generated method stub
        //super.onScrollChanged(x,y,oldx,oldy);

        if(scrollViewListener != null) {  
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);  
        }  
    }
}

以及自定义监听接口:

package com.example.testscrollview;

public interface ScrollviewListener {
     void onScrollChanged(Myscrollview scrollView, int x, int y, int oldx, int oldy);  
}

接下来就是main了,在这里重新滑动方法:

package com.example.testscrollview;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class MainActivity extends Activity implements ScrollviewListener{
Myscrollview scrollView;
ImageView imageView;
RelativeLayout layout;
//Myscrollview scrollView_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollView=(Myscrollview) findViewById(R.id.scrollview);
     //   scrollView_img=(Myscrollview) findViewById(R.id.scrollview_img);
        scrollView.setScrollViewListener(this);

       // scrollView_img.setScrollViewListener(this);
        layout=(RelativeLayout) findViewById(R.id.layout);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onScrollChanged(Myscrollview scrollView1, int x, int y,
            int oldx, int oldy) {
        Log.e("fc", x+"  "+y);
            layout.scrollTo(x, -y/3);   //注意这里是负的 否则比下面的快

        /* else if(scrollView1 == scrollView_img) {  
            scrollView.scrollTo(x, y*3);  
        }  */

    }

}

源码:下载

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义 scrollview 标头部分的滑动速度慢(scrollview 子控件滑动速度不一致) 的相关文章

随机推荐

  • mysql 字段拼接_mysql 字符串拼接,你知道几种方式?

    第一种 mysql自带语法CONCAT string1 string2 此处是直接把string1和string2等等的字符串拼接起来 无缝拼接哦 说明 此方法在拼接的时候如果有一个值为NULL 则返回NULL 如 1 SELECT CON
  • shell入门第三课 case语句

    虽然if elif语言可以做多项选择 但是使用case在有大量选择的情况下 更为合理 case语句与C语言的case有些相似 可以根据条件选择对应的语句执行 1 形式 case语句 case 变量 in 模式11 模式12 表达式 模式21
  • 区块链节点和区块区别_《区块链百问百答》-区块链的节点竞选,到底是什么...

    上期讲解的区块链在医疗领域的应用 你看了吗 本期视频将解答大家关于节点竞选的困惑 现在跟随由LemoChain策划的 区块链百问百答 来认识一下吧 https www zhihu com video 1139492556072271872
  • 删除字符串最后一个字符的几种方法

    偶然看到的 记录一下 以免忘记 字符串 string s 1 2 3 4 5 目标 删除最后一个 方法 1 用的最多的是Substring 这个也是我一直用的 s s Substring 0 s Length 1 2 用 RTrim 这个我
  • 网络安全-php安全知识点

    目录 语法与注释 输出 变量 弱类型安全 超级全局常量 函数 常用 字符串相关 正则表达式 子字符串位置 数据库相关 mysqli pdo 伪协议相关 反序列化漏洞 serialize函数 unserialize函数 魔法函数 举例 写给和
  • MYSQL——解决多维度随机组合查询场景:grouping sets函数

    一 引入 注意 通常用在构建数据集市和复杂随机组合场景查询时使用 对于经常需要对数据进行多维度的聚合分析的场景 您既需要对A列做聚合 也要对B列做聚合 同时要对A B两列做聚合 因此需要多次使用union all 案例 比如此处有一张表te
  • C++算法之快速排序

    C 算法之快速排序 文章目录 C 算法之快速排序 一 快速排序引出 二 快排步骤 三 代码实现 四 复杂度分析 一 快速排序引出 我们知道 给一个长度为n的序列排序 有三种很简单的算法 选择排序 冒泡排序 插入排序 这三种算法的复杂度均为O
  • SpingBoot集成Swagger和Knife4j

    1 项目开发中编写接口文档实在是痛苦 于是打算引入swagger 之后发现swagger不够舒服 便又引入knife4j springboot的相关依赖不再贴 下面是swagger和knife4j的相关依赖 版本有需求的可以上maven中央
  • 百度群组链接分享 - 铁人圈子

    百度网盘群组链接分享 铁人圈子 铁人圈子 tierenpan com 是一个分享百度网盘群组的发布平台 可以在铁人圈子上实时分享你的群组链接 并且和其他网友互动交流分享资源 群组分享 百度群组链接分享 地址 https www tieren
  • shape文件格式简单说明

    一 简介 ShapeFile是Esri开发的一种空间数据格式 她是一种矢量数据存储格式 通常用于描述几何体形状 点 线 面 及相关属性 二 组成说明 2 1 必须文件 shp 图形格式 用于描述几何体形状 shx 图形索引格式 记录每一个几
  • 一文带你彻底搞懂二叉树(Python实现)——真香

    文章目录 前言 树 树中的一些术语 树的分类 树的存储表示 二叉树 二叉树的性质 二叉树的实现 二叉树结点的实现 二叉树的创建 广度优先遍历 先序 中序 后序遍历 后记 前言 终于到了数据结构中的关键部分了 二叉树 说起二叉树啊 简直是我当
  • PTA 浙大版《C语言程序设计(第4版)》题目集 习题4-6 水仙花数

    原题链接 问题描述 水仙花数是指一个N位正整数 N 3 它的每个位上的数字的N次幂之和等于它本身 例如 153 13 53 33 本题要求编写程序 计算所有N位水仙花数 输入格式 输入在一行中给出一个正整数N 3 N 7 输出格式 按递增顺
  • abp Application层获取请求的Header内容

    abp 如何在应用层返回header自定义的内容 参考 https blog csdn net u012659600 article details 99579369 首先在AppService中注入HttpContextAccessor
  • Ubuntu 更新 CMake 版本

    项目中有时候会出现CMake版本小于最低要求的情况 实际上没有有必要这么高的要求 但是在不能改对方代码的情况下 只能去升级自身的版本了 尝试了网上说的直接update之后再次安装的方式 结果版本号没有改变 sudo apt get upda
  • css文字超出隐藏显示...

    单行 overflow hidden white space nowrap text overflow ellipsis 多行 display webkit box webkit box orient vertical webkit lin
  • Windows10开启Hyper-v并安装Linux CentOS虚拟机

    Windows10开启Hyper v虚拟机配置静态网络 1 Windows10 开启Hyper v 右键单击 Windows 按钮并选择 应用和功能 选择相关设置下右侧的 程序和功能 选择 打开或关闭 Windows 功能 选择 Hyper
  • Element 标签页样式修改

    deep el tabs nav wrap after height 1px deep el tabs item height 50px font size 16px font family PingFang SC font weight
  • Java编程实现Softmax函数功能

    Java编程实现Softmax函数功能 Softmax函数是一种常用的数学函数 广泛应用于机器学习和深度学习领域 尤其在分类问题中起到重要作用 本文将介绍如何使用Java编程实现Softmax函数 并提供相应的源代码 首先 我们来了解一下S
  • centos7启动docker: dial tcp 104.18.123.25:443: i/o timeout.

    在centos7上安装好了docker之后 测试docker是否安装成功 使用官方给出的sudo docker run hello world 解决 再运行一遍命令即可
  • 自定义 scrollview 标头部分的滑动速度慢(scrollview 子控件滑动速度不一致)

    scrollview 子控件滑动速度不一致 先来个布局图 向上滑动时 图片向上划出的速度较下面的蓝色view慢 现在我们先来看布局文件