Android 确定显示的条目是listview

Android 确定显示的条目是listview,android,listview,scroll,Android,Listview,Scroll,我试图在单击按钮时在列表视图中发送数据 但是,我的listview同时显示两行,一整行和一部分行。是否有办法确定哪一行显示部分,哪一行显示全部 我能够得到只显示的索引。还有别的办法吗 @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_IDLE){ Rect r = new Rect ();

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

但是,我的listview同时显示两行,一整行和一部分行。是否有办法确定哪一行显示部分,哪一行显示全部

我能够得到只显示的索引。还有别的办法吗

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

    }
}
});
@覆盖
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
如果(滚动状态==滚动状态空闲){
Rect r=新的Rect();
View child=recordListview.getChildAt(View.getFirstVisiblePosition());//第一个可见的子对象
if(child==null)
返回;
双倍高度=child.getHeight()*1.0;
recordListview.getChildVisibleRect(child,r,null);
Log.d(“Visible1”,view.getFirstVisiblePosition()+“”+height+“”+r.height());
如果(数学绝对值(r.height())
似乎您对文档的理解不正确

它提到:

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

您是否尝试过getChildVisibleRect()?@Sandstar我已使用中显示的示例更新了代码。这将导致我的listview不再可滚动。是否调用onScroll()?我认为您需要使用onScrollStateChanged(),检测SCROLL\u STATE\u IDLE,然后执行逻辑。@Sandstar Yes onscrolled被激发。然而,r.height始终为零。嗨,sandstar,我尝试了上面的解决方案。当我从项目1滚动到项目2时,从项目0滚动到项目1的效果很好。列表开始从1-2-1-2移动。。。毫不客气地<代码>07-10 02:49:46.930:D/Visible1(2772):1190.01902 07-10 02:49:46.930:D/Visible1位置(2772):107-10 02:49:46.980:D/Visible1(2772):1190.0522-10 02:49:46.980:D/Visible1位置(2772):207-10 02:49:47.040:D/Visible1位置(2772):1190.01902-10:49:47.070:47.072:113:1 190.0 52 2您想实现什么目标?事实上,你们的问题是‘如何确定哪一行显示部分’——你们能做到吗?是的,没错。我试图确定显示哪一行,然后滚动到显示面积最大的行。我只是好奇,为什么它会对项目0到1的滚动有效,而不会对后续滚动有效。我提供了代码来确定哪个视图可见,哪个视图不可见(或部分可见)。我认为2项是一种极端情况,可能使用Math.abs(r.height())@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 "); } } }