我正在创建一个最近在其中播放过歌曲的音乐播放器播放列表。我正在存储歌曲的数组列表和正在共享首选项中播放的歌曲的索引。要获取最近播放的歌曲,我要做的是从共享首选项中检索数组列表和歌曲索引,并将其保存在另一个数组列表中。但问题是recyclerView一次只能显示一首歌曲。
例如,如果我播放歌曲A,recyclerView应该在第一位置显示歌曲A,如果我播放歌曲B,recyclerView应该在第一位置显示歌曲B,在第二位置显示歌曲A。但它只显示了第一个位置。
RecentlyPLayedsongs.java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(this);
View view6 = inflater.inflate(R.layout.activity_recently_played, null);
FrameLayout container6 = (FrameLayout) findViewById(R.id.container);
container6.addView(view6);
recyclerView_recently_played = findViewById(R.id.recyclerView_recently_played);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView_recently_played.setLayoutManager(linearLayoutManager);
StorageUtil storageUtil2 = new StorageUtil(getApplicationContext());
SongList=storageUtil2.loadAudio();
pos = storageUtil2.loadAudioIndex();
songInfoModel = SongList.get(pos);
RecentlyPlayedList.add(songInfoModel);
adapter1 = new Playlist_Recently_Added_Adapter(RecentlyPlayedList, getApplicationContext());
recyclerView_recently_played.setAdapter(adapter1);
}发布于 2017-11-19 14:17:02
您可以通过以下两种方式之一来完成此操作
list.add(o,currentSong);
这里传递的是0,这意味着当你播放一首歌曲时,这首歌将被添加到索引为0的位置,这意味着你最近播放的歌曲将总是在最前面。希望这对你有帮助。
注意:我已经用下面的代码验证了第二个解决方案,它可以正常工作
List<Integer> list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
Log.d(TAG, "firs time");
for (int a=0; a < list.size();a++ ) {
Log.d(TAG, "" + list.get(a));
}
list.add(0,4);
Log.d(TAG, "second time");
for (int a=0; a < list.size();a++ ) {
Log.d(TAG, "" + list.get(a));
}日志如下所述
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: firs time
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 1
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 2
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 3
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: second time
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 4
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 1
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 2
11-19 11:24:53.400 3681-3681/com.example.awaheed.datetime D/test: 3看看第二次编号在'0‘索引处被添加的日志,而不是在第一个位置上,剩余的项目被下推。希望这能说得通。
https://stackoverflow.com/questions/47374182
复制相似问题