您的位置:首页 > 其它

listview 滑动监听

2016-01-25 09:31 471 查看
// listview touch and scroll listener
boolean isTabShow = true;
float downY = 0;
boolean isActionDown = false;
private int mListViewFirstItem = -1;
//listView中第一项的在屏幕中的位置
private int mScreenY = 0;
private int screenHeight = 0;
private  int lastDirection = 0; // 0  向上滑动 1 向下滑动
View.OnTouchListener listViewOnTouchListener = new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isActionDown = true;
downY = event.getY();
Log.d("onTouchListener", "downY:" + downY);
break;
case MotionEvent.ACTION_MOVE:
if (!isActionDown) {
// 当为false时就触发了ACTION_MOVE,第一个action需要当成ACTION_DOWN处理
isActionDown = true;
downY = event.getY();
Log.d("onTouchListener", "downY:" + downY + " no action down");
} else {
float currentY = event.getY();
Log.d("onTouchListener", "downY:" + downY + " currentY::"
+ currentY + " currentY - downY:"
+ (currentY - downY));
if (currentY - downY < -20 && isTabShow) {
// 向上,隐藏
isTabShow = false;
Log.d("onTouchListener", "downY:" + downY + " currentY::"
+ currentY + " currentY - downY:"
+ (currentY - downY) + " hide");

} else if (currentY - downY > 20 && !isTabShow && mListViewFirstItem == 0) {

isTabShow = true;
Log.d("onTouchListener", "downY:" + downY + " currentY::"
+ currentY + " currentY - downY:"
+ (currentY - downY) + " show");

}
}
break;
case MotionEvent.ACTION_UP:
isActionDown = false;// isActionDown重置
break;
default:
break;
}
return false;
}
};
AbsListView.OnScrollListener listScrollListener = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState)
{
if(mListViewFirstItem==-1)
{
return;
}
if (SCROLL_STATE_IDLE==scrollState )
{
//                if(Math.abs(mScreenY)-(screenHeight*3)/5>0)
//                {
//                    listRes.smoothScrollToPosition(mListViewFirstItem + 1);
//
//                }else{
//                    listRes.smoothScrollToPosition(mListViewFirstItem);
//                }
//                if(listRes.getFirstVisiblePosition() == 0 && lastDirection == 1) {
//                    mMPHBarChart.setVisibility(View.VISIBLE);
//                }

}

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)
{
if(listRes.getAdapter()==null || listRes.getChildCount()==0)
{
return;
}
View visibleFirstChild = (View) listRes.getChildAt(firstVisibleItem);
if(visibleFirstChild==null)
{
return;
}
int[] location = new int[2];
visibleFirstChild.getLocationOnScreen(location);
Log.d("zzq", "first="+firstVisibleItem+" , screenY="+location[1]+",deltY="+(Math.abs(mScreenY)-(screenHeight*3)/5));

if(firstVisibleItem!=mListViewFirstItem)
{
if(firstVisibleItem>mListViewFirstItem)
{
Log.d("zzq", "between item 向上滑动  hide mpchart");
//   mMPHBarChart.setVisibility(View.GONE);
}else{
Log.d("zzq", "向下滑动");
}
}else{
if(mScreenY>location[1])
{
Log.d("zzq", "->finger in item 向上滑动 hide mpchart");
//mMPHBarChart.setVisibility(View.GONE);
}
else if(mScreenY<location[1])
{
Log.d("zzq", "->向下滑动");
}
}
if(firstVisibleItem == 0) {
// mMPHBarChart.setVisibility(View.VISIBLE);
Log.d("zzq", "first visible ==0 show mpchart");
}
mListViewFirstItem = firstVisibleItem;
mScreenY = location[1];

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: