Code example for ListView

Methods: getChildAt, getFirstVisiblePosition, getViewTreeObserver

0
					@SuppressWarnings("deprecation") 
					@Override 
					public void onGlobalLayout() { 
						onScrollChanged(); 
 
						ViewTreeObserver obs = mListView.getViewTreeObserver();
						if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
							obs.removeOnGlobalLayoutListener(this);
						} else { 
							obs.removeGlobalOnLayoutListener(this);
						} 
					} 
				}); 
 
		mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
 
			@Override 
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			} 
 
			@Override 
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				onScrollChanged(); 
			} 
		}); 
 
		// Create and set the adapter for the listView. 
		SimpleAdapter simpleAdpt = new SimpleAdapter(this, createListViewData(), R.layout.list_item, new String[] {"item"}, new int[] {android.R.id.text1});		
		mListView.setAdapter(simpleAdpt);
	} 
 
	/** 
	 * Function used to calculate the position of the sticky view according to the position of the first item in the ListView. 
	 */ 
	private void onScrollChanged() { 
		View v = mListView.getChildAt(0);
		int top = (v == null) ? 0 : v.getTop();
 
		// This check is needed because when the first element reaches the top of the window, the top values from top are not longer valid.  
		if (mListView.getFirstVisiblePosition() == 0) {
			mStickyView.setTranslationY(
					Math.max(0, mPlaceholderView.getTop() + top));
 
			// Set the image to scroll half of the amount scrolled in the ListView. 
			mItemTop.setTranslationY(top / 2);