1

i have listview witch contain 40 items and i want to write scroll listener method.i mean.when scroll position will be last item position(40th item) i would to show Toast message. i wrote some code .i can show Toast message in a last item position,but then when i scroll up i again can show Toast message this is a my source

list.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {




        }

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

            if (list.getLastVisiblePosition() == totalItemCount - 1
                    ) {

                Toast.makeText(getActivity(), "Last", Toast.LENGTH_SHORT)
                        .show();


                try {

                } catch (Exception e) {
                }

            }

        }
    });

how i can change code to can show toast message only last item position? thanks

BekaKK
  • 1,797
  • 3
  • 35
  • 70

2 Answers2

6

Try this Hope it helps ..

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

            if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
            {
                //your Toast
            }


        }   
Rajesh Mikkilineni
  • 864
  • 10
  • 22
  • thank you.but nothink changed.when i scroll down last item ,then i can show Toast message but this message never hiden – BekaKK Jul 24 '14 at 08:29
1

This a detailed code for scroll listener

 ListView lv = (ListView)findViewById(R.id.list_view);
    lv.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, 
            int visibleItemCount, int totalItemCount) {
            //Check if the last view is visible
            if (++firstVisibleItem + visibleItemCount > totalItemCount) {
                //load more content
            }
        }
    });
Ranjithkumar
  • 669
  • 5
  • 15