본문 바로가기

프로그래밍/안드로이드

[안드로이드 - ListView ] 아이템 추가시 스크롤의 위치 문제

안드로이드에서 여러개의 데이터를 보여줄때 많이 사용되는 ListView .
허나! 사용해 보신분들은 알겠지만 참 원하는게 많은 놈입니다.

이번에 이런저런 장난을 치면서 귀찮은거 하나.  데이터가 추가되면 왜 스크롤은 맨위로 올라가느냐.

난 하고 싶은것이 안드로이드 마켓에서 보여지는 ListView처럼 데이터가 추가가 되더라도 현재 위치가 보여지게 하고 싶은데...

API를 뒤져야죠..

public void setSelection (int position)

Since: API Level 1

Sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. If the specified selection position is less than 0, then the item at position 0 will be selected.

Parameters
position Index (starting at 0) of the data item to be selected.


그래서 찾은 녀석이 이놈입니다. 자바단에서 추가를 해주게 되면 되죠. 하지만 우연히 구글링을 하다가 발견된 또 하나의 좋은 녀석이 있었습니다.

바로 ListView 의 상위 추상클래스 중의 하나인 AbsListView 안에 들어 있는  setTranscriptMode () 이놈 입니다.

public void setTranscriptMode (int mode)

Since: API Level 1

Puts the list or grid into transcript mode. In this mode the list or grid will always scroll to the bottom to show new items.

Parameters
mode the transcript mode to set
간단히

TRANSCRIPT_MODE_DISABLED ( Constant Value: 0 (0x00000000)  )
 - Disables the transcript mode

TRANSCRIPT_MODE_NORMAL ( Constant Value: 1 (0x00000001) )
 - The list will automatically scroll to the bottom when a data set change notification is received and only if the last item is already visible on screen

TRANSCRIPT_MODE_ALWAYS_SCROLL  ( Constant Value: 2 (0x00000002)  )
 - The list will automatically scroll to the bottom, no matter what items are currently visible.

[출처] - 위의 정보는 http://dmh11.tistory.com/68  에 게시된 글을 바탕으로 작성되었습니다.