ListView Scrolling - One by One

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

After scrolling through the ListView, I have to adjust the scroll to fit 4 items. I mean, I can't show the item in half.

Another question, is there a way to get the current scrollview offset of a ListView? Because the listView.getScrollY () method is in the view, but not in the Scroller object inside the ListView.

+3
source share
2 answers

I found a great solution. He's great for me. Maybe 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) {
        }
    }

, , , 4, ("getChildAt(0)" and "getChildAt(1)"). !

+3

"OnTouchListener" ScrollView. , , scrooler, , . :

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, ...)
    }});

, ! , , .

0

All Articles