Create a OnTouchListener in your ListView and search for the ACTION_MOVE motionEvent
When the onTouch method is called, get the id of the first element visible in the listview with the getFirstVisiblePosition () method, and grab the object with the getItemAtPosition ()
To change the header, if you have a Toolbar in XML, just change it using setTitle () , if you do not have it, you can call it with the getSupportActionBar () / getActionBar () and use setTitle ()
listview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View button, MotionEvent motion) {
if ( motion.getAction() == MotionEvent.ACTION_MOVE) {
int firstVisibleID = listview.getFirstVisiblePosition();
YourObj obj = listview.getItemAtPosition(firstVisibleID);
getSupportActionBar().setTitle(obj.getStringForTitle());
return true;
}
return false;
}
});