Listview.offsetTopAndBottom将不会在Android中保留

Jas*_* O. 3 java android listview

拖动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();
  }
Run Code Online (Sandbox Code Playgroud)

Aus*_*son 5

您需要覆盖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);
}
Run Code Online (Sandbox Code Playgroud)