【问题标题】:How can i check that last item of listview is visible or not..?我如何检查列表视图的最后一项是否可见..?
【发布时间】:2015-08-06 06:13:29
【问题描述】:

我的要求很简单。 在我的应用程序中有一个列表视图,其中包含聊天消息和屏幕顶部的箭头图标。新项目将被动态添加,我想通过检查某些条件在新消息到达时显示此箭头图标,

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.   

我使用了一个布尔值来检查 listvew 的位置状态, 当新消息到达时,我正在检查,

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

当用户滚动列表视图时,会使用以下代码,

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

但是这段代码不起作用..有什么想法吗..?

【问题讨论】:

标签: android listview count visible listitem


【解决方案1】:

显示数据是否不足的指示器:在显示之前先检查最后一个可见位置是否不等于列表视图长度。

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

检查新项目是否可见:如果新项目被添加到顶部,检查它是否小于第一个可见位置

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

如果它被添加到底部,请检查它是否大于最后一个可见位置。

int itemPos = lv.getCount();
if(itemPos > lv.getLastVisiblePosition())
    // show the indicator

【讨论】:

  • 这不起作用..我试过这个..但有时当我处于列表视图的中间时,我向列表视图添加了新项目,它会滚动到底部而不显示箭头指标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多