繁体   English   中英

Android ListView向下滚动

[英]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.

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