- 浏览: 5775136 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (890)
- WindowsPhone (0)
- android (88)
- android快速迭代 (17)
- android基础 (34)
- android进阶 (172)
- android高级 (0)
- android拾遗 (85)
- android动画&效果 (68)
- Material Design (13)
- LUA (5)
- j2me (32)
- jQuery (39)
- spring (26)
- hibernate (20)
- struts (26)
- tomcat (9)
- javascript+css+html (62)
- jsp+servlet+javabean (14)
- java (37)
- velocity+FCKeditor (13)
- linux+批处理 (9)
- mysql (19)
- MyEclipse (9)
- ajax (7)
- wap (8)
- j2ee+apache (24)
- 其他 (13)
- phonegap (35)
最新评论
-
Memories_NC:
本地lua脚本终于执行成功了,虽然不是通过redis
java中调用lua脚本语言1 -
ZHOU452840622:
大神://处理返回的接收状态 这个好像没有监听到 遇 ...
android 发送短信的两种方式 -
PXY:
拦截部分地址,怎么写的for(int i=0;i<lis ...
判断是否登录的拦截器SessionFilter -
maotou1988:
Android控件之带清空按钮(功能)的AutoComplet ...
自定义AutoCompleteTextView -
yangmaolinpl:
希望有表例子更好。。。,不过也看明白了。
浅谈onInterceptTouchEvent、onTouchEvent与onTouch
要实现一个功能:当Scrollview滑动到最底端的时候需要触发事件加载其他数据。很多人都以为ScrollView可以像ListViev那样setOnScrollListener,其实沒那么简单,因为ScrollView压根就没有该接口,在baidu上兜了一圈没有找到合适的答案,没办法只能google去了,居然一下子解决了这个问题,还是老外比较牛,呵呵,这是我访问的网址:
http://stackoverflow.com/questions/2864563/how-do-i-know-that-the-scrollview-is-already-scrolled-to-the-bottom
注意,如果数据不满一页的话,会执行onBottom方法!通常要使用懒加载的话数据都会超过一页,所以我沒仔细考虑这个问题!
我把ScrollView封装成类了,源码如下:
用的时候也很简单,通常这样使用:
感激我吧,我呕心沥血才出来了这么个类。呵呵。
重写onScrollChanged()的模式
顺便记一下老外使用fullScroll的做法。当然也可以直接fullScroll而不需要放入post()。
只要把fullScroll改成scrollTo就可以做一个书签效果了:
http://yangsongjing.iteye.com/blog/1855063
Android-ObservableScrollView
https://github.com/ksoichiro/Android-ObservableScrollView
在HorizontalScrollView中使用ScrollView相互影响问题的解决办法:
On my ScrollView, I needed to override the onInterceptTouchEvent method to only intercept the touch event if the Y motion is > the X motion. It seems like the default behavior of a ScrollView is to intercept the touch event whenever there is ANY Y motion. So with the fix, the ScrollView will only intercept the event if the user is deliberately scrolling in the Y direction and in that case pass off the ACTION_CANCEL to the children.
Here is the code for my Scroll View class that contains the HorizontalScrollView:
android监听ScrollView滑动停止
在ScrollView中嵌入GridView
http://fariytale.iteye.com/blog/1420254
做android程序开发的都知道,不能在一个拥有Scrollbar的组件中嵌入另一个拥有Scrollbar的组件,因为这不科学,会混淆滑动事件,导致只显示一到两行数据。那么就换一种思路,首先让子控件的内容全部显示出来,禁用了它的滚动。如果超过了父控件的范围则显示父控件的scrollbar滚动显示内容,思路是这样,一下是代码。
具体的方法是自定义GridView组件,继承自GridView。重载onMeasure方法:
其中onMeasure函数决定了组件显示的高度与宽度;
makeMeasureSpec函数中第一个函数决定布局空间的大小,第二个参数是布局模式
MeasureSpec.AT_MOST的意思就是子控件需要多大的控件就扩展到多大的空间
之后在ScrollView中添加这个组件就OK了,同样的道理,ListView也适用。
滚动监听的ScrollView
ScrollView也可以实现OnTouchListener来监听是否滑动到最底部
在滚动的视图观测滚动事件的Android库:Android-ObservableScrollView
http://www.open-open.com/lib/view/open1415854429070.html
网上说的方法乱七八糟,能用的就是自己算高度,其实sdk-9中,ScrollView已经加入了一个方法,能监听到是否已经不能滚动,稍加处理,就可以监听是否滑到底部了。
先上自定义的ScrollView方法:
调用方法:
scrollView 又不继承自absListView!
http://stackoverflow.com/questions/2864563/how-do-i-know-that-the-scrollview-is-already-scrolled-to-the-bottom
注意,如果数据不满一页的话,会执行onBottom方法!通常要使用懒加载的话数据都会超过一页,所以我沒仔细考虑这个问题!
我把ScrollView封装成类了,源码如下:
package com.ql.view; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ScrollView; public class LazyScrollView extends ScrollView{ private static final String tag="LazyScrollView"; private Handler handler; private View view; public LazyScrollView(Context context) { super(context); // TODO Auto-generated constructor stub } public LazyScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public LazyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } //这个获得总的高度 public int computeVerticalScrollRange(){ return super.computeHorizontalScrollRange(); } public int computeVerticalScrollOffset(){ return super.computeVerticalScrollOffset(); } private void init(){ this.setOnTouchListener(onTouchListener); handler=new Handler(){ @Override public void handleMessage(Message msg) { // process incoming messages here super.handleMessage(msg); switch(msg.what){ case 1: if(view.getMeasuredHeight() <= getScrollY() + getHeight()) { if(onScrollListener!=null){ onScrollListener.onBottom(); } }else if(getScrollY()==0){ if(onScrollListener!=null){ onScrollListener.onTop(); } } else{ if(onScrollListener!=null){ onScrollListener.onScroll(); } } break; default: break; } } }; } OnTouchListener onTouchListener=new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: if(view!=null&&onScrollListener!=null){ handler.sendMessageDelayed(handler.obtainMessage(1), 200); } break; default: break; } return false; } }; /** * 获得参考的View,主要是为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。 */ public void getView(){ this.view=getChildAt(0); if(view!=null){ init(); } } /** * 定义接口 * @author admin * */ public interface OnScrollListener{ void onBottom(); void onTop(); void onScroll(); } private OnScrollListener onScrollListener; public void setOnScrollListener(OnScrollListener onScrollListener){ this.onScrollListener=onScrollListener; } }
用的时候也很简单,通常这样使用:
scrollView=(LazyScrollView)findViewById(R.id.scrollView); scrollView.getView(); scrollView.setOnScrollListener(new OnScrollListener() { @Override public void onTop() { // TODO Auto-generated method stub Log.d(tag,"------滚动到最上方------"); } @Override public void onScroll() { // TODO Auto-generated method stub Log.d(tag,"没有到最下方,也不是最上方"); } @Override public void onBottom() { // TODO Auto-generated method stub Log.d(tag,"------滚动到最下方------"); } });
感激我吧,我呕心沥血才出来了这么个类。呵呵。
重写onScrollChanged()的模式
import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.ScrollView; /** * BorderScrollView * <ul> * <li>onTop and onBottom response ScrollView</li> * <li>you can {@link #setOnBorderListener(OnBorderListener)} to set your top and bottom response</li> * </ul> * * @author trinea@trinea.cn 2013-5-21 */ public class BorderScrollView extends ScrollView { private OnBorderListener onBorderListener; private View contentView; public BorderScrollView(Context context) { super(context); } public BorderScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public BorderScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); doOnBorderListener(); } public void setOnBorderListener(final OnBorderListener onBorderListener) { this.onBorderListener = onBorderListener; if (onBorderListener == null) { return; } if (contentView == null) { contentView = getChildAt(0); } } /** * OnBorderListener, Called when scroll to top or bottom * * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-22 */ public static interface OnBorderListener { /** * Called when scroll to bottom */ public void onBottom(); /** * Called when scroll to top */ public void onTop(); } private void doOnBorderListener() { if (contentView != null && contentView.getMeasuredHeight() <= getScrollY() + getHeight()) { if (onBorderListener != null) { onBorderListener.onBottom(); } } else if (getScrollY() == 0) { if (onBorderListener != null) { onBorderListener.onTop(); } } } }
顺便记一下老外使用fullScroll的做法。当然也可以直接fullScroll而不需要放入post()。
scrollView.post(new Runnable() { @Override public void run() { scrollView.fullScroll(View.FOCUS_DOWN); } });
只要把fullScroll改成scrollTo就可以做一个书签效果了:
http://yangsongjing.iteye.com/blog/1855063
Android-ObservableScrollView
https://github.com/ksoichiro/Android-ObservableScrollView
在HorizontalScrollView中使用ScrollView相互影响问题的解决办法:
On my ScrollView, I needed to override the onInterceptTouchEvent method to only intercept the touch event if the Y motion is > the X motion. It seems like the default behavior of a ScrollView is to intercept the touch event whenever there is ANY Y motion. So with the fix, the ScrollView will only intercept the event if the user is deliberately scrolling in the Y direction and in that case pass off the ACTION_CANCEL to the children.
Here is the code for my Scroll View class that contains the HorizontalScrollView:
public class CustomScrollView extends ScrollView { private GestureDetector mGestureDetector; View.OnTouchListener mGestureListener; public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(new YScrollDetector()); setFadingEdgeLength(0); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } // Return false if we're scrolling in the x direction class YScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if(Math.abs(distanceY) > Math.abs(distanceX)) { return true; } return false; } } }
android监听ScrollView滑动停止
在ScrollView中嵌入GridView
http://fariytale.iteye.com/blog/1420254
做android程序开发的都知道,不能在一个拥有Scrollbar的组件中嵌入另一个拥有Scrollbar的组件,因为这不科学,会混淆滑动事件,导致只显示一到两行数据。那么就换一种思路,首先让子控件的内容全部显示出来,禁用了它的滚动。如果超过了父控件的范围则显示父控件的scrollbar滚动显示内容,思路是这样,一下是代码。
具体的方法是自定义GridView组件,继承自GridView。重载onMeasure方法:
public class MyGridView extends GridView { public MyGridView(android.content.Context context, android.util.AttributeSet attrs) { super(context, attrs); } /** * 设置不滚动 */ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
其中onMeasure函数决定了组件显示的高度与宽度;
makeMeasureSpec函数中第一个函数决定布局空间的大小,第二个参数是布局模式
MeasureSpec.AT_MOST的意思就是子控件需要多大的控件就扩展到多大的空间
之后在ScrollView中添加这个组件就OK了,同样的道理,ListView也适用。
滚动监听的ScrollView
import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; public class NotifyingScrollView extends ScrollView { public interface OnScrollChangedListener { void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt); } private OnScrollChangedListener mOnScrollChangedListener; public NotifyingScrollView(Context context) { super(context); } public NotifyingScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mOnScrollChangedListener != null) { mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt); } } public void setOnScrollChangedListener(OnScrollChangedListener listener) { mOnScrollChangedListener = listener; } }
ScrollView也可以实现OnTouchListener来监听是否滑动到最底部
import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ScrollView; import android.app.Activity; /** * Demo描述: * 监听ScrollView滑动到顶端和底部 * * 注意事项: * 1 mScrollView.getChildAt(0).getMeasuredHeight()表示: * ScrollView所占的高度.即ScrollView内容的高度.常常有一 * 部分内容要滑动后才可见,这部分的高度也包含在了 * mScrollView.getChildAt(0).getMeasuredHeight()中 * * 2 view.getScrollY表示: * ScrollView顶端已经滑出去的高度 * * 3 view.getHeight()表示: * ScrollView的可见高度 * */ public class MainActivity extends Activity { private ScrollView mScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mScrollView=(ScrollView) findViewById(R.id.scrollView); mScrollView.setOnTouchListener(new TouchListenerImpl()); } private class TouchListenerImpl implements OnTouchListener{ @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int scrollY=view.getScrollY(); int height=view.getHeight(); int scrollViewMeasuredHeight=mScrollView.getChildAt(0).getMeasuredHeight(); if(scrollY==0){ System.out.println("滑动到了顶端 view.getScrollY()="+scrollY); } if((scrollY+height)==scrollViewMeasuredHeight){ System.out.println("滑动到了底部 scrollY="+scrollY); System.out.println("滑动到了底部 height="+height); System.out.println("滑动到了底部 scrollViewMeasuredHeight="+scrollViewMeasuredHeight); } break; default: break; } return false; } }; }
在滚动的视图观测滚动事件的Android库:Android-ObservableScrollView
http://www.open-open.com/lib/view/open1415854429070.html
import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; public class ObservableScrollView extends ScrollView { private ScrollViewListener scrollViewListener = null; public ObservableScrollView(Context context) { super(context); } public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(ScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (scrollViewListener != null) { scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); } } }
网上说的方法乱七八糟,能用的就是自己算高度,其实sdk-9中,ScrollView已经加入了一个方法,能监听到是否已经不能滚动,稍加处理,就可以监听是否滑到底部了。
先上自定义的ScrollView方法:
import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; public class BottomScrollView extends ScrollView { private OnScrollToBottomListener onScrollToBottom; public BottomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public BottomScrollView(Context context) { super(context); } @Override protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { super.onOverScrolled(scrollX, scrollY, clampedX, clampedY); if(scrollY != 0 && null != onScrollToBottom){ onScrollToBottom.onScrollBottomListener(clampedY); } } public void setOnScrollToBottomLintener(OnScrollToBottomListener listener){ onScrollToBottom = listener; } public interface OnScrollToBottomListener{ public void onScrollBottomListener(boolean isBottom); } }
调用方法:
BottomScrollView scroll = (BottomScrollView)findViewById(R.id.id_scroll); scroll.setOnScrollToBottomLintener(new OnScrollToBottomListener() { @Override public void onScrollBottomListener(boolean isBottom) { // TODO Auto-generated method stub Log.e("SCROLLVIEW", isBottom + ""); } });
评论
10 楼
xfx108
2016-01-11
下面这行代码。 如果lazyScrollview里含有 水平滑动的组件,比如HorizontalScrollView .会与水平滑动的组件上下左右滑动冲突。
//这个获得总的高度
public int computeVerticalScrollRange(){
return super.computeHorizontalScrollRange();
}
//这个获得总的高度
public int computeVerticalScrollRange(){
return super.computeHorizontalScrollRange();
}
9 楼
wwwkp1236
2013-05-27
楼主、用了懒加载嵌套listView和GridView后滚动条都不见了、求解决办法!
8 楼
gundumw100
2013-04-26
feiyu0526 写道
为什么这个地方 scrollView.setOnScrollListener(new OnScrollListener() 让我定义成android.widget.AbsListView.OnScrollListener;这个类型 定义 后 onBottom就会出错了求解
scrollView 又不继承自absListView!
7 楼
feiyu0526
2013-04-26
为什么这个地方 scrollView.setOnScrollListener(new OnScrollListener() 让我定义成android.widget.AbsListView.OnScrollListener;这个类型 定义 后 onBottom就会出错了求解
6 楼
feiyu0526
2013-04-26
为什么这个地方 scrollView.setOnScrollListener(new OnScrollListener() 让我定义成android.widget.AbsListView.OnScrollListener;这个类型 定义 后 onBottom就会出错了求解
5 楼
崮rz教隍
2012-09-19
学习了。!!
4 楼
hlyaowan
2012-06-16
非常感谢楼主啊,很重要的东西
3 楼
蓝桥魔兽
2012-05-23
学习了,多谢!!!!!
2 楼
Samanoseky
2011-11-24
太感谢了...
1 楼
llty
2011-09-25
顶 学习 、。。。。
发表评论
-
NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端
2018-10-31 20:45 6798网上有一个StickyScrollView,称之为粘性Scro ... -
自定义Behavior实现AppBarLayout越界弹性效果
2017-03-31 09:33 10220一、继承AppBarLayout.Beha ... -
Android - 一种相似图片搜索算法的实现
2017-03-31 09:33 2567算法 缩小尺寸。 将图片缩小到8x8的尺寸,总共64个 ... -
使用SpringAnimation实现带下拉弹簧动画的 ScrollView
2017-03-30 11:30 2788在刚推出的 Support Library 25.3.0 里面 ... -
Android为应用添加角标(Badge)
2017-03-30 11:21 60991.需求简介 角标是什么意思呢? 看下图即可明了: 可 ... -
Android端与笔记本利用局域网进行FTP通信
2017-03-23 10:17 922先看图 打开前: 打开后: Activity类 ... -
PorterDuffColorFilter 在项目中的基本使用
2017-03-03 10:58 1297有时候标题栏会浮在内容之上,而内容会有颜色的变化,这时候就要求 ... -
ColorAnimationView 实现了滑动Viewpager 时背景色动态变化的过渡效果
2017-02-24 09:41 2168用法在注释中: import android.anima ... -
迷你轻量级全方向完美滑动处理侧滑控件SlideLayout
2017-01-16 16:53 2546纯手工超级迷你轻量级全方向完美滑动处理侧滑控件(比官方 sup ... -
Effect
2017-01-05 09:57 0https://github.com/JetradarMobi ... -
动态主题库Colorful,容易地改变App的配色方案
2016-12-27 14:49 2516Colorful是一个动态主题库,允许您很容易地改变App的配 ... -
对视图的对角线切割DiagonalView
2016-12-27 14:23 1070提供对视图的对角线切割,具有很好的用户定制 基本用法 ... -
仿淘宝京东拖拽商品详情页上下滚动黏滞效果
2016-12-26 16:53 3417比较常用的效果,有现成的,如此甚好!:) import ... -
让任意view具有滑动效果的SlideUp
2016-12-26 09:26 1657基本的类,只有一个: import android.a ... -
AdvancedWebView
2016-12-21 09:44 16https://github.com/delight-im/A ... -
可设置圆角背景边框的按钮, 通过调节色彩明度自动计算按下(pressed)状态颜色
2016-11-02 22:13 1859可设置圆角背景边框的的按钮, 通过调节色彩明度自动计算按下(p ... -
网络请求库相关
2016-10-09 09:35 62https://github.com/amitshekhari ... -
ASimpleCache一个简单的缓存框架
2015-10-26 22:53 2121ASimpleCache 是一个为android制定的 轻量级 ... -
使用ViewDragHelper实现的DragLayout开门效果
2015-10-23 10:55 3330先看一下图,有个直观的了解,向下拖动handle就“开门了”: ... -
保证图片长宽比的同时拉伸图片ImageView
2015-10-16 15:40 3677按比例放大图片,不拉伸失真 import android. ...
相关推荐
简单的实现了ScrollView下拉继续加载,没有太多的界面控制代码,主要是实现下拉加载逻辑
ScrollView动态加载资源节省动态更新!
UIScroView 3倍的contentSize,左右Scroll时,懒惰添加左右的View
scrollview每个页面延迟加载 滑动到哪个页面 才加载当前页面
安卓ScrollView下拉刷新上拉加载效果
Ig-Lazy-Module-Loader This library helps with loading modules (features) in Android apps on demand, whenever needed. Before this library can be used a module needs to be compiled to a separate jar/...
判断scrollView判断是否停止,然后加载下一页,另外还有一个关于scrollView的简单例子
支持上拉加载、下拉刷新 支持无限循环滚动 分帧创建item 循环分页 pageview 嵌套分页 pageview 支持表格布局Grid 支持不定宽高缩放自适应,item在运行时可随意更改宽高和缩放 支持滑动自动居中,居中锚点可自定义
很多朋友因项目需求问题需要在ScrollView中嵌套使用ListView或ScrollView 但是彼此的滑动事件会发生冲突,网上大多数都是计算ListView的高度来解决这个问题,但个人亲手尝试 结果ListView的Item要是过多的话ListView...
titanium ScrollView
ScrollView动态加载最新版加动态锁定当前页面!!!!
Android官方刷新组件SwipeRefreshLayout 支持scrollview上拉加载更多,还有正常listview加载更多
ScrollView 嵌套ScrollView 事件分发问题
listview,gridview,scrollview下拉刷新和上拉加载更多
主要是增加了一个自定义view的属性,用于选择该控件是支持(只有下拉刷新,既无刷新也无加载,只有上拉加载,既有下拉刷新又有上拉加载)
将开源项目PullToRefresh中的ScrollView下拉刷新提取出来了,可直接集成到项目中。
Android ScrollView嵌套ListView嵌套GridView的上下拉以及加载更多
UGUI Super ScrollView
scrollView详细描述,可以更换每个scrollView上的图片,每个图片有相应的点击事件
UGUI scrollview 插件