gpt4 book ai didi

java - Listview.offsetTopAndBottom 不会在 Android 中持续存在

转载 作者:太空宇宙 更新时间:2023-11-03 13:00:55 28 4
gpt4 key购买 nike

当拖动 ImageView 时,我希望 ImageView 和 ListView 都垂直移动并停留在那里。下面是我的代码。起初,它似乎有效,但是当我上下滚动 ListView 时, 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();
}

最佳答案

您需要覆盖 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);
}

关于java - Listview.offsetTopAndBottom 不会在 Android 中持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11592683/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com