How do I make a ListView update a title at the top, as I scroll the scroll?

3

I do not know how to explain very well what I want without images, but I need to learn how to make a ListView that changes title as you go down ... It's basically like in HTML when you make long pages and separate the sections by an anchor, where the ListView is divided by sections and as each section goes down, the section title is updated ...

    
asked by anonymous 31.10.2014 / 13:45

1 answer

2
  • 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;
                }
            });
    
        
    02.03.2016 / 12:55