Listview.offsetTopAndBottom does not persist in Android… here is a solution to the problem.
Listview.offsetTopAndBottom does not persist in Android
When dragging an ImageView, I want both the ImageView and the ListView to move vertically and stay there. Below is my code. At first, it seems to work, but when I scroll the ListView up and down, the ListView jumps back to its original position. Can anyone tell me how to fix this?
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();
}
Solution
You need to override the onLayout and leave it there. The base ViewGroup (ListView) will recalculate the positioning there and override your offset. This means that, in your case, you need to extend the 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);
}