1

How can I bring the item in front off the screen on stop scrolling automatically with animation.Currently it works with following code but it perform very fast with scroll.

So my requirement is to perform this with animation so user can see sliding of listview.

Below is my code,but I did not get expected result.

listview.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE) {
                    View child = layout.getChildAt(0);    // first visible child
                    Rect r = new Rect(0, 0, child.getWidth(), child.getHeight());     // set this initially, as required by the docs
                    double height = child.getHeight() * 1.0;
                    layout.getChildVisibleRect(child, r, null);
                    if (Math.abs(r.height()) != height)
                    {
                        //only smooth scroll when not scroll to correct position
                        if (Math.abs(r.height()) < height / 2.0)
                        {
                            listview.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    listview.smoothScrollBy(listview.getFirstVisiblePosition(), 10000);
                                }
                            }, 500);
                        } else if (Math.abs(r.height()) > height / 2.0)
                        {
                            listview.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    listview.smoothScrollBy(listview.getFirstVisiblePosition(), 10000);
                                }
                            }, 500);
                        } else
                        {

                            listview.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    listview.smoothScrollBy(listview.getFirstVisiblePosition(),10000);
                                }
                            },500);

                        }

                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
user8938
  • 559
  • 1
  • 4
  • 12

1 Answers1

0

Old question but just for reference, this worked for me:

    if (condition) {
        // add stuff to begining of ListView and scroll up
        arrayList.add(0, stuff);
        listView.post(new Runnable() {
            @Override
            public void run() {
                listView.smoothScrollToPosition(0);
            }
        });
    } else {
        // add stuff to end of ListView and scroll down
        arrayList.add(index, stuff);
        listView.post(new Runnable() {
            @Override
            public void run() {
                listView.smoothScrollToPosition(adapter.getCount() - 1);
            }
        });
    }

Basically just smoothScrollToPosition instead of smoothScrollBy.

Iker Hoëk
  • 242
  • 3
  • 14