Skip to content Skip to sidebar Skip to footer

Android ListView: Detect If ListView Data Fits On Screen Without Scrolling

Possible Duplicate: Android: using notifyDataSetChanged and getLastVisiblePosition - when the listView is actually updated? I have a simple ListView with only a few entries. Dep

Solution 1:

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.

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

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

listView.post(fitsOnScreen);

Solution 2:

Why are you looking for problems? Instead of your idea you can use 3 layouts:

  1. Widgets if necessary - header
  2. ListView - main layout
  3. ImageView or other widgets - footer


ListView lv = (ListView) findViewById(R.id.listView);
View header = getLayoutInflater().inflate(R.layout.header_layout, null);
View footer = getLayoutInflater().inflate(R.layout.foorer_layout, null);
lv.addHeaderView(headerComment);
lv.addFooterView(footerComment);

Post a Comment for "Android ListView: Detect If ListView Data Fits On Screen Without Scrolling"