我有一个使用MediaPlayer类从互联网上播放音乐的应用程序。我使用了recyclerview,在每个项目上,它都有一个播放/按钮按钮来开始播放和暂停声音。当用户点击其他项目播放按钮时,如何更改播放按钮以暂停按钮?
public class Adapter extends RecyclerView.Adapter<Adapter.Viewholder> {
private List<Track> tracks;
private Context context;
public Adapter(List<Track> tracks, Context context) {
this.tracks = tracks;
this.context = context;
}
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.track_item, parent, false);
return new Viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull final Viewholder holder, final int position) {
holder.title.setText(tracks.get(position).getTitle());
holder.id.setText(tracks.get(position).getId());
Glide.with(context).load(tracks.get(position).getImgurl()).into(holder.trackImage);
holder.btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.btnPause.setVisibility(View.VISIBLE);
Intent intent = new Intent(context, Player.class);
intent.putExtra("URL", tracks.get(position).getMusicurl());
context.startService(intent);
}
});
holder.btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Player.killMediaPlayer();
holder.btnPause.setVisibility(View.INVISIBLE);
}
});
}
@Override
public int getItemCount() {
return tracks.size();
}
public static class Viewholder extends RecyclerView.ViewHolder {
TextView title, id;
ImageView trackImage;
ImageButton btnPlay;
public Viewholder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
id = itemView.findViewById(R.id.id);
trackImage = itemView.findViewById(R.id.trackImage);
btnPlay = itemView.findViewById(R.id.play);
}
}
}
答案 0 :(得分:0)
非常简单,只需遍历整个数据集,并将所有数据集设置为暂停,但当前的数据集除外。然后调用notifyDatasetChanged();
答案 1 :(得分:0)
你可以使用notifyDataSetChange() - 它会更新所有的recyclerView项目
使用notifyItemChanged(poition) - 它会更新特定位置。
注意:假设您的手机屏幕上有8个项目,并且您 将暂停作为第1个位置,第9个位置将显示为暂停。 要解决此问题,请尝试比较之前的song_title / artist名称 通知
答案 2 :(得分:0)
你必须使用 notifyDataSetChanged() - 它会重置所有的recyclerView项目 ,然后强> 使用 notifyItemChanged(poition)或 mList.get(getAdapterPosition()) - 它会更新具体位置。
答案 3 :(得分:0)
以下是示例代码:
public class MainActivity extends AppCompatActivity {
private TrackAdapter trackAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.reycleView);
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
trackAdapter = new TrackAdapter();
trackAdapter.setCallBack(new CallBackImpl());
trackAdapter.setTracks(getTracks());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(trackAdapter);
}
private List<Track> tracks;
private List<Track> getTracks() {
tracks = new ArrayList<>();
for (int i = 0; i < 50; i++) {
Track track = new Track();
track.setTrackName(" Track " + i);
track.setPlaying(false);
tracks.add(track);
}
return tracks;
}
int currentPosition = -1;
private class CallBackImpl implements TrackAdapter.CallBack {
@Override
public void clicked(int position) {
Track track = tracks.get(position);
tracks.get(position).setPlaying(!track.isPlaying());
if (currentPosition >-1) {
track = tracks.get(currentPosition);
tracks.get(currentPosition).setPlaying(!track.isPlaying());
}
trackAdapter.setTracks(tracks);
trackAdapter.notifyItemChanged(currentPosition);
trackAdapter.notifyItemChanged(position);
currentPosition = position;
}
}
}
TrackAdapter.java
public class TrackAdapter extends RecyclerView.Adapter<TrackAdapter.VH> {
private int current;
public void setCallBack(CallBack callBack) {
this.callBack = callBack;
}
private CallBack callBack;
interface CallBack
{
void clicked(int position);
}
public void setTracks(List<Track> tracks) {
this.tracks = tracks;
}
private List<Track> tracks;
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
return new VH(view);
}
@Override
public void onBindViewHolder(VH holder, int position) {
holder.setData(tracks.get(position));
}
@Override
public int getItemCount() {
if (tracks == null) {
return 0;
} else {
return tracks.size();
}
}
class VH extends RecyclerView.ViewHolder {
TextView textView;
TextView playView;
public VH(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.track);
playView = itemView.findViewById(R.id.trackplay);
playView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (callBack!=null)
{
callBack.clicked(getAdapterPosition());
}
}
});
}
void updateState(boolean isPlaying) {
if (isPlaying) {
current = getAdapterPosition();
playView.setText("Playing");
} else {
playView.setText("Stopped");
}
}
void setData(Track data) {
textView.setText(data.getTrackName());
updateState(data.isPlaying());
}
}
}
跟踪.java
public class Track {
private boolean isPlaying;
public String getTrackName() {
return trackName;
}
public void setTrackName(String trackName) {
this.trackName = trackName;
}
private String trackName;
public boolean isPlaying() {
return isPlaying;
}
public void setPlaying(boolean playing) {
isPlaying = playing;
}
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/track"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/trackplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
点击已在停止文字上实施。
答案 4 :(得分:0)
holder.btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Track.isPlaying()){
notifyItemChanged(currentItem, holder );
}
Track.setPlaying(true);
currentItem = holder.getAdapterPosition();
Log.i("TAG", "Position : " + currentItem);
holder.equalizer.setVisibility(View.VISIBLE);
holder.btnPause.setVisibility(View.VISIBLE);
holder.btnPlay.setVisibility(View.INVISIBLE);
Intent intent = new Intent(context, Player.class);
intent.putExtra("URL", tracks.get(position).getMusicurl());
intent.putExtra("ID", tracks.get(position).getId());
context.startService(intent);
}
});