[英]Android ListView Scroll Down
我在服務中有一個媒體播放器,它會將消息發送到托管ListView
的活動。 假設我在ListView
有100首歌曲,每當我選擇一首歌曲時,該行的背景都會改變。 我需要所選的行在屏幕上始終可見。 我現在遇到的問題是,在完成當前歌曲后,我的服務將轉到下一首歌曲,並選擇該行。 但是,當所選歌曲在屏幕上不可見時,它不會向下滾動。 當它檢測到當前歌曲是用戶在屏幕上可以看到的最后一首歌曲時,我需要它自動滾動。
這是我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator" >
<TextView
android:id="@+id/txt_song"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txt_singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_song" />
</RelativeLayout>
下面的代碼來自我的活動。 我的服務類中有一個處理程序,當歌曲完成並轉到下一首歌曲時,該處理程序會向活動發送消息。
private void updateUI(Intent serviceIntent){
int songPos = serviceIntent.getIntExtra("songpos", 0);
mListSongs.setItemChecked(songPos, true);
//mListSongs.setSelection(songPos);
}
如果我使用mListSongs.setSelection(songPos)
,則始終將歌曲選在頂部,但這不是我想要的。
您可以為此使用ListView.smoothScrollToPosition()
。
視圖將滾動以顯示指示的位置。
private void updateUI(Intent serviceIntent){
int songPos = serviceIntent.getIntExtra("songpos", 0);
mListSongs.setItemChecked(songPos, true);
mListSongs.smoothScrollToPosition(songPos);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.