43097

ListView scroll - one by one

Question:

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.

Answer1:

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!

Answer2:

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

Recommend

  • Need to hide a textView while scrolling a grid view
  • Set first visible item in Android gridview completely seen
  • How can I hide tabs while scrolling down a ListView then show them again if the user scrolls up?
  • Android: using fragments programmatically
  • How can I integrate this Scrollable Tab library in Eclipse?
  • sed newline and carriage return pattern capture
  • AMD - Building library example
  • How to capture and parse JSON returned from Google Maps v3 API?
  • Robotium and Jenkins UI testing errors
  • String values look the same but don't “.equals()” each other
  • Batch For loop.. wildcards to determine length of searched files?
  • cannot be cast to android.content.pm.PackageInfo on click in my listview
  • How add listView header as a custom layout
  • | operator operation in linq?
  • Using third party libraries
  • Force Down error in MapView
  • Put value at centre of bins for histogram
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • GridView breaks while scrolling
  • NHibernate Validation Localization with S#arp Architecture
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Can I display google adwords (AdView) in javafx on android
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?