1. 程式人生 > >android 標題動態變換(listview scrollview 滑動距離計算)

android 標題動態變換(listview scrollview 滑動距離計算)

1:android:layout_marginTop="-100dp"給列表頭是圖片的列表加個這個屬性有下拉彈起的效果

2:計算列表下拉的距離(如果所有列表項長度一樣的話可行)

public int getScrollY() {
View c = myPullListView.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = myPullListView.getFirstVisiblePosition();
int top = c.getTop();
System.out.println(c.getHeight());
return -top + (firstVisiblePosition-1) * c.getHeight() ;
}

3:scrollView滑動距離計算

當手勢緩慢滑動時scrollView.getScrollY()即可計算出滑動距離.

當手勢快速滑動時,手勢已ACTION_UP,但scrollView仍會繼續滑動,此時就要通過隔一段時候計算一下scrollView.getScrollY(),當它不變時才表示不再滑動

scrollView.setOnTouchListener(new OnTouchListener() {
private int lastY = 0;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (lastY == scrollView.getScrollY()) {
ChangeTitle();
} else {
lastY = scrollView.getScrollY();
ChangeTitle();
handler.sendEmptyMessageDelayed(0, 300);
}
}
};


private void ChangeTitle() {
System.out.println("scrollView.getScrollY"+scrollView.getScrollY());
if (Util.px2dip(StoreDetailActivity.this, scrollView.getScrollY()) > 146) {
title.setBackgroundColor(Color.parseColor("#ffffff"));
} else {
title.setBackgroundColor(Color.parseColor("#00ffffff"));
}
}


@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int eventAction = event.getAction();
switch (eventAction) {
case MotionEvent.ACTION_UP:
handler.sendEmptyMessage(0);
break;
case MotionEvent.ACTION_MOVE:
ChangeTitle();
default:
break;
}
return false;
}
});

4:"#00ffffff"表示完全透明,00改成ff則是完全不透明