【问题标题】:How to avoid small jerk in view while Calling setSelection() after scrolling in onScrollStateChanged() method?滚动 onScrollStateChanged() 方法后调用 setSelection() 时如何避免视图出现小混蛋?
【发布时间】:2014-12-14 02:16:27
【问题描述】:

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

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

【问题讨论】:

    标签: android listview android-listview scroll


    【解决方案1】:

    尝试改用smoothScrollToPosition(position)Android ref.

    【讨论】:

      【解决方案2】:

      最后,通过传递第一个子元素的高度和持续时间(以毫秒为单位),我得到了基于 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;
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        • 1970-01-01
        • 2018-12-07
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 2020-06-22
        相关资源
        最近更新 更多