首页 >> 知识 >> androids 各种 滚动View 监听 和各种判断

androids 各种 滚动View 监听 和各种判断

总结下各种View 的滑动监听

滑动阈值:int touchSlop = ViewConfiguration.get(this).getScaledTouchSlop();

getMeasuredHeight()是实际View的大小,与屏幕无关,而getHeight的大小此时则是屏幕的大小。

当超出屏幕后, getMeasuredHeight() 等于 getHeight()加上屏幕之外没有显示的大小

滚动到顶部判断: getScrollY() == 0

滚动到底部判断: View childView = getChildAt(0);

childView.getMeasuredHeight() 50) { Toast.makeText(MainActivity.this, "向下滑", Toast.LENGTH_SHORT).show(); } else if(x1 - x2 > 50) { Toast.makeText(MainActivity.this, "向左滑", Toast.LENGTH_SHORT).show(); } else if(x2 - x1 > 50) { Toast.makeText(MainActivity.this, "向右滑", Toast.LENGTH_SHORT).show(); } } return super.onTouchEvent(event); } }

2. 继承SrcoolView 

/使用自定义view继承自ScrollViewpublic class MyScrollView extends ScrollView { private OnScrollListener listener; public void setOnScrollListener(OnScrollListener listener) { this.listener = listener; } public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //设置接口 public interface OnScrollListener{ void onScroll(int scrollY); } //重写原生onScrollChanged方法,将参数传递给接口,由接口传递出去 @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if(listener != null){ //这里我只传了垂直滑动的距离 listener.onScroll(t); } }} //1. 第一个参数是目前水平滑动后的距离 //2. 第二个参数是目前垂直滑动后的距离 //3. 第三个参数是之前水平滑动前的距离 //4. 第四个参数是之前水平滑动前的距离 ListView 和GridView 监听

移动到顶部底部:

listview.setSelection(position);   

gridview.smoothScrollToPosition(position);  带动画   post新开线程

需要在新线程使用

滚动监听:

private AbsListView.OnScrollListener LIST_GRID_LISTENER = new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case SCROLL_STATE_FLING: break; case SCROLL_STATE_IDLE: // 判断滚动到顶部 if (mListView.getFirstVisiblePosition() == 0) { View firstVisibleItemView = view.getChildAt(0); if (firstVisibleItemView != null && firstVisibleItemView.getTop() == 0) { } } break; case SCROLL_STATE_TOUCH_SCROLL: break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem == 0) { //顶部 不够准确 } } };

ScroolState 三种状态

OnScrollListener.SCROLL_STATE_IDLE:滚动停止时的状态OnScrollListener.SCROLL_STATE_STOUCH_SCROLL:触摸正在滚动,手指还没离开界面时的状态OnScrollListener.SCROLL_STATE_FLING:用户在用力滑动后,ListView由于惯性将继续滑动时的状态  

onScroll 介绍

firstVisibleItem:当前能看见的第一个item的ID(从0开始)visibleItemCount:当前可见的item总数totalItemCount:列表中适配器总数量,也就是整个ListView中item总数

滑动方向:

firstVisibleItem > oldVisibleItem   // 向上滑动firstVisibleItem < oldVisibleItem  // 向下滑动

判断顶部底部

firstVisibleItem + visibleItemCount == totalItemCount    //底部firstVisibleItem=0  顶部     在onScrollStateChanged中判断mListView.getLastVisiblePosition() == (mListView.getCount() - 1)  //底部mListView.getFirstVisiblePosition() == 0   //顶部

ListView也为草莓视频在线观看APP提供了一些封装好了的方法,来获取item的位置信息 

// 获取当前可见区域内第一个item            mListView.getFirstVisiblePosition();

 // 获取当前可见区域内最后一个item       mListView.getLastVisiblePosition();

 

RecyclerView :

移动:

RecyclerView提供了几种移动的方法

scrollToPosition       这个方法的作用是定位到指定项,就是把你想显示的项显示出来,但是在屏幕的什么位置是不管的,只要那一项现在看得到了,那它就罢工了!

scrollTo

scrollBy   这个方法是自己去控制移动的距离,单位是像素,所以在使用scrollBy(x, y)需要自己去计算移动的高度或宽度。

smoothScrollBy    

smoothScrollToPosition      惯性滑动至某一位置

scrollToPositionWithOffset   用法:((LinearLayoutManager)recyclerView.getLayoutManager()).scrollToPositionWithOffset(position,0);

该置顶就置顶

判断 顶部 底部方法一:

垂直判断:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); Log.i(TAG, "--------------------------------------"); if(mRecyclerView.canScrollVertically(1)){ Log.i(TAG, "direction 1: true"); }else { Log.i(TAG, "direction 1: false");//滑动到底部 } if(mRecyclerView.canScrollVertically(-1)){ Log.i(TAG, "direction -1: true"); }else { Log.i(TAG, "direction -1: false");//滑动到顶部 } } });

横向:

/** * Check if this view can be scrolled horizontally in a certain direction. * * @param direction Negative to check scrolling left, positive to check scrolling right. * @return true if this view can be scrolled in the specified direction, false otherwise. */ public boolean canScrollHorizontally(int direction) { final int offset = computeHorizontalScrollOffset(); final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent(); if (range == 0) return false; if (direction < 0) { return offset > 0; } else { return offset < range - 1; } }

判断是否滑动到底部, recyclerView.canScrollVertically(1);返回false表示不能往上滑动,即代表到底部了;

判断是否滑动到顶部, recyclerView.canScrollVertically(-1);返回false表示不能往下滑动,即代表到顶部了;

方法二:  

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); Log.i(TAG, "--------------------------------------"); LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition(); Log.i(TAG, "firstCompletelyVisibleItemPosition: "+firstCompletelyVisibleItemPosition); if(firstCompletelyVisibleItemPosition==0) Log.i(TAG, "滑动到顶部"); int lastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition(); Log.i(TAG, "lastCompletelyVisibleItemPosition: "+lastCompletelyVisibleItemPosition); if(lastCompletelyVisibleItemPosition==layoutManager.getItemCount()-1) Log.i(TAG, "滑动到底部"); } });

滑动方向:

//dy 0 表示下滑

三种状态:

/** * The RecyclerView is not currently scrolling. * 当前的recycleView不滑动(滑动已经停止时) */public static final int SCROLL_STATE_IDLE = 0;/** * The RecyclerView is currently being dragged by outside input such as user touch input. * 当前的recycleView被拖动滑动 */public static final int SCROLL_STATE_DRAGGING = 1;/** * The RecyclerView is currently animating to a final position while not under * outside control. * 当前的recycleView在滚动到某个位置的动画过程,但没有被触摸滚动.调用 scrollToPosition(int) 应该会触发这个状态 */public static final int SCROLL_STATE_SETTLING = 2;

 

网站地图