1

My requirement is simple. In my application there is a listview with chat messages and a arrow icon at top of the screen. New items will be added dynamically and i want to show this arrow icon when new message arrives by checking some conditions,

1. Arrow will be shown if listview is not at bottom (ie last item is not visible).
2. Arrow will be dismissed when user scrolls listview to bottom.
3. Arrow will not be showed if listview is at bottom.   

I have used a boolean value for checking the position status of listvew, when new message arrives i am checking,

                   if(isAtBottom){
                   // Adding to list
                     mChatMessages.add(mNewMessage);
                     mAdapter.setListing(mChatMessages);
                     scrollListViewToBottom();
                   }else{
                     mChatMessages.add(mNewMessage);
                     mAdapter.setListing(mChatMessages);
                     startArrowIndicator();
                   }

And when user scrolls the listview the following code is used,

 @Override
        public void onScroll(AbsListView absListView, final int firstVisibleItem,
                             final int visibleItemCount, final int totalItemCount) {
            final int lastItem = firstVisibleItem + visibleItemCount;
            if(lastItem == totalItemCount) {
                stopArrowIndicator();
                isAtBottom = true;
            }else{
                isAtBottom = false;
            }
        }

But this code is not working.... Any idea..?

Nidheesh
  • 433
  • 10
  • 20

1 Answers1

1

Indicator showing if there is not enough data: Check first if the last visible position isn't equal to the listview length before showing it.

ListView lv = getListView();
if(lv.getCount() != lv.getLastVisiblePosition() + 1)
    // show the indicator

Check new item is visible or not: If the new item is added to the top check if it's less than the first visible position

int itemPos = 0;
if(itemPos < lv.getFirstVisiblePosition())
    // show the indicator

in case it's added to the bottom check if it's greater than the last visible position.

int itemPos = lv.getCount();
if(itemPos > lv.getLastVisiblePosition())
    // show the indicator
SaNtoRiaN
  • 2,212
  • 2
  • 15
  • 25
  • this is not working.. i had tried this one.. but some times when i have been middle of the listview, then i have added new item to listview it will scroll to bottom without showing the arrow indicator. – Nidheesh Oct 14 '15 at 10:26