确定显示的条目是listview

时间:2013-07-09 07:56:17

标签: android listview scroll

我正在尝试在单击按钮时在列表视图中发送数据。

但是我的列表视图一次显示一行,一行显示一行,一行显示一行。有没有办法可以确定哪一行显示部分,哪一行显示完全。

我能够获得仅显示的索引。有另一种方法吗?

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {


    if (scrollState == SCROLL_STATE_IDLE){
        Rect r = new Rect ();
        View child = recordListview.getChildAt(view.getFirstVisiblePosition());    // first visible child
        if (child == null)
            return; 
        double height = child.getHeight () * 1.0;

        recordListview.getChildVisibleRect (child, r, null);
        Log.d("Visible1 ", view.getFirstVisiblePosition() + "  "  + height + "  " + r.height()  );

        if (Math.abs (r.height ()) < height / 2.0) {
                    // show next child
            recordListview.smoothScrollToPosition(view.getFirstVisiblePosition()+1);
            Log.d("Visible1 Location", view.getFirstVisiblePosition() +1+ "");
        }

        else {
            recordListview.smoothScrollToPosition(view.getFirstVisiblePosition());
            Log.d("Visible1 Location", view.getFirstVisiblePosition()+ "");
        }

    }
}
});

1 个答案:

答案 0 :(得分:2)

似乎您已经错误地理解了getChildVisibleRect()的文档。

提到:

  

r输入矩形,在子坐标系中定义。将   被覆盖以包含所得到的可见矩形,表达   在全局(根)坐标

那么,如果您在子坐标中提供空矩形,那么它只能被翻译成空的可见直肠,对吗?

对我来说,这段代码似乎有效:

recordListview.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(final AbsListView view, final int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE) {
            final View child = recordListview.getChildAt(view.getFirstVisiblePosition());

            if (child == null) {
                return;
            }

            final Rect r = new Rect (0, 0, child.getWidth(), child.getHeight());
            final double height = child.getHeight () * 1.0;

            recordListview.getChildVisibleRect(child, r, null);
            Log.d("Visible1 ", view.getFirstVisiblePosition() + "  "  + height + "  " + r.height());

            if (Math.abs (r.height ()) < height / 2.0) {
                // show next child
                recordListview.smoothScrollToPosition(view.getFirstVisiblePosition()+1);
                Log.d("Visible1 Location", view.getFirstVisiblePosition() +1+ "");
            } else {
                recordListview.smoothScrollToPosition(view.getFirstVisiblePosition());
                Log.d("Visible1 Location", view.getFirstVisiblePosition()+ "");
            }
        }
    }

    @Override
    public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {
        // nothing to do here
    }
});

关于确定哪个视图完全可见而哪些视图不可见的初始问题,我建议使用以下代码:

@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
    if (scrollState == SCROLL_STATE_IDLE) {

        final int firstVisiblePosition = view.getFirstVisiblePosition();
        View child = recordListview.getChildAt(firstVisiblePosition);

        if (child == null) {
            return;
        }

        if (mListItemsOnScreen == 0) {
            // number of total visible items, including items which are not fully visible
            mListItemsOnScreen = (int) Math.ceil(((double)recordListview.getHeight()) / (child.getHeight() + recordListview.getDividerHeight()));
        }

        final Rect r = new Rect(0, 0, child.getWidth(), child.getHeight());
        final double height = child.getHeight();

        recordListview.getChildVisibleRect(child, r, null);
        Log.d("Visible1", " items till " + firstVisiblePosition + " are not visible");
        // Check top item
        Log.d("Visible1", firstVisiblePosition + " is visible " + (r.height() >= height ? " fully" : "partially"));
        // check bottom item
        child = recordListview.getChildAt(firstVisiblePosition + mListItemsOnScreen);

        if (child != null) {
            r.set(0, 0, child.getWidth(), child.getHeight());
            recordListview.getChildVisibleRect(child, r, null);

            Log.d("Visible1", " items from " + firstVisiblePosition + " till " + (firstVisiblePosition + mListItemsOnScreen) + " are fully visible");
            Log.d("Visible1", (firstVisiblePosition + mListItemsOnScreen) + " is visible " + (r.height() >= height ? " fully" : "partially"));
        } else {
            Log.d("Visible1", " items from " + firstVisiblePosition + " till " + (firstVisiblePosition + mListItemsOnScreen) + " are fully visible");
            Log.d("Visible1", (firstVisiblePosition + mListItemsOnScreen) + " is invisible ");
        }
    }
}