简体   繁体   中英

Android listview scroll listener last item position

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

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
            }


        }   

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
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM