2015-09-25 118 views
0

我有一個服務中的媒體播放器,它發送消息給我的活動,其中託管一個ListView。假設我在ListView中有100首歌曲,並且每次選擇歌曲時,該行的背景都會改變。我需要選中的行始終在屏幕上可見。我現在遇到的問題是,當前歌曲完成後,我的服務將轉到下一首歌曲,並選擇該行。但是,當所選歌曲在屏幕上不可見時,它不會向下滾動。當它檢測到當前歌曲是用戶可以在他的屏幕上看到的最後一首歌曲時,我需要它自動滾動。Android ListView向下滾動

這是我的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),這首歌總是會在頂部選擇,但是這不是我想要的。

回答

0

您可以使用ListView.smoothScrollToPosition()

該視圖將滾動顯示指定的位置。

private void updateUI(Intent serviceIntent){ 
    int songPos = serviceIntent.getIntExtra("songpos", 0); 
    mListSongs.setItemChecked(songPos, true); 
    mListSongs.smoothScrollToPosition(songPos); 
}