Android 如何检查my ListView是否有可滚动的项目数?

Android 如何检查my ListView是否有可滚动的项目数?,android,android-listview,Android,Android Listview,如何知道ListView是否有足够数量的项目,以便可以滚动 例如,如果我的ListView上有5个项目,那么所有项目都将显示在一个屏幕上。但如果我有7个或更多,我的ListView开始滚动。如何知道列表是否可以通过编程方式滚动?使用,通过将回调应用于,您可以使用预定义的常量确定是否正在滚动,并相应地处理情况。在android使用列表视图呈现屏幕之前,您无法检测到这一点。但是,您完全可以检测此后期渲染 boolean willMyListScroll() { if(listV

如何知道ListView是否有足够数量的项目,以便可以滚动


例如,如果我的ListView上有5个项目,那么所有项目都将显示在一个屏幕上。但如果我有7个或更多,我的ListView开始滚动。如何知道列表是否可以通过编程方式滚动?

使用,通过将回调应用于,您可以使用预定义的常量确定是否正在滚动,并相应地处理情况。

在android使用
列表视图呈现屏幕之前,您无法检测到这一点。但是,您完全可以检测此后期渲染

boolean willMyListScroll() {        
   if(listView.getLastVisiblePosition() + 1 == listView.getCount()) {
      return false;
   }

   return true;             
}

这样做的目的是检查
listView
可见窗口是否包含所有列表视图项。如果可以,则
列表视图
将永远不会滚动,并且
getLastVisiblePosition()
将始终等于列表的dataAdapter中的项目总数

Diegosan的回答无法区分最后一项在屏幕上是否部分可见。这是解决这个问题的办法

首先,必须在屏幕上呈现ListView,然后才能检查其内容是否可滚动。这可以通过ViewTreeObserver完成:

ViewTreeObserver observer = listView.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            if (willMyListScroll()) {
                 // Do something
            } 
        }
    });
下面是WillyListScroll()


我以前就是这样检查的

if (listView.getAdapter() != null
                    && listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1
                    && listView.getChildAt(listView.getChildCount() - 1).getBottom() == listView.getBottom())

如果答案为真,则列表位于底部

根据我对Mike Ortiz答案的评论,我认为他的答案是错误的:

getChildAt()仅统计屏幕上可见的子项。同时,getLastVisiblePosition返回基于适配器的索引。所以,如果lastVisiblePosition是8,因为它是列表中的第9项,而屏幕上只有4个可见项,那么您将遇到崩溃。通过调用getChildCount()进行验证,请参阅。getChildAt的索引与数据集的索引不同。看看这个:

以下是我的解决方案:

public boolean isScrollable() {
        int last = listView.getChildCount()-1; //last visible listitem view
        if (listView.getChildAt(last).getBottom()>listView.getHeight() || listView.getChildAt(0).getTop()<0) { //either the first visible list item is cutoff or the last is cutoff
                return true;
        }
        else{
            if (listView.getChildCount()==listView.getCount()) { //all visible listitem views are all the items there are (nowhere to scroll)
                return false;
            }
            else{ //no listitem views are cut off but there are other listitem views to scroll to
                return true;
            }
        }
    }
public boolean isScrollable(){
int last=listView.getChildCount()-1;//最后可见的listitem视图

如果(listView.getChildAt(last).getBottom()>listView.getHeight()| | | listView.getChildAt(0).getTop()
boolean listBiggerThanWindow=appHeight-50这是我为在listView最后一行之后显示图片而编写的代码:

public class ShowTheEndListview
{
    private ImageView the_end_view;
    private TabbedFragRootLayout main_layout;
    private ListView listView;
    private float pas;
    private float the_end_img_height;
    private int has_scroll = -1;

    public ShowTheEndListview(float height)
    {
        the_end_img_height = height;
        pas = 100 / the_end_img_height;
    }

    public void setData(ImageView the_end_view, TabbedFragRootLayout main_layout, ListView listView)
    {
        this.the_end_view = the_end_view;
        this.main_layout = main_layout;
        this.listView = listView;
    }

    public void onScroll(int totalItemCount)
    {
        if(totalItemCount - 1 == listView.getLastVisiblePosition())
        {
            int pos = totalItemCount - listView.getFirstVisiblePosition() - 1;
            View last_item = listView.getChildAt(pos);

            if (last_item != null)
            {
                if(listHasScroll(last_item))
                {
//                        Log.e(TAG, "listHasScroll TRUE");
                }
                else
                {
//                        Log.e(TAG, "listHasScroll FALSE");
                }
            }
        }
    }

    private boolean listHasScroll(View last_item)
    {
        if(-1 == has_scroll)
        {
            has_scroll = last_item.getBottom() > (main_layout.getBottom() - the_end_img_height - 5) ? 1 : 0;
        }

        return has_scroll == 1;
    }

    public void resetHasScroll()
    {
        has_scroll = -1;
    }
}

AbsListView包括以下内容:

/**
 * Check if the items in the list can be scrolled in a certain direction.
 *
 * @param direction Negative to check scrolling up, positive to check scrolling down.
 * @return true if the list can be scrolled in the specified direction, false otherwise.
 * @see #scrollListBy(int)
 */
public boolean canScrollList(int direction);
您需要重写layoutChildren()并在其中使用它:

@Override
protected void layoutChildren() {
    super.layoutChildren();
    isAtBottom = !canScrollList(1);
    isAtTop = !canScrollList(-1);
}

你为什么需要知道这一点?毕竟,答案可能会随着方向的改变而改变,答案也可能会根据行中的内容而改变(不同的行布局,
wrap\u content
根据散文的高度变化).你想实现什么?5个项目可能不会在hdpi 800x480上滚动,但可能会在ldpi 240x320上滚动,让安卓为你处理滚动!事实上,我的意思是,Commonware的想法在我脑海中闪过,-你为什么要这样做?@Commonware,我完全理解。我想在表演的底部显示一个页脚有效性。如果ListView滚动,我想将页脚图像作为ListView的页脚视图。否则,我可以设置另一个放置在活动布局末尾的页脚的可见性。@t0mm13b,是的,我有不同屏幕大小的问题,这就是我想知道的原因。此外,列表中的项目数可能不同,我需要显示一个页脚r视图取决于卷轴。为什么人们质疑这个问题背后的动机,这是一个很好的问题!我也想知道,因为如果ListView的内容比屏幕长,我想添加一些内容(屏幕外)在列表视图的底部。只有当事件发生时,我才会收到回调,不是吗?但即使在用户尝试滚动列表之前,布局也必须更改。您好,谢谢。但是如果最后一项在屏幕上半可见,那么列表视图也会滚动。删除“+1”会吗Diegosan答案工作的一部分?不,这不会使他的答案工作。请检查下面的我的答案。极好的解决方案-需要在getChildAt(pos)的willMyListScroll()方法中添加空检查。如果列表为空,它将崩溃:我相信这是错误的。getChildAt()仅统计屏幕上可见的子项。同时,getLastVisiblePosition返回基于适配器的索引。因此,如果lastVisiblePosition是8,因为它是列表中的第9项,而屏幕上只有4个可见项,则会发生崩溃。请调用getChildCount()验证它请参阅。getChildAt的索引与数据集的索引不同。请检查:在listView中崩溃。getChildAt(pos)。getBottom()此解决方案甚至在您滚动到列表上的某个点或旋转手机后也可以使用。它不限于在初次加载列表后立即拨打电话。您使用的是appHeight-50。appHeight是什么,它的度量值是什么(px、dp、dip..等)。请添加更多信息。
/**
 * Check if the items in the list can be scrolled in a certain direction.
 *
 * @param direction Negative to check scrolling up, positive to check scrolling down.
 * @return true if the list can be scrolled in the specified direction, false otherwise.
 * @see #scrollListBy(int)
 */
public boolean canScrollList(int direction);
@Override
protected void layoutChildren() {
    super.layoutChildren();
    isAtBottom = !canScrollList(1);
    isAtTop = !canScrollList(-1);
}