gpt4 book ai didi

java - 如何更改不是我在 RecycleView 中单击的项目?

转载 作者:行者123 更新时间:2023-12-02 05:32:13 26 4
gpt4 key购买 nike

摘要

我在 RecycleView 中有一个 CardView,带有一个播放或停止歌曲的按钮,并且一次只能播放一首歌曲。因此,当我单击其中一个按钮时,所有其他按钮都应该有一个“播放”图标,而单击的按钮应该有一个“停止”图标。基本上,我需要更改我未单击的项目的图像。

问题

项目编译后,当同时播放多个歌曲时,歌曲不会重叠,当我单击一个图标时,它会更改为另一个图标,但如果我单击另一个项目,则单击的前一个不会变回并保持不变相同的。

尝试

因此,我尝试迭代 RecycleView 的所有元素,将它们更改回默认图标,然后仅更改单击的图标,但我无法真正访问我没有访问的其他项目点击。我尝试使用全局变量引用前一项,但是,我再次无法真正处理前一项。

AudioGridActivity 代码:

public class AudioGridActivity1 extends AppCompatActivity {
private RecyclerView recyclerView2;
private MyAdapter2 adapter2;
private RecyclerView.LayoutManager layoutManager;

private ArrayList<CardAudioGrid> mList;

private MediaPlayer song;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_grid1);
createList();
buildRecyclerView();

final ImageView mediaPlayer = (ImageView) findViewById(R.id.mediaPlayerId); // this is the play/stop "button"

adapter2.setOnItemClickListener(new MyAdapter2.OnItemClickListener() {
@Override
public void onPlayClick(int position) {
switch(position){
case 0:
pauseTheme();
song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
playTheme();
break;
case 1:
pauseTheme();
song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
playTheme();
break;
case 2:
pauseTheme();
song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
playTheme();
break;
case 3:
pauseTheme();
song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
playTheme();
break;
case 4:
pauseTheme();
song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
playTheme();
break;
case 5:
pauseTheme();
song = MediaPlayer.create(AudioGridActivity1.this, R.raw.gato);
playTheme();
break;


}

}
});

}

public void createList(){
mList = new ArrayList<>();
mList.add(new CardAudioGrid(R.drawable.mediaplay2, R.color.colorAccent, ""));
...
mList.add(new CardAudioGrid(R.drawable.mediaplay2, R.color.colorPrimary, ""));
}

public void buildRecyclerView() {
recyclerView2 = findViewById(R.id.recyclerViewId2);
recyclerView2.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this, 2);
recyclerView2.setLayoutManager(layoutManager);
adapter2 = new MyAdapter2(this, mList);
recyclerView2.setAdapter(adapter2);
}
public void playTheme(){
if(song!=null) song.start();
}
public void pauseTheme(){
if(song!=null && song.isPlaying()) song.release();
}
...}

适配器代码:

public class MyAdapter2 extends RecyclerView.Adapter<MyAdapter2.MyViewHolder>{
private Context mContext;
private List<CardAudioGrid> mList;
private OnItemClickListener mListener;

public interface OnItemClickListener{
void onPlayClick (int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}

public MyAdapter2(Context mContext, List<CardAudioGrid> mList) {
this.mContext = mContext;
this.mList = mList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.audiocard, viewGroup, false);
return new MyViewHolder(view, mListener);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.audioCardBackground.setImageResource(mList.get(position).getCardBackground());
...
holder.mediaPlayer.setImageResource(mList.get(position).getPlayButton());
}
@Override
public int getItemCount() {
return mList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView mediaPlayer, audioCardBackground;

public MyViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);

mediaPlayer = (ImageView) itemView.findViewById(R.id.mediaPlayerId);
audioCardBackground = (ImageView) itemView.findViewById(R.id.cardbackgroundId);
mediaPlayer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener!=null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION){
listener.onPlayClick(position);
mediaPlayer.setImageResource(R.drawable.mediapause2);
}else{

}
}
}
});
}

}
}

我该如何解决这个问题?

我目前正在学习如何正确管理 Android,因此如果您能解释一下这些步骤的解决方案,我将不胜感激。

最佳答案

在您的 CardAudioGrid 模型中初始化一个 boolean 变量 isPlaying = false

在 RecyclerViewAdapter 中 MyAdapter2声明一个整数变量来保存当前播放项目的位置。

int nowPlaying = -1;

单击某个项目时,根据需要更改该项目的 isPlayingtrue/false

检查 nowPlaying 的值,如果它大于 -1 则使最后一个模型的 isPlaying = false

并分配nowPlaying新的点击位置值。

并调用notifyDataSetChanged();

onBindViewHolder不要忘记根据您的dataModel设置您的资源

if(mList.get(position).isPlaying())
holder.mediaPlayer.setImageResource(mList.get(position).getPlayButton());
else
holder.mediaPlayer.setImageResource(mList.get(position).getStopButton());

您也可以在不向 CardAudioGrid 模型类中添加新成员的情况下实现此目的。在这种情况下,不要检查 isPlaying 检查

if(position == nowPlaying)
holder.mediaPlayer.setImageResource(mList.get(position).getPlayButton());
else
holder.mediaPlayer.setImageResource(mList.get(position).getStopButton());

关于java - 如何更改不是我在 RecycleView 中单击的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56195382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com