0

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

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

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

@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()+ "");
        }

    }
}
});
4

1 回答 1

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 ");
        }
    }
}
于 2013-07-09T09:05:11.253 回答