18

ListView がスクロールできるように十分な数の項目があるかどうかは、どうすればわかりますか?

たとえば、ListView に 5 つの項目がある場合、すべてが 1 つの画面に表示されます。しかし、7 つ以上ある場合、ListView はスクロールを開始します。リストをプログラムでスクロールできるかどうかはどうすればわかりますか?

4

8 に答える 8

12

ディエゴサンの答えは、最後のアイテムが画面に部分的に表示されている場合を区別できません。これがその問題の解決策です。

まず、コンテンツがスクロール可能かどうかを確認する前に、ListView を画面にレンダリングする必要があります。これは、ViewTreeObserver で実行できます。

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

        @Override
        public void onGlobalLayout() {
            if (willMyListScroll()) {
                 // Do something
            } 
        }
    });

そしてここにあるwillMyListScroll()

boolean willMyListScroll() {   
    int pos = listView.getLastVisiblePosition();
    if (listView.getChildAt(pos).getBottom() > listView.getHeight()) {
        return true;
    } else {
        return false;
    }
}
于 2014-03-12T21:04:29.077 に答える
3

Mike Ortiz の回答に対する私のコメントによると、彼の回答は間違っていると思います。

getChildAt() は、画面に表示されている子のみをカウントします。一方、getLastVisiblePosition はアダプタに基づいてインデックスを返します。したがって、lastVisiblePosition がリストの 9 番目の項目であり、画面に表示される項目が 4 つしかないために 8 である場合、クラッシュが発生します。getChildCount() を呼び出して確認し、確認してください。getChildAt のインデックスは、データ セットのインデックスと同じではありません。これをチェックしてください: ListView getChildAt は、表示されている子に対して null を返します

これが私の解決策です:

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;
            }
        }
    }
于 2015-02-03T04:47:03.790 に答える
1

これは、リストビューの最後の行の後に画像を表示するために私が書いたコードです:

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;
    }
}
于 2015-07-06T16:18:35.510 に答える
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);
}
于 2016-08-25T18:08:23.693 に答える
1

Android がlistView. ただし、このポストレンダリングは絶対に検出できます。

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

   return true;             
}

これが行うことは、listView表示されているウィンドウにすべてのリスト ビュー アイテムが含まれているかどうかを確認することです。可能な場合、listViewはスクロールせず、getLastVisiblePosition()常にリストの dataAdapter 内のアイテムの総数と等しくなります。

于 2013-01-24T08:23:51.437 に答える
0

これが私がチェックしていた方法です

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

true の場合、リストは一番下にあります

于 2014-07-16T08:24:00.413 に答える
-1

setOnScrollListenerを使用します。コールバックをOnScrollListenerに適用することにより、事前定義された定数を使用してスクロールが行われているかどうかを判断し、それに応じて状況を処理できます。

于 2012-06-23T22:58:31.060 に答える
-2
 boolean listBiggerThanWindow = appHeight - 50 <= mListView.getHeight();

            Toast.makeText(HomeActivity.this, "list view bigger that window? " + listBiggerThanWindow, Toast.LENGTH_LONG)
                    .show();

            if (listBiggerThanWindow) {
                // do your thing here...
               }

View で post(Runnable) を呼び出すことにより、onCreate() でディメンションを取得できます。

于 2015-03-24T15:49:19.350 に答える