开发者

Calculate scrollbar height in grid with varied row height

I've grid with a lot of rows (e.g. 1 000 000). Height of each row may be unique. But most of rows has same height. So it's not possible to determine height of each row and get total grid height.

I need implement smooth vertical scrolling over this grid, not only jump over row, because row can be higher than visible area.

My solution is:

  1. get number of rows
  2. each row is divided into 10 parts
  3. => scroll bar max value is (number of rows)*10
  4. from scroll position I get :
    • first visible row = (scroll position) / 10
    • first visible row shift = (scroll position) % 10

This work fine, if all rows has +- same height. If there is one row with height 500 px and other has 25 px scroll looks awful.

Has anybody suggestion how to better solve this problem?

Grid 开发者_Go百科is here : http://img560.imageshack.us/img560/7775/scroll.png


Let the scroll be in pixel units:

  • Sum the total height of all the rows and set the scrollbar max value to that value.
  • Cache the first visible row index in a variable.
  • When the user scrolls up or down, you can scan sequentially from the current first visible row to find the new one. This gives amortized constant-time work per update for sequential read.
  • You won't do random access (e.g. scroll to row number N) frequently so doing a linear search when you do is fine. If you need something faster (I doubt that) then you can pre compute the partial sums of row heights and do binary search.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜