gpt4 book ai didi

android - ListView inside ScrollView 滚动改进

转载 作者:行者123 更新时间:2023-11-29 16:21:40 25 4
gpt4 key购买 nike

我可以看到一些关于“ListView inside ScrollView”的问题。而且我知道,我们不应该做这样的嵌套,只是因为两个组件都有自己的滚动并且谷歌这么说(我读过它是无用的东西)。但在我当前的项目中,我需要这样的行为:如果 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);有没有人有想法,如何改进 scrollview 滚动 :)?它被称为经常(在移动中)和在岗位上,也许这就是原因?

最佳答案

我遇到了同样的问题。我改进了这段代码,现在我认为它可以正常工作了。

    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;
}
});
}

当我们触摸 ListView 时 - 我们启用它的滚动并禁用父滚动:

                    scroll.requestDisallowInterceptTouchEvent(true);
list.requestDisallowInterceptTouchEvent(false);

如果我们到达列表的边界 - 我们禁用它的滚动并启用父滚动:

                    scroll.requestDisallowInterceptTouchEvent(false);
list.requestDisallowInterceptTouchEvent(true);

它对我来说滚动起来非常流畅。希望这会有所帮助。

关于android - ListView inside ScrollView 滚动改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com