I have found a solution. Not sure if it is too elegant, but it works, so it will leave it if someone does not come up with something better.
The view that I want to view along with the list is a custom view that should know about the list view. Therefore, I implement the setListView (ListView listView) method on it:
private int scrollY; //1 private Map<Integer, Integer> listViewItemHeights = new Hashtable<>(); public void setListView(final ListView listView) { listView.setOnScrollListener(new OnScrollListener() { //2 @Override public void onScrollStateChanged(AbsListView view, int scrollState) {} //3 @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //4 View c = listView.getChildAt(0); if (c != null) { int oldScrollY = scrollY; scrollY = -c.getTop(); listViewItemHeights.put(listView.getFirstVisiblePosition(), c.getHeight()); for (int i = 0; i < listView.getFirstVisiblePosition(); ++i) { if (listViewItemHeights.get(i) != null) scrollY += listViewItemHeights.get(i); } scrollBy(0, scrollY - oldScrollY); } } }); }
Comment # 1: this is a variable that allows me to track the current scroll position.
Comment # 2: setting a new one in a scrollable listener so that my user view will recognize when the list
Comment No. 3: In this case, this is not necessary.
Comment # 4: magic happens here. Note that in the end I look at my view with scrollY - oldScrollY, let me start with this bit first. oldScrollY is the saved scroll position, scrollY is the new one. I need to scroll through the difference between the two. Regarding how scrollY is calculated, I refer to my answer here: Android gets the exact scroll position in the ListView , since the scroll position is calculated as a list.
Maria
source share