-1

I have added a Button at the bottom of screen. It is a "Go To Top" button. I want to make it hidden when user is at top of the list i.e on first row, and show it when he scrolls down to beyond second row.

I can add onscroll listner to the ListView. But I am not sure how to check for row number

Can anyone provide an example how to achieve it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Abu Qatada
  • 211
  • 3
  • 16

2 Answers2

2

you can use setOnScrollListener

setOnScrollListener

Set the listener that will receive notifications every time the list scrolls.

sample code

listview.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {


        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            // add here your logic like this
            int lastItem = firstVisibleItem + visibleItemCount;
            if (firstVisibleItem < 2) {

                fab.setVisibility(View.INVISIBLE);
            }else {
                fab.setVisibility(View.VISIBLE);
            }
        }
    });
Community
  • 1
  • 1
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

You can try something like this

Find out if ListView is scrolled to the bottom?

private int preLast;
// Initialization stuff.
yourListView.setOnScrollListener(this);

// ... ... ...

@Override
public void onScroll(AbsListView lw, final int firstVisibleItem,
        final int visibleItemCount, final int totalItemCount)
{

    switch(lw.getId()) 
    {
        case R.id.your_list_id:     

            // Make your calculation stuff here. You have all your
            // needed info from the parameters of this function.

            // Sample calculation to determine if the last 
            // item is fully visible.
            final int lastItem = firstVisibleItem + visibleItemCount;

            if(lastItem == totalItemCount)
            {
                if(preLast!=lastItem)
                {
                    //to avoid multiple calls for last item
                    Log.d("Last", "Last");
                    preLast = lastItem;
                }
            }
    }
}