您的位置:首页 > 其它

关于ListView,GridView,ScrollView是否滑动到了顶部或者底部处理

2016-05-16 09:54 417 查看
我也是在研究下拉刷新,上拉加载时必须要判断的,首先ListView和GridView一样,通过判断fisrtPosition是否为0(ListView.getCcount)和firstView距离顶部或者底部的距离来判断,具体代码:

private boolean isTop(ListView listView){
View firstView=null;
if(listView.getCount()==0){
return true;
}
firstView=listView.getChildAt(0);
if(firstView!=null){
if(listView.getFirstVisiblePosition()==0&&firstView.getTop()==listView.getListPaddingTop()){
return true;
}
}else{
return true;
}

return false;
}
private boolean isBottom(ListView listView){
int lastPosition=listView.getLastVisiblePosition();
int count=listView.getCount();
int childCount=listView.getChildCount();
View lastVisibaleView=listView.getChildAt(childCount-1);
if(childCount==count){
return true;
}
if(lastVisibaleView!=null){
if(lastPosition==count-1&&lastVisibaleView.getBottom()+listView.getListPaddingBottom()==listView.getHeight()){
return true;
}
}
return false;
}


至于ScrollView的判断则是另一种,具体代码:
private boolean isBottom(ScrollView scrollView){
if(scrollView.getScrollY()==0){
return true;
}
return false;
}
private boolean isBottom(ScrollView sc){
int scrollY=sc.getScrollY();
int height=sc.getHeight();
View view=sc.getChildAt(0);
if(view==null){
return true;
}else{
if((scrollY+height)>=view.getMeasuredHeight()){//滑到了底部
return true;
}
}
return false;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: