2014-11-05 65 views
0

我想滚动到特定项目,具体取决于用户点击的位置。我所有的列表视图都是不同的高度。请看下面的图片。滚动ListView到点击坐标,如QQ

Example Image

因此,如果在列表项1,用户点击允许假设,所以它的底部应该滚动,直到softkeyboard的顶部。

项目和键盘的高度各不相同。我已尝试getTopgetHeight,getMeasuredHeight,但迄今没有任何工作。

回答

1

Distance Formula

主要的一点是要找到点击UI,并在键盘顶部之间的距离。

这是距离公式。你可以用这个来实现你的功能。您需要

  • 点击坐标您的列表视图项
  • 您的ListView项的高度

假设下面是你的ListView,正在单击

int[] loc = new int[2]; 
lvItem_button.getLocationOnScreen(loc); 

  • 坐标布局底部的布局,在中将您的活动标记为,现在它将永远处于键盘的顶部,当键盘打开时。

    以同样的方式获取它的坐标。

    int[] cords= new int[2]; 
    keyboard_top_layout.getLocationOnScreen(cords); 
    
    
    double distance = Math.sqrt(Math.pow((cords[0] - loc[0]), 2) + Math.pow((cords[1] - loc[1]), 2)); 
    
    distance -= heightOfItem; //subtract height because we want to align bottom of item to top of keyboard 
    
    
    
    distance = Math.ceil(distance); 
    lvItem.smoothScrollBy((int) -distance, 500); //-distance because X is increasing at bottom and decreasing at top. 500 is delay in scrolling. so it's smooth scroll 
    

    全部完成:)

  • +0

    谢谢,这工作:) – 2014-11-05 02:45:07