3

拖动 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();
  }
4

1 回答 1

5

You need to override onLayout and persist it there. The base ViewGroup (ListView) will recalculate positioning there and override your offset. Which means, in your case, you'll need to extend ListView to override it.

@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);
}
于 2012-10-17T22:06:26.360 回答