Hide and show the header of a listview when rolled

1

I'm developing an app for Android using Android Studio, and I want a listview with header where the header is hidden when the list scrolls down, and it appears when rolled up independently position of the scroll bar. This mechanism exists on the Facebook feeds screen as an example.

    
asked by anonymous 10.02.2014 / 14:03

2 answers

1

You can create a view as the header of your list and, when you recognize the scroll, hide / show this view.

final HeaderView header = new HeaderView(getActivity(), json);
mListView.addHeaderView(header);
setOnScrollListener(new OnScrollListener(){
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        // TODO Auto-generated method stub
    }
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub

        if(scrollState == 0) {
            Log.i("a", "scrolling stopped...");

            if (view.getId() == mListView.getId()) {
                final int currentFirstVisibleItem = mListView.getFirstVisiblePosition();
                if (currentFirstVisibleItem > mLastFirstVisibleItem) {
                    header.setVisibility(View.VISIBLE);
                } else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
                    header.setVisibility(View.GONE)
                }
                mLastFirstVisibleItem = currentFirstVisibleItem;
            } 
        }
     }
 });
    
11.02.2014 / 14:59
0

You create a view that will be your header and gives an addHeaderView:

    HeaderView header = new HeaderView(getActivity(), json);
    mListView.addHeaderView(header);
    
10.02.2014 / 15:29