滚动型内滚动的ListView改善 [英] ListView inside ScrollView scroll improvement
问题描述
我可以看到有关里面的ListView滚动型问题的一些问题。我知道,我们不应该做这样的嵌套,只是因为这两个组件都有自己的滚动和谷歌是这么说的(我读过,这是没用的东西)。但是,在我当前的项目,我需要这样的行为:如果列表视图可以滚动 - 它正在滚动,如果不是(顶部或ListView底部边框) - 滚动视图的滚动。
所以,我写了这样code:
I can see some questions about "ListView inside ScrollView" issue. And I know, that we shouldn't do such nesting, just because both components have their own scrolling and Google said so(I've read that it is useless thing). But in my current project I need such behavior: if listview can scroll - it is scrolling, if not(top or bottom border of listview) - scrollview is scrolling. So, I've wrote such code:
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)三角洲);有任何人的想法,如何提高滚动视图滚动:)?它通常被称为(在移动)和岗位,也许这是什么原因?
And it works, at least on target API 8. But there are some scroll glitches(unnatural jumps). I think, the cause in scroll.smoothScrollBy(0, -(int)delta); Have anybody thoughts, how to improve scrollview scrolling :)? It is called often(on move) and on post, maybe that is the cause?
推荐答案
我面临同样的问题。我已经改进了这一code和现在的作品正确的,我认为。
I have faced same problem. I've improved this code and now it works correct i think.
public static void smartScroll(final ScrollView scroll, final ListView list){
list.setOnTouchListener(new View.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:
scroll.requestDisallowInterceptTouchEvent(true);
list.requestDisallowInterceptTouchEvent(false);
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.requestDisallowInterceptTouchEvent(false);
list.requestDisallowInterceptTouchEvent(true);
}
break;
default: break;
}
return false;
}
});
}
当我们触摸列表视图 - 我们使它的滚动和禁用父滚动:
When we touch listview - we enable its scroll and disable parent scroll:
scroll.requestDisallowInterceptTouchEvent(true);
list.requestDisallowInterceptTouchEvent(false);
如果我们达到了列表的边界 - 我们禁止其滚动和启用父滚动:
And if we reaching the border of the list - we disable its scroll and enable parent scroll :
scroll.requestDisallowInterceptTouchEvent(false);
list.requestDisallowInterceptTouchEvent(true);
它滚动pretty smoothy我。希望这有助于。
It scrolls pretty smoothy for me. Hope this helps.
这篇关于滚动型内滚动的ListView改善的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!