在我的应用程序的recyclerview中,每个项目都有播放/暂停按钮来启动/停止媒体播放器。有许多事件,如下载进度,播放和停止音乐。我将使用广播接收器或以其他方式处理事件并立即更新UI。 我不知道在活动中访问播放或暂停按钮。
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
holder.downloading.setVisibility(View.INVISIBLE);
holder.equalizer.setVisibility(View.INVISIBLE);
holder.btnPlay.setVisibility(View.VISIBLE);
holder.btnPause.setVisibility(View.INVISIBLE);
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageButton btnPlay, btnPause;
TextView title, id;
private ImageView trackImage, downloaded;
private GifImageView equalizer, downloading;
private CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
equalizer = itemView.findViewById(R.id.eq);
downloading = itemView.findViewById(R.id.loading);
btnPlay = itemView.findViewById(R.id.play);
btnPause = itemView.findViewById(R.id.pause);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyOnClickListener.playOnClick(v, getAdapterPosition());
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyOnClickListener.pauseOnClick(v, getAdapterPosition());
}
});
}
和内部活动,这里我可以访问 btnPlay , btnPause ,...,以便在specefic时间将它们设置为可见或不可见:
adapter = new Adapter(tracks, this, new Adapter.MyAdapterListener() {
@Override
public void playOnClick(View v, int position) {
if (//true) {
adapter.notifyItemChanged(currentItem, //?);
}
// hide some views in recyclerview's item and show some others
} else {
//...
}
}
更新
for (int i = 0; i < tracks.size(); i++) {
Track track = tracks.get(i);
if (track.getPlayStatus()) {
track.setPlayStatus(false);
//tell the RecyclerView this data position has changes
adapter.notifyItemChanged(i);
break;
}
}
Track track = tracks.get(position);
track.setPlayStatus(true);
adapter.notifyItemChanged(position);
和 onBindViewHolder :
if (track.getPlayStatus()) {
holder.btnPlay.setVisibility(View.INVISIBLE);
holder.btnPause.setVisibility(View.VISIBLE);
//other sets you need
} else {
holder.btnPlay.setVisibility(View.VISIBLE);
holder.btnPause.setVisibility(View.INVISIBLE); }
答案 0 :(得分:1)
如果您需要更改特定行的项目,则不需要将任何内容传递给Activity
。
让我们说点击您的列表项进行播放。然后,您只需将数据项的相应isPlaying
标记(如果它尚未添加)添加到true
,如下所示,并通知适配器有更改:
@Override
public void playOnClick(View v, int position) {
// reset playing state for a previous track if only one item can be played at a time
for (int i = 0; i < trackList.size(); i++) {
Track track = trackList.get(i);
if (track.isPlaying()) {
track.setIsPlaying(false);
//tell the RecyclerView this data position has changes
adapter.notifyItemChanged(i);
break;
}
}
Track track = trackList.get(position);
track.setIsPlaying(true);
//initiate the RecyclerView redrawal
adapter.notifyItemChanged(position);
}
您的pauseOnClick
可能是:
@Override
public void pauseOnClick(View v, int position) {
Track track = trackList.get(position);
track.setIsPlaying(false);
//initiate the RecyclerView redrawal
adapter.notifyItemChanged(position);
}
在onBindViewHolder
中,根据您传递的适配器数据更改项目可见性:
@Override
public void onBindViewHolder(@NonNull Wkdapter.Holder holder, int position) {
Track track = trackListList.get(position);
if (track.isPlaying()) {
holder.btnPlay.setVisibility(View.VISIBLE);
holder.btnPause.setVisibility(View.INVISIBLE);
//other sets you need
} else {
holder.btnPlay.setVisibility(View.INVISIBLE);
holder.btnPause.setVisibility(View.VISIBLE);
//your other inits
}
}
答案 1 :(得分:1)
你应该跟踪当前的比赛位置来解决这个问题。 诀窍是刷新当前播放的视图以及新单击的视图
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private int currentPlayingPosition = -1;
private MyOnClickListener mMyOnClickListener;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// return viewholder
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if (position == currentPlayingPosition) {
holder.btnPlay.setVisibility(View.GONE);
holder.btnPause.setVisibility(View.VISIBLE);
} else {
holder.btnPlay.setVisibility(View.GONE);
holder.btnPause.setVisibility(View.VISIBLE);
}
// other bindings
}
@Override
public int getItemCount() {
// return size
}
public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
mMyOnClickListener = myOnClickListener;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageButton btnPlay, btnPause;
public MyViewHolder(View itemView) {
super(itemView);
// bind views
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
int prev = currentPlayingPosition;
currentPlayingPosition = position;
if (prev >= 0)
notifyItemChanged(prev); // refresh previously playing view
notifyItemChanged(currentPlayingPosition);
mMyOnClickListener.playOnClick(v, position);
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
notifyItemChanged(position);
currentPlayingPosition = -1;
mMyOnClickListener.pauseOnClick(v, position);
}
});
}
}
private interface MyOnClickListener {
void playOnClick(View v, int position);
void pauseOnClick(View v, int position);
}
}
答案 2 :(得分:0)
您可以将小部件作为参数传递给MyOnClickListener
函数,如下所示:
MyOnClickListener.playOnClick(v, btnPlay, getAdapterPosition());
答案 3 :(得分:-1)
尝试类似这样的内容
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private CardView oneway_card, CardView;
private ImageView logo, arrow;
private Context context;
private List<Onwardflights> objectlist;
private TextView arrival, departure, duration, amount, flightcode, arr_text, stops, stops_text;
public ViewHolder(View itemView, Context context, List<Onwardflights> objectlist) {
super(itemView);
itemView.setOnClickListener(this);
this.context = context;
this.objectlist = objectlist;
*Initialize here*
logo = (ImageView) itemView.findViewById(R.id.oneway_logo);
arrival = (TextView) itemView.findViewById(R.id.oneway_arrtime);
}
您的onclick方法
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Log.i("Position", "Postion " + position + " is clicked");
oneway_card.setCardBackgroundColor(Color.parseColor("#E0FFFF"));
//your code
}