确定条目显示的是列表视图的是、条目、视图、列表

2023-09-06 13:18:23 作者:离群的狼

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

不过我的列表视图显示2行一次一整排和一个部分行。有没有一种方法,我可以判断哪一行显示的部分,哪些是显示完全。

我能够得到的只是显示出该指数。有另一种方式?

  @覆盖
公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){


    如果(scrollState == SCROLL_STATE_IDLE){
        矩形R =新的矩形();
        查看孩子= recordListview.getChildAt(view.getFirstVisiblePosition()); //第一个可见子
        如果(孩子== NULL)
            返回;
        双高= child.getHeight()* 1.0;

        recordListview.getChildVisibleRect(子河,NULL);
        Log.d(Visible1,view.getFirstVisiblePosition()++高度++ r.height());

        如果(Math.abs(r.height())≤;高度/ 2.0){
                    //显示下一子
            recordListview.smoothScrollToPosition(view.getFirstVisiblePosition()+ 1);
            Log.d(Visible1位置,view.getFirstVisiblePosition()+1+);
        }

        其他 {
            recordListview.smoothScrollToPosition(view.getFirstVisiblePosition());
            Log.d(Visible1位置,view.getFirstVisiblePosition()+);
        }

    }
}
});
 

解决方案

看来你已经理解的 getChildVisibleRect()不正确。

它提到:

  

R中的输入矩形,在子定义的坐标系。将   覆盖包含所产生的可见矩形,前pressed   在全球(根)坐标

所以,如果你提供的子空矩形的协调,然后它可以被翻译成只能空可见rectagle,对吧?

对于我这个code似乎工作:

  recordListview.setOnScrollListener(新AbsListView.OnScrollListener(){
    @覆盖
    公共无效onScrollStateChanged(最终AbsListView认为,最终诠释scrollState){
        如果(scrollState == SCROLL_STATE_IDLE){
            最后查看孩子= recordListview.getChildAt(view.getFirstVisiblePosition());

            如果(孩子== NULL){
                返回;
            }

            最终的Rect R =新的Rect(0,0,child.getWidth(),child.getHeight());
            最终双高= child.getHeight()* 1.0;

            recordListview.getChildVisibleRect(子河,NULL);
            Log.d(Visible1,view.getFirstVisiblePosition()++高度++ r.height());

            如果(Math.abs(r.height())≤;高度/ 2.0){
                //显示下一子
                recordListview.smoothScrollToPosition(view.getFirstVisiblePosition()+ 1);
                Log.d(Visible1位置,view.getFirstVisiblePosition()+1+);
            } 其他 {
                recordListview.smoothScrollToPosition(view.getFirstVisiblePosition());
                Log.d(Visible1位置,view.getFirstVisiblePosition()+);
            }
        }
    }

    @覆盖
    公共无效onScroll(最终AbsListView认为,最终诠释firstVisibleItem,最终诠释visibleItemCount,最终诠释totalItemCount){
        // 在这儿无事可做
    }
});
 

对于如何确定哪种说法是完全可见的,哪些不是,我建议使用下面的code最初的问题:

  @覆盖
公共无效onScrollStateChanged(最终AbsListView认为,最终诠释scrollState){
    如果(scrollState == SCROLL_STATE_IDLE){

        最终诠释firstVisiblePosition = view.getFirstVisiblePosition();
        查看孩子= recordListview.getChildAt(firstVisiblePosition);

        如果(孩子== NULL){
            返回;
        }

        如果(mListItemsOnScreen == 0){
            //总人数可见的物品,包括不是完全可见的物品
            mListItemsOnScreen =(INT)Math.ceil(((双)recordListview.getHeight())/(child.getHeight()+ recordListview.getDividerHeight()));
        }

        最终的Rect R =新的Rect(0,0,child.getWidth(),child.getHeight());
        最终双高= child.getHeight();

        recordListview.getChildVisibleRect(子河,NULL);
        Log.d(Visible1,项目到+ firstVisiblePosition +不可见);
        //检查顶级项目
        Log.d(Visible1,firstVisiblePosition +可见+(r.height()> =身高充分:部分));
        //检查底部项目
        孩子= recordListview.getChildAt(firstVisiblePosition + mListItemsOnScreen);

        如果(孩子!= NULL){
            r.set(0,0,child.getWidth(),child.getHeight());
            recordListview.getChildVisibleRect(子河,NULL);

            Log.d(Visible1+ firstVisiblePosition +至+(firstVisiblePosition + mListItemsOnScreen来自项目)+是完全可见);
            Log.d(Visible1,(firstVisiblePosition + mListItemsOnScreen)+可见+(r.height()> =身高充分:部分));
        } 其他 {
            Log.d(Visible1+ firstVisiblePosition +至+(firstVisiblePosition + mListItemsOnScreen来自项目)+是完全可见);
            Log.d(Visible1,(firstVisiblePosition + mListItemsOnScreen)+是无形的);
        }
    }
}
 
学习元 学习元系统功能更新

i am trying to send data in a listview when a button is clicked.

However my listview show 2 row on at once one full row and one partial row . Is there a way i can determine which row is showing partial and which is showing fully.

I am able to get the index that is showing only. is there another approach ?

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

    }
}
});

解决方案

Seems You've understood documentation of getChildVisibleRect() incorrectly.

It mentions:

r The input rectangle, defined in the child coordinate system. Will be overwritten to contain the resulting visible rectangle, expressed in global (root) coordinates

So, if You're providing empty rectangle in the child coordinate then it can be translated only into empty visible rectagle, right?

For me this code seems to work:

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

Regarding initial question about determining which view is visible fully and which is not, I would suggest to use the following code:

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