3

I have a ListView that must display 4 items at a time. And I must scroll one item, one by one.

After user scrolls the ListView, I must readjust the scroll to fit 4 items. I mean, I can´t show an item by half.

Another question, is there any way to get the current ListView scrollY offset? Because the listView.getScrollY() method is from View, but not the Scroller object inside the ListView.

Korem
  • 11,383
  • 7
  • 55
  • 72
rlecheta
  • 361
  • 1
  • 3
  • 16

2 Answers2

3

I've found an excellent solution. It works perfect for me. May be my answer will help someone.

class ScrollListener implements AbsListView.OnScrollListener{
        boolean aligned;

        @Override
        public void onScrollStateChanged(AbsListView absListView, int state) {
            if (state == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                aligned = false;
            }
            if (state == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                if (!aligned) {
                    if (Math.abs(absListView.getChildAt(0).getY()) < Math.abs(absListView.getChildAt(1).getY())) {
                        listView.smoothScrollToPosition(absListView.getFirstVisiblePosition());
                    } else {
                        listView.smoothScrollToPosition(absListView.getLastVisiblePosition());
                    }
                }
                aligned = true;
            }
        }

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

I must say that in my situation I had two visible items so if you have 4 you should play with indexes ("getChildAt(0)" and "getChildAt(1)"). Good luck!

Shruti
  • 1
  • 13
  • 55
  • 95
Shamm
  • 1,004
  • 7
  • 9
0

you can implement the sensor "OnTouchListener" on ScrollView. then the technique is not to scroll until the person has not scrooler at least the height of an item. example of the code:

scroll.setOnTouchListener(new OnTouchListener()
{
     private int mLastY;
     public boolean onTouch(View v, MotionEvent event) 
     {          
        switch (event.getAction()) 
        {
            case MotionEvent.ACTION_DOWN:   //the user places his finger on the screen
                mLastY=(int)event.getY();   //to get the "y" position starting
                break;
            case MotionEvent.ACTION_MOVE:
                if(mLastY-event.getY() >= theSizeOfItem) //if a movement of the size of an item occurs
                {   
                    scroll.scrollTo(0, scroll.getScrollY()+ theSizeOfItem));
                    scroll.invalidate();
                    mLastY=(int)event.getY();   //reset the starting position to the current position                       

                }
                if(event.getY()-mLastY >= theSizeOfItem) //if a movement of the size of an item occurs
                {   
                    scroll.scrollTo(0, scroll.getScrollY() - theSizeOfItem));
                    scroll.invalidate();
                    mLastY=(int)event.getY();   //reset the starting position to the current position                       

                }
                break;
            default:
                break;
        }
        return v.onTouchEvent (event); //to use other sensors (OnClick, OnLongClick, ...)
    }});

must then implement the case where the scroll reaches the end ! Sorry for my English, I hope it will help you

julien dumortier
  • 1,303
  • 1
  • 13
  • 28
  • Yes, it works this way, thank you. But the ScrollView don´t uses Adapters. In my scenario I have to use adapters with ListView, because I need a infinite loop, and its easier to do using adapters.The problem with ListView, is that you cant get its scrollY real offset. – rlecheta May 22 '12 at 21:50
  • if I understand your problem. the solution would be to use a GridView with an adapter, so you pourez obtain accurate values ​​of the position. – julien dumortier May 23 '12 at 11:59