首页 > 其他分享 >ScrollView中浮动条的实现

ScrollView中浮动条的实现

时间:2023-04-06 21:35:56浏览次数:42  
标签:浮动 实现 ScrollView int import android public ObservableScrollView


简单的浮窗
http://www.open-open.com/lib/view/open1467185415741.html


ScrollView中如果内容比较长,往下拉的时候有一部分(通常是菜单)View就一直固定在屏幕顶端,像个浮动条一样,该效果Web页面使用比较多。
实现这种效果需要重写ScrollView的onScrollChanged(),具体如下:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

/**
 * 带浮动条的ScrollView
 * 
 * 
 */
public class ObservableScrollView extends ScrollView {
	private OnScrollListener onScrollListener = null;

	private View viewInScroll,viewOutScroll;
	public ObservableScrollView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public ObservableScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ObservableScrollView(Context context) {
		super(context);
	}

	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}

	@Override
	protected void onScrollChanged(int x, int y, int oldx, int oldy) {
		super.onScrollChanged(x, y, oldx, oldy);
		if (onScrollListener != null) {
			onScrollListener.onScrollChanged(this, x, y, oldx, oldy);
		}
		computeFloatIfNecessary();
	}

	/**
	 * 监听ScrollView滚动接口
	 * @author reyo
	 *
	 */
	public interface OnScrollListener {

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

	}
	
	/**
	 * 设置需要浮动的View
	 * @param viewInScroll ScollView内的view
	 * @param viewFloat ScollView外的view,真正需要浮动的view
	 */
	public void setFloatView(View viewInScroll,View viewOutScroll){
		this.viewInScroll=viewInScroll;
		this.viewOutScroll=viewOutScroll;
	}
	
	private void computeFloatIfNecessary(){
		if(viewInScroll==null&&viewOutScroll==null){
			return;
		}
		// 获取ScrollView的x,y坐标
		int[] location = new int[2];
		this.getLocationInWindow(location);
		// 获取浮动View的x,y坐标
		int[] loc = new int[2];
		viewInScroll.getLocationOnScreen(loc);
		// 当浮动view的y <= ScrollView的y坐标时,把固定的view显示出来
		if (loc[1] <= location[1]) {
			// 处理一下把原有view设置INVISIBLE,这样显示效果会好点
			viewOutScroll.setVisibility(View.VISIBLE);
			viewInScroll.setVisibility(View.INVISIBLE);
		} else {
			// 记得还原回来
			viewOutScroll.setVisibility(View.GONE);
			viewInScroll.setVisibility(View.VISIBLE);
		}
	}
}




用法:


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.reyo.view.ObservableScrollView;
public class FloatActivity extends Activity{

	private ObservableScrollView scrollView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_float);
		scrollView = (ObservableScrollView) findViewById(R.id.scrollView);
		scrollView.setFloatView(findViewById(R.id.viewInScroll), findViewById(R.id.viewOutScroll));
		scrollView.setOnScrollListener(new com.reyo.view.ObservableScrollView.OnScrollListener() {

			@Override
			public void onScrollChanged(ObservableScrollView scrollView, int x,
					int y, int oldx, int oldy) {
				Log.i("tag", "y="+y+";oldy="+oldy);
			}
		});

	}

}



界面:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <com.reyo.view.ObservableScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

            <ImageView
                android:id="@+id/img"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/pic" />

            <LinearLayout
                android:id="@+id/viewInScroll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button0"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="排队叫号" />

                <Button
                    android:id="@+id/button1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="预定桌位" />

            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:padding="5dp"
                android:text="@string/test1"
                android:textColor="#555555"
                android:textSize="20dip" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:padding="5dp"
                android:text="@string/test2"
                android:textColor="#555555"
                android:textSize="20dip" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:padding="5dp"
                android:text="@string/test3"
                android:textColor="#555555"
                android:textSize="20dip" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:padding="5dp"
                android:text="@string/test4"
                android:textColor="#555555"
                android:textSize="20dip" />
        </LinearLayout>
    </com.reyo.view.ObservableScrollView>

    <FrameLayout
        android:id="@+id/viewOutScroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:visibility="gone" >

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

            <Button
                android:id="@+id/button0"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="排队叫号" />

            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="预定桌位" />

        </LinearLayout>
    </FrameLayout>
</RelativeLayout>




测试时尽量让ScrollView超过一屏,好让其滑动,这样才能看出效果。




Android 仿美团网,大众点评购买框悬浮效果之修改版




一个可以滚动监听的类,这下可以根据自己的需求任意扩展了


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

/**
 * A custom ScrollView that can accept a scroll listener.
 */
public class ObservableScrollView extends ScrollView {
    private Callbacks mCallbacks;

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mCallbacks != null) {
            mCallbacks.onScrollChanged(t);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (mCallbacks != null) {
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    mCallbacks.onDownMotionEvent();
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mCallbacks.onUpOrCancelMotionEvent();
                    break;
            }
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public int computeVerticalScrollRange() {
        return super.computeVerticalScrollRange();
    }

    public void setCallbacks(Callbacks listener) {
        mCallbacks = listener;
    }

    public static interface Callbacks {
        public void onScrollChanged(int scrollY);
        public void onDownMotionEvent();
        public void onUpOrCancelMotionEvent();
    }
}




如何解决ViewPager在ScrollView中滑动经常失效、无法正常滑动问题?


解决方法只需要在接近水平滚动时ScrollView不处理事件而交由其子View(即这里的ViewPager)处理即可,重写ScrollView的onInterceptTouchEvent函数,如下:


package cc.newnews.view;

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

public class VerticalScrollView extends ScrollView {

	private GestureDetector mGestureDetector;

	public VerticalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mGestureDetector = new GestureDetector(context, new YScrollDetector());
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		return super.onInterceptTouchEvent(ev)
				&& mGestureDetector.onTouchEvent(ev);
	}

	class YScrollDetector extends SimpleOnGestureListener {

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			/**
			 * 如果我们滚动更接近水平方向,返回false,让子视图来处理它
			 */
			return (Math.abs(distanceY) > Math.abs(distanceX));
		}
	}
}



再将xml中的ScrollView改为<xxx.xxx.xxx.VerticalScrollView>即包名.重写的ScrollView的类名)即可。


本方法同样适用于ScrollView中ListView等其他View无法滚动。



Android UI开发第四十篇——ScrollTricks介绍



Android-ObservableScrollView


https://github.com/ksoichiro/Android-ObservableScrollView


标签:浮动,实现,ScrollView,int,import,android,public,ObservableScrollView
From: https://blog.51cto.com/u_5454003/6174168

相关文章

  • vue之过滤、筛选功能的实现
    目录需求代码需求给定一个列表(模拟数据),根据用户输入,自动筛选输入的内容并输出到屏幕代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title><scriptsrc="./js/jQuery.js"></......
  • 懒加载的Scrollview
    要实现一个功能:当Scrollview滑动到最底端的时候需要触发事件加载其他数据。很多人都以为ScrollView可以像ListViev那样setOnScrollListener,其实沒那么简单,因为ScrollView压根就没有该接口,在baidu上兜了一圈没有找到合适的答案,没办法只能google去了,居然一下子......
  • 储能控制器,simulink仿真模型。 采用下垂控制实现蓄电池超级电容构成的混合储能功率分
    储能控制器,simulink仿真模型。采用下垂控制实现蓄电池超级电容构成的混合储能功率分配、SOC均衡控制、考虑线路阻抗情况下提高电流分配精度控制、母线电压补控制。YID:2580685477458200......
  • 蚁群算法 Dijkstra算法 遗传算法 人工势场法实现二维 三维空间路径规划
    【改进蚁群算法】蚁群算法Dijkstra算法遗传算法人工势场法实现二维三维空间路径规划本程序为改进蚁群算法+Dijkstra算法+MAKLINK图理论实现的二维空间路径规划算法实现:1)基于MAKLINK图理论生成地图,并对可行点进行划分;2)用Dijkstra算法实现次优路径的寻找;3)在Dijkstra算法......
  • FolderBrowserDialog类实现选择打开文件
    privatevoidbutton1_Click(objectsender,EventArgse){FolderBrowserDialogdilog=newFolderBrowserDialog();dilog.Description="请选择文件夹";if(dilog.ShowDialog()==DialogResult.OK||dilog.ShowDialo......
  • rsyslog实现日志的集中管理
    配置远程日志服务器->实现日志的集中管理环境:两台服务器server端client端1)server端配置[root@centos7-xinsz08~]#vim/etc/rsyslog.conf#ProvidesTCPsyslogreception$ModLoadimtcp$InputTCPServerRun514重启[root@centos7-xinsz08~]#systemctlrestartrsyslog3......
  • uni-app 滚动通知组件的实现
    uni-app滚动通知组件的实现一、实现思路二、使用scroll-view组件实现<template><viewclass="notice"><scroll-viewclass="notice-scroll":scroll-y="true":scroll-with-animation="true":scroll-t......
  • keepalived+MySQL实现高可用
    (一)keepalived概述Keepalived通过VRRP(虚拟路由冗余协议)协议实现虚拟IP的漂移。当master故障后,VIP会自动漂移到backup,这时通知下端主机刷新ARP表,如果业务是通过VIP连接到服务器的,则此时依然能够连接到正常运行的主机,RedHat给出的VRRP工作原理如下图: 本来对VIP漂移有一定了解的......
  • c#.net怎么实现web端上传超大文件
    ​ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现。下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压。ASP.NET页面设计:TextBox和Button按钮。​编辑TextBox中需要自己受到输入文件夹的路径(包含文件夹),通过Button实......
  • OneDrive实现电脑文件分享与协同
      本文介绍基于OneDrive网盘实现电脑大文件共享、协同办公的方法。1前言  作为网盘的重度用户,在学习、工作、生活中可以说少不了与各类云盘打交道。在这一过程中,也慢慢了解到不同网盘软件的特点,从而逐渐结合其各自的特点,在不同的应用场合选择不同的网盘软件。  在日常运......