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;
}
});
}
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?
i think u need this
I have faced same problem. I've improved this code and now it works correct i think.
When we touch listview - we enable its scroll and disable parent scroll:
And if we reaching the border of the list - we disable its scroll and enable parent scroll :
It scrolls pretty smoothy for me. Hope this helps.
If the parent is ScrollView , change it as LinearLayout. It will work. For example , in here I wrote LinearLayout instead of ScrollView.
My sugesstion would put the listview tag out side the scrollview. And the add the area outside the listview inside a scrollview. Then put everything inside a linear layout. So the scollview and listview will be seperate.