Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListView inside Gallery makes the scrolling not Smooth

Tags:

android

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

like image 724
Daniel Benedykt Avatar asked Jul 14 '10 19:07

Daniel Benedykt


People also ask

How do I make ListView scroll smoothly?

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 .

Does ListView have scroll?

A ListView in Android is a scrollable list used to display items.

Is List view scrollable by default?

ListView itself is scrollable.


2 Answers

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.

like image 116
Jonathan Schneider Avatar answered Sep 29 '22 01:09

Jonathan Schneider


Scrollable items inside of other scrollable items tends to have problems. I am not surprised that this does not work well.

like image 20
CommonsWare Avatar answered Sep 29 '22 02:09

CommonsWare