我可以看到一些關於「ListView裏面的ScrollView」問題的問題。我知道,我們不應該做這樣的嵌套,只是因爲這兩個組件都有自己的滾動功能,而Google也這樣說(我讀過它是無用的東西)。但在我目前的項目中,我需要這樣的行爲:如果listview可以滾動 - 它是滾動,如果不是(列表視圖的頂部或底部邊界) - 滾動視圖滾動。 所以,我寫了這樣的代碼:ListView裏面的ScrollView滾動改進
public static void smartScroll(final ScrollView scroll, final ListView list){
scroll.requestDisallowInterceptTouchEvent(true);
list.setOnTouchListener(new OnTouchListener() {
private boolean isListTop = false, isListBottom = false;
private float delta = 0, oldY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
oldY = event.getY();
break;
case MotionEvent.ACTION_UP:
delta = 0;
break;
case MotionEvent.ACTION_MOVE:
delta = event.getY() - oldY;
oldY = event.getY();
isListTop = false;
isListBottom = false;
View first = list.getChildAt(0);
View last = list.getChildAt(list.getChildCount()-1);
if(first != null && list.getFirstVisiblePosition() == 0 && first.getTop() == 0 && delta > 0.0f){
isListTop = true;
}
if(last != null && list.getLastVisiblePosition() == list.getCount()-1 && last.getBottom() <= list.getHeight() && delta < 0.0f){
isListBottom = true;
}
if((isListTop && delta > 0.0f) || (isListBottom && delta < 0.0f)){
scroll.post(new Runnable() {
public void run() {
scroll.smoothScrollBy(0, -(int)delta);
}
});
}
break;
default: break;
}
scroll.requestDisallowInterceptTouchEvent(true);
return false;
}
});
}
和它的作品,至少在目標API 8.但也有一些滾動毛刺(不自然的跳躍)。我認爲,scroll.smoothScrollBy(0, - (int)delta)的原因;有沒有人想過,如何提高滾動視圖滾動:)?它經常被稱爲(在移動中)和在後,也許這是原因?
嗨,你目前在這個問題上的想法是什麼?你有沒有解決不自然的跳轉問題?謝謝... – OferR
嗨!那麼,設計已經改變了,不再需要這種行爲了。我的想法 - 我們不能在ScrollView中使用ListView,因爲它們的實現。谷歌的人都是對的:)所以,要實現這樣的行爲,最好是基於ViewGroup實現自己的UI組件。 – Olsavage
感謝您的回答Olsavage。因此,我將研究其他呈現信息的方式。 – OferR