Question

I need to set an offset in the listview. I've achieved it initially by using ScrollBy(X, Y), but as soon as I touch the list view it snaps back to zero (my offset in negative).

How can I maintain this offset?

Was it helpful?

Solution

If you want an empty space at the top of the ListView I would do it with a padding.

Add this to your ListView in the layout xml

android:clipToPadding="false"
android:paddingTop="25dp"

You can also do it programmatically but doing it in xml should always be preferable if possible:

float paddingTopInDp = 25.0f;

Resources resources = getResources();
float paddingTopInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, paddingTopInDp, resources.getDisplayMetrics());
listView.setClipToPadding(false);
listView.setPadding(0, (int)(paddingTopInPixels + 0.5f), 0, 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top