每当我播放一首歌曲时,我的“播放”按钮都会隐藏并且“停止”可见,但是当我单击其他视图的播放时,以前的视图应该从停止变为播放而不会发生
List<firebaseDatum> dataList;
Context activity;
String image, title;
// Media Player
private MediaPlayer mediaplayer = new MediaPlayer();
//itemclicklistener listener;
public Recycler_Adapter(Context activity, List<firebaseDatum> dataList) {
this.dataList = dataList;
this.activity = activity;
// this.listener=itemclicklistener;
}
public Recycler_Adapter(mainActivity activity, Object value, long childrenCount) {
}
@Override
public MyviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler, parent, false);
MyviewHolder mviewHolder = new MyviewHolder(itemView);
return mviewHolder;
}
@Override
public void onBindViewHolder(MyviewHolder holder, final int position) {
image = dataList.get(position).getImage();
title = dataList.get(position).getTitle();
holder.tv_title.setText(title);
Picasso.with((Context) activity).load(image).placeholder(R.drawable.no_cover).into(holder.iv_albumart);
if (!mediaplayer.isPlaying()) {
holder.tv_play.setVisibility(View.VISIBLE);
holder.tv_pause.setVisibility(View.GONE);
holder.progressbar.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return dataList.size();
}
public class MyviewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView tv_title, tv_play, tv_pause;
public ImageView iv_albumart;
TextView tv_download_button;
RelativeLayout rl_info;
ProgressBar progressbar;
public MyviewHolder(View itemView) {
super(itemView);
tv_title = itemView.findViewById(R.id.tv_title);
iv_albumart = itemView.findViewById(R.id.iv_albumart);
tv_download_button = itemView.findViewById(R.id.iv_download_button);
rl_info = itemView.findViewById(R.id.rl_info);
tv_play = itemView.findViewById(R.id.tv_play);
tv_pause = itemView.findViewById(R.id.tv_pause);
progressbar = itemView.findViewById(R.id.progress);
tv_download_button.setOnClickListener(this);
rl_info.setOnClickListener(this);
tv_title.setOnClickListener(this);
tv_play.setOnClickListener(this);
tv_pause.setOnClickListener(this);
}
@SuppressLint("StaticFieldLeak")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_download_button:
Intent download = new Intent(Intent.ACTION_VIEW, Uri.parse(dataList.get(getAdapterPosition()).getSong()));
activity.startActivity(download);
break;
case R.id.tv_title:
case R.id.rl_info:
Intent youtube = new Intent(Intent.ACTION_VIEW, Uri.parse(dataList.get(getAdapterPosition()).getYtlink()));
activity.startActivity(youtube);
break;
case R.id.tv_play:
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
tv_pause.setVisibility(View.GONE);
tv_play.setVisibility(View.GONE);
progressbar.setVisibility(View.VISIBLE);
// mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
@Override
protected Void doInBackground(Void... params) {
try {
mediaplayer.setDataSource(dataList.get(getAdapterPosition()).getSong());
mediaplayer.prepare();
} catch (IllegalArgumentException e) {
// tv_play.setVisibility(View.GONE);
// progressbar.setVisibility(View.GONE);
// tv_pause.setVisibility(View.VISIBLE);
e.printStackTrace();
} catch (SecurityException e) {
// tv_play.setVisibility(View.GONE);
// progressbar.setVisibility(View.GONE);
// tv_pause.setVisibility(View.VISIBLE);
e.printStackTrace();
} catch (IllegalStateException e) {
// tv_play.setVisibility(View.GONE);
// progressbar.setVisibility(View.GONE);
// tv_pause.setVisibility(View.VISIBLE);
e.printStackTrace();
} catch (IOException e) {
// tv_play.setVisibility(View.GONE);
// progressbar.setVisibility(View.GONE);
// tv_pause.setVisibility(View.VISIBLE);
e.printStackTrace();
}
mediaplayer.start();
return null;
}
protected void onPostExecute(Void result) {
tv_play.setVisibility(View.GONE);
progressbar.setVisibility(View.GONE);
tv_pause.setVisibility(View.VISIBLE);
mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaplayer.release();
tv_play.setVisibility(View.VISIBLE);
progressbar.setVisibility(View.GONE);
tv_pause.setVisibility(View.GONE);
}
});
}
}.execute();
break;
case R.id.tv_pause:
if (mediaplayer.isPlaying()) {
mediaplayer.stop();
mediaplayer.release();
tv_pause.setVisibility(View.GONE);
tv_play.setVisibility(View.VISIBLE);
}
break;
}
}
}
}
除此播放/停止可见性问题外,其他所有操作均正常。 非常感谢您的帮助。