How to make a Listview update a title at the top, as I scroll the same?

Asked

Viewed 178 times

3

I don’t know how to explain very well what I want without pictures, 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 do long pages and separate sections by an anchor, where Listview is divided by sections and as each section descends, the section title is updated...

  • 1

    Something thus? Or a floating header at the top of the list?

  • That’s right @Paulo Rodrigues. Thank you very much.

  • I’m not sure if this is exactly what you’re looking for, but there are some libraries to keep the header of the list sections always visible. https://github.com/emilsjolander/StickyListHeaders
https://github.com/diegocarloslima/FloatingGroupExpandableListView

  • At this link showed you how to use ExpandableListView, it presents a result similar to what you described!

1 answer

2

  1. Create a Ontouchlistener in his Listview and look for motionEvent ACTION_MOVE
  2. When the onTouch method is called, take the id of the first visible element in the listview with the method getFirstVisiblePosition(), and take the object with the getItemAtPosition()
  3. To change the header if using a Toolbar in XML, only change it using setTitle(), if not, you can call her with the getSupportActionBar()/getActionBar() and use the 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;
            }
        });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.