安卓ScrollView向上滑动控件顶部悬浮效果实现
发布日期:2021-07-01 04:34:24 浏览次数:2 分类:技术文章

本文共 3516 字,大约阅读时间需要 11 分钟。

文章目录

效果图

如果你要的不是以下的效果,请停止浏览文章,不要浪费时间!

在这里插入图片描述

从上打下动漫依次是:《东京喰种》《魁拔》《海贼王》《名侦探柯南》《学院默示录》《你的名字》《神兵小将》《铁臂阿童木》《猫和老鼠》《虹猫蓝兔七侠传》《太空历险记》《洛洛历险记》《超兽武装》《风云决》这些都是我非常喜欢的动漫

实现思路

重写ScrollView中的onScrollChanged方法,通过接口回调计算滑动距离,控制控件的显示隐藏达到这种效果,在布局中,注意魁拔那张图片(第二张),其实在布局中存在两个完全一样的这张图片,这时候ScrollView滑动便会通过接口,计算出滑动距离,当滑动距离大于这张图高度时候,便会出现,反之隐藏!

代码布局、逻辑


布局文件

给出简单的布局


自定义ViewNorthernScrollViewListener.java

继承自ScrollView重写onScrollChanged方法

package com.northernbrain.myapplication;import android.content.Context;import android.util.AttributeSet;import android.widget.ScrollView;public class NorthernScrollView extends ScrollView {
private NorthernScrollViewListener scrollViewListener = null; public void setScrollViewListener(NorthernScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener; } public NorthernScrollView(Context context) {
super(context); } public NorthernScrollView(Context context, AttributeSet attrs) {
super(context, attrs); } public NorthernScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt); if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt); } } public interface NorthernScrollViewListener {
void onScrollChanged(NorthernScrollView scrollView, int x, int y, int oldx, int oldy); }}

MainActivity.java

在此要实现NorthernScrollView.NorthernScrollViewListener这个接口

package com.northernbrain.myapplication;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewTreeObserver;import android.widget.ImageView;public class MainActivity extends AppCompatActivity implements NorthernScrollView.NorthernScrollViewListener {
private NorthernScrollView northernScrollView; private ImageView title; private ImageView view1; int height; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实力化控件 initView(); //计算控件高度 getHetght(); } private void getHetght() {
ViewTreeObserver vto = view1.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
height = view1.getHeight(); northernScrollView.setScrollViewListener(MainActivity.this); } }); } private void initView() {
northernScrollView = (NorthernScrollView) findViewById(R.id.northernScrollView); title = (ImageView) findViewById(R.id.title); view1 = (ImageView) findViewById(R.id.view1); } @Override public void onScrollChanged(NorthernScrollView scrollView, int x, int y, int oldx, int oldy) {
if (y <= height) {
title.setVisibility(View.GONE); } else {
title.setVisibility(View.VISIBLE); } }}

转载地址:https://myhub.blog.csdn.net/article/details/93236601 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:修改Tomcat服务器启动图标
下一篇:去除IDEA黄色曲线警告线

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月25日 22时57分14秒