Android的ListView控件:检测是否在屏幕上的ListView数据适合不滚动 [英] Android ListView: Detect if ListView data fits on screen without scrolling

查看:198
本文介绍了Android的ListView控件:检测是否在屏幕上的ListView数据适合不滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/6044796/android-using-notifydatasetchanged-and-getlastvisibleposition-when-the-listvi">Android:使用notifyDataSetChanged和getLastVisiblePosition - ?当ListView的实际更新

我有一个简单的ListView只有少数条目。根据装置的大小和方向,在ListView需要滚动或不

I have a simple ListView with only a few entries. Depending on the device size and orientation, the ListView needs to scroll or not.

我也曾经在屏幕上,这是不是真的有必要的图片。我想删除的图片,如果没有足够的空间为ListView无需滚动。

I have also a picture on the screen, which is not really necessary. I would like to remove the picture if there is not enough space for the ListView without scrolling.

当我填写我的活动的onCreate,该getLastVisiblePosition(ListView控件)尚未生效,则返回-1。因此,我不能比较最后一个可见条目列出计数。

When I fill my ListView in the onCreate of the activity, the getLastVisiblePosition() is not yet valid, it returns -1. Thus I can not compare last visible item to list count.

INT lastVisiblePos = listView.getLastVisiblePosition(); //返回-1

int lastVisiblePos = listView.getLastVisiblePosition(); // returns -1

(如果我点击一个按钮后重新检查,该值是正确的。)

(If I click on a button later to recheck, the value is correct.)

使用onScrollingListener太晚,作为图象不应该首先被显示。

Using the onScrollingListener is too late, as the picture should not be shown in the first place.

我正在开发的API级别10。

I am developing on API level 10.

有什么建议?

推荐答案

比较<一href="http://developer.android.com/reference/android/widget/AdapterView.html#getLastVisiblePosition%28%29"><$c$c>getLastVisiblePosition()与<一个href="http://developer.android.com/reference/android/widget/AdapterView.html#getCount%28%29"><$c$c>getCount()在一个可运行,以查看是否在屏幕上的整个的ListView适合只要它已被绘制。您也应该检查,如果最后可见行完全适合在屏幕上。

Compare getLastVisiblePosition() with getCount() in a Runnable to see if the entire ListView fits on the screen as soon as it has been drawn. You should also check to see if the last visible row fits entirely on the screen.

创建了Runnable:

Create the Runnable:

ListView listView;
Runnable fitsOnScreen = new Runnable() {
    @Override
    public void run() {
        int last = listView.getLastVisiblePosition();
        if(last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) {
            // It fits!
        }
        else {
            // It doesn't fit...
        }
    }
};

的onCreate()排队你在ListView控件的处理程序可运行:

In onCreate() queue your Runnable in the ListView's Handler:

listView.post(fitsOnScreen);

这篇关于Android的ListView控件:检测是否在屏幕上的ListView数据适合不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆