简体   繁体   English

Android:ListView条件滚动

[英]Android: ListView conditional scrolling

I have a 2 pane layout. 我有2个窗格布局。 On the left is a ListView , on the rights some content linked with the list. 左边是ListView ,右边是与列表链接的某些内容。 The content is scrollable. 内容是可滚动的。 If content moves forward, I want the list to go forward only if this item is not yet displayed. 如果内容向前移动,则仅当此项目尚未显示时,我希望列表向前。 This prevents that the list has to update on every content change. 这样可以防止列表必须在每次内容更改时进行更新。 I also want the list to go ahead one full page. 我也希望列表能排整整一页。 Is there something like 是否有类似的东西

listView.isThisElementCurrentlyShown(int nr)

and

listView.moveAheadOnePage(int direction)

?

Thanks in advance. 提前致谢。

Here is an example of isThisElementCurrentlyShown(Element e) : 这是isThisElementCurrentlyShown(Element e)的示例:

    public boolean isThisElementCurrentlyShown(Element e){
                ListView lv = getListView();
                int start = lv.getFirstVisiblePosition();
                for(int i=start, j=lv.getLastVisiblePosition();i<=j;i++){
                    if(e==lv.getItemAtPosition(i)){
                        return true;
                    }
                }
                return false;
}

This will tell you whether the element is visible or not. 这将告诉您该元素是否可见。

For the moveAheadOnePage you should be able to use listview's built in functions getFirstVisiblePosition() and getLastVisiblePosition() to calculate the number of rows visible on that device, then advance the listview ahead that many positions. 对于moveAheadOnePage您应该能够使用listview的内置函数getFirstVisiblePosition()getLastVisiblePosition()来计算该设备上可见的行数,然后将listview向前推进许多位置。 Something like this pseudocode (you will have to write this one): 类似于此伪代码(您将不得不编写此伪代码):

public void moveAheadOnePage(int direction){//direction: 0-up, 1-down
    int numVisibleRows = getLastVisiblePosition() - getFirstVisiblePosition();
    this.setSelected(currentSelection + numVisibleRows) // +/- depending on direction
}

Mind you these are both expensive calls to make while scrolling through a listview, so you will definitely have to implement wisely~ 请注意,在滚动列表视图时,这些都是昂贵的调用,因此,您一定要明智地实现!

EDIT: Updated code to include return false 编辑:更新代码以包括return false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM