简体   繁体   中英

How to set a scroll offset in a ListView

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM