I implemented a gallery, and inside it I have many listviews from left to right. For some reason Gallery works great with all views but not with listview. With listview, when scrolling on the gallery, sometimes I get little jumps.
Anyone have an idea on how to solve this?
some notes: The gallery uses an adapter to find out what to show, and then the listview is created based on the adapter
Thanks
The key to a smoothly scrolling ListView is to keep the application's main thread (the UI thread) free from heavy processing. Ensure you do any disk access, network access, or SQL access in a separate thread. To test the status of your app, you can enable StrictMode .
A ListView in Android is a scrollable list used to display items.
ListView itself is scrollable.
I had a similar issue. The problem is that the ListView is intercepting touch events from your Gallery and modifying the view position in its block of code that deals with the vertical scrolling of the ListView. If only Gallery intercepted the touch event first... I consider this a bug in the Android source, but in the meantime you can fix the un-smooth scrolling by subclassing Gallery and using your subclass instead. This will do the trick:
public class BetterGallery extends Gallery {
private boolean scrollingHorizontally = false;
public BetterGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public BetterGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BetterGallery(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
super.onInterceptTouchEvent(ev);
return scrollingHorizontally;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrollingHorizontally = true;
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
scrollingHorizontally = false;
}
return super.onTouchEvent(event);
}
}
Also, set something like this in the activity that implements the gallery:
ListView listView = (ListView) view.findViewById(R.id.users);
listView.setAdapter(userListAdapter);
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
galleryView.onTouchEvent(event);
return false;
}
});
Finally, add onTouchEvent() to the activity itself:
@Override
public boolean onTouchEvent(MotionEvent event) {
return galleryView.onTouchEvent(event);
}
One final note... After implementing this fully, I found that from a usability perspective, it was better to extend ViewAnimator with a custom class I called AdaptableViewAnimator, that of course just had some adapter functionality baked-in, and embed the ListView inside it. It doesn't float around as much as the ListView-inside-Gallery.
Scrollable items inside of other scrollable items tends to have problems. I am not surprised that this does not work well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With