Android ListView - stop scrolling at 'whole' row position

前端 未结 7 1450
生来不讨喜
生来不讨喜 2020-12-29 11:46

Sorry for the confusing title, I cannot express the problem very concisely...

I have an Android app with a ListView that uses a circular / \"infinite\" adapter, whi

7条回答
  •  星月不相逢
    2020-12-29 12:01

    You can get the visible dimensions of a child using the getChildVisibleRect method. When you have that, and you get the total height of the child, you can scroll to the appropriate child.

    In the example below I check whether at least half of the child is visible:

    View child = lv.getChildAt (0);    // first visible child
    Rect r = new Rect (0, 0, child.getWidth(), child.getHeight());     // set this initially, as required by the docs
    double height = child.getHeight () * 1.0;
    
    lv.getChildVisibleRect (child, r, null);
    
    if (Math.abs (r.height ()) < height / 2.0) {
        // show next child
    }
    
    else {
        // show this child
    }
    

提交回复
热议问题