Skip to content

Commit

Permalink
播放列表自动定位到当前播放位置
Browse files Browse the repository at this point in the history
  • Loading branch information
wangchenyan committed Mar 28, 2024
1 parent da7292f commit 57c8d9f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.View
import android.view.ViewGroup
import androidx.core.text.buildSpannedString
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -31,6 +32,7 @@ import javax.inject.Inject
class CurrentPlaylistFragment : BottomSheetDialogFragment() {
private val viewBinding by viewBindings<FragmentCurrentPlaylistBinding>()
private val adapter by lazy { RAdapter<SongEntity>() }
private val layoutManager by lazy { LinearLayoutManager(requireContext()) }

@Inject
lateinit var audioPlayer: AudioPlayer
Expand Down Expand Up @@ -71,6 +73,7 @@ class CurrentPlaylistFragment : BottomSheetDialogFragment() {
}

adapter.register(CurrentPlaylistItemBinder(audioPlayer))
viewBinding.recyclerView.layoutManager = layoutManager
viewBinding.recyclerView.adapter = adapter
}

Expand All @@ -97,8 +100,17 @@ class CurrentPlaylistFragment : BottomSheetDialogFragment() {
}
adapter.refresh(playlist)
}
audioPlayer.currentSong.observe(this) {
audioPlayer.currentSong.observe(this) { song ->
adapter.notifyDataSetChanged()
val playlist = audioPlayer.playlist.value
if (playlist?.isNotEmpty() == true && song != null) {
val index = playlist.indexOf(song)
if (index == 0) {
layoutManager.scrollToPosition(index)
} else if (index > 0) {
layoutManager.scrollToPosition(index - 1)
}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/fragment_current_playlist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,5 @@
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_current_playlist" />
</com.hjq.shape.layout.ShapeLinearLayout>

0 comments on commit 57c8d9f

Please sign in to comment.