Android ListView向下滚动

时间:2015-09-25 16:13:21

标签: android scroll

我在服务中有一个媒体播放器,它会向我的活动发送消息,其中托管了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),歌曲将始终被选中,但这不是我想要的。

1 个答案:

答案 0 :(得分:0)

您可以使用ListView.smoothScrollToPosition()

  

视图将滚动,以显示指示的位置。

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