Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable the overscroll and the bounce in an android listview?

The behaviour of my listview on 2 devices is that either it turns yellow/orange when I overscroll it, or that it can be overscrolled and then snaps back. The latter behaviour is bad because it shows the background beneath it which I want to prevent.

I tried:

listview.setOverScrollMode(ListView.OVER_SCROLL_NEVER);

and it doesn't show the background anymore but now there is a very annoying bounce effect. Is it possible to both disable the bounce and the overscrolling and make it so the scrolling just ends without any effect when it reaches the end ?

PS: I am using android 2.3 on both devices.

like image 642
HardCoder Avatar asked Apr 29 '12 00:04

HardCoder


1 Answers

Here's how I solved this, hopefully it will help those searching. The key is to attach an OnScrollListener to the list, keep track of when a fling gesture is being processed, and when the end of the list has been reached. Then, while the fling is still going on, keep on resetting the position to the end if the system tries to move it.

private ListView mListView;
private ListAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_list);

    mListView = (ListView) findViewById(R.id.listView);
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getList(25));
    mListView.setAdapter(mAdapter);
    mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
    if(Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD || Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1){
        mListView.setOnScrollListener(new OnScrollListener(){
            private boolean flinging = false;
            private boolean reachedEnd = false;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                flinging = (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING);
                reachedEnd = false;
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(reachedEnd && flinging && (firstVisibleItem + visibleItemCount < totalItemCount)){
                    mListView.setSelection(mAdapter.getCount() - 1);
                }else if(firstVisibleItem + visibleItemCount == totalItemCount){
                    reachedEnd = true;
                }else{
                    reachedEnd = false;
                }

            }

        });
    }

}
like image 134
Stevie Kideckel Avatar answered Nov 08 '22 20:11

Stevie Kideckel