Android: make a top view of the scroll list along with the list

I have a list that scrolls explicitly. The list contains some form questions. As soon as the user submits the form, we put in the form of a list a kind that looks like a stamp at the top of the list (and do not allow the user to answer questions, but it doesn’t matter here).

The mark should be displayed at the top of the list (as at the top of the screen) and scroll at the same speed as the list, i.e. it should disappear when the list items that were originally under it disappeared at the top of the screen when scrolling.

Any idea how to achieve this?

+8
android android-listview android-custom-view
source share
2 answers

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.

+6
source share

You can add a headerview to view the top screen, so bind the coustom compost as a headerlayout listview ... so you scroll the same as listview ...

 View headerview = ((LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.track_footer_view, null, false); listview.addHeaderView(headerview); 
+5
source share

All Articles