【问题标题】:Listview.offsetTopAndBottom won't persist in AndroidListview.offsetTopAndBottom 不会在 Android 中持续存在
【发布时间】:2012-07-21 13:41:43
【问题描述】:

当拖动 ImageView 时,我希望 ImageView 和 ListView 都垂直移动并停留在那里。下面是我的代码。起初,它似乎可以工作,但是当我上下滚动列表视图时,列表视图会跳回原来的位置。谁能告诉我如何解决这个问题?

class MyOnGestureListener implements OnGestureListener{

  public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {

  mImageView.scrollBy(0, (int)distanceY);
  mListView.offsetTopAndBottom((int)-distanceY);
  mListView.invalidate();
  }

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    您需要覆盖 onLayout 并将其持久化。基本 ViewGroup (ListView) 将在那里重新计算定位并覆盖您的偏移量。这意味着,在您的情况下,您需要扩展 ListView 以覆盖它。

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int offset = getMenuState() != MenuState.Closed ? getContentView().getLeft() : 0;
    
        super.onLayout(changed, left, top, right, bottom);
    
        /*
         * Lazily offset the view - rather than comparing the child views to find the
         * content view
         */
        getContentView().offsetLeftAndRight(offset);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 2017-11-17
      • 2018-03-27
      • 2020-05-12
      • 2023-04-03
      相关资源
      最近更新 更多