在onScrollStateChanged()方法中滚动后调用setSelection()时如何避免视图中的小混蛋?

时间:2014-10-18 18:08:01

标签: android listview android-listview scroll

当我在onScrollStateChanged()方法中滚动后调用setSelection()时,它会在listView中发生振动或小的混蛋或跳转。现在我将索引传递给setSelection(),这是滚动后列表视图中的第一个可见项索引,但我没有在滚动后传递最后一个索引或列表视图中远处可见项的任何其他索引。

任何想法或建议如何在setSelection()中显示特定项目时如何减慢视图或避免小的混蛋...或如何根据滚动动作管理视图的速度。

2 个答案:

答案 0 :(得分:0)

请尝试使用smoothScrollToPosition(position)Android ref

答案 1 :(得分:0)

最后,基于smoothScrollBy(),通过传递第一个子元素的高度和持续时间(以毫秒为单位),我得到了我真正需要的答案。

通过执行以下3个步骤:

1.初始化两个变量以进行上下滚动:

int scrollingUp=0,scrollingDown=0;

2.然后根据滚动增加变量的值:

 @Override
public void onScroll(AbsListView view, int firstVisibleItem,
    int visibleItemCount, int totalItemCount) {

    if(mLastFirstVisibleItem<firstVisibleItem)
            {
                scrollingDown=1;
            }
            if(mLastFirstVisibleItem>firstVisibleItem)
            {
                scrollingUp=1;
            }
            mLastFirstVisibleItem=firstVisibleItem;
} 

3.然后在onScrollStateChanged()中进行更改:

@Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
        case SCROLL_STATE_IDLE:

            if(scrollingUp==1)
            {
                mainListView.post(new Runnable() {
                    public void run() {
                        View child = mainListView.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;
                        mainListView.getChildVisibleRect (child, r, null);
                        int dpDistance=Math.abs (r.height());
                        double minusDistance=dpDistance-height;
                        if (Math.abs (r.height()) < height/2)
                        {
                            mainListView.smoothScrollBy(dpDistance, 1500);
                        }       
                        else
                        {
                            mainListView.smoothScrollBy((int)minusDistance, 1500);
                        }
                        scrollingUp=0;

                    }
                });
            }
            if(scrollingDown==1)
            {
                mainListView.post(new Runnable() {
                    public void run() {

                        View child = mainListView.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;
                        mainListView.getChildVisibleRect (child, r, null);
                        int dpDistance=Math.abs (r.height());
                        double minusDistance=dpDistance-height;
                        if (Math.abs (r.height()) < height/2)
                        {
                            mainListView.smoothScrollBy(dpDistance, 1500);
                        }       
                        else
                        {
                            mainListView.smoothScrollBy((int)minusDistance, 1500);
                        }
                        scrollingDown=0;
                    } 
                });
            }
        break;
        case SCROLL_STATE_TOUCH_SCROLL:

            break;
            }
        }