大家好,希望你们都好。 我正在开发一款可以播放不同动物声音的Android应用程序。
我正在使用回收视图,如下面的屏幕截图所示。现在我遇到了一个问题,我的行内有一个播放按钮,每当我从我的项目0播放声音时播放动物的声音,或者任何位置显示我在视图中暂停选项。但是,每当我点击下一个项目1或任何其他项目时,它都不会更改先前项目0的图像。
我是通过使用以下代码刷新我的适配器列表来实现的。 但我认为这不是一个好习惯。!
t1.recyclerView.setAdapter(null);
t1.customAdapter=new CustomAdapter(t1.animalList,ctx);
t1.recyclerView.setAdapter(t1.customAdapter);
我尝试使用notifyItemChanged(oldPosition)
之类的所有东西,但仍然没有得到我想要的结果。!
请建议我一个合适的方法来解决这个问题?我是android的新手,我有一个想法是为了查看最后一项并保存它然后我得到那个视图并在我之前的列表按钮上设置默认图像播放但我不知道如何这样做?
这是我的自定义适配器代码。!
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private List<Animal> AnimalList;
public Context ctx;
public static int postionchange=-1;
public static Tab1Birds t1;
final int[] countLike = {0};
//public static MediaPlayer mediaPlayer2 = new MediaPlayer();
int pos=0;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView display;
public TextView nameTextView;
public ImageButton play,options,menubtn;
public MyViewHolder(View view) {
super(view);
nameTextView = (TextView) view.findViewById(R.id.nameTextView);
play=(ImageButton)view.findViewById(R.id.playbtn);
options=(ImageButton)view.findViewById(R.id.menubtn);
display=(ImageView)view.findViewById(R.id.imageview);
menubtn = (ImageButton) view.findViewById(R.id.menubtn);
//MediaPlayerClass.mediaPlayer= new MediaPlayer();
}
}
public CustomAdapter(List<Animal> moviesList,Context ct) {
this.AnimalList = moviesList;
this.ctx=ct;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Animal animal = AnimalList.get(position);
final int[] previous = {position};
holder.nameTextView.setText(animal.getName());
holder.display.setImageResource(animal.getImageResource());
//////////////////////////////////////////////////////////////////////////////////////////////////////////
holder.options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (MediaPlayerClass.mediaPlayer.isPlaying()) {
holder.play.setBackgroundResource(R.drawable.play_btn);
MediaPlayerClass.mediaPlayer.stop();
}
} catch (Exception ex) {
}
Dialog dialog;
final String[] items = {"SET AS RING TUNE", "SET AS MESSAGE TUNE", "SET AS ALARM TUNE"};
final ArrayList itemsSelected = new ArrayList();
final AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("OPTIONS");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
itemsSelected.add(selectedItemId);
} else if (itemsSelected.contains(selectedItemId)) {
itemsSelected.remove(Integer.valueOf(selectedItemId));
}
}
})
.setPositiveButton("Done!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Toast toast = Toast.makeText(ctx, "successfully selected", Toast.LENGTH_SHORT);
toast.show();
SparseBooleanArray CheCked = ((AlertDialog) dialog).getListView().getCheckedItemPositions();
String str = "";
if (CheCked.get(0)) {
savering(animal.getSoundResource());
}
if (CheCked.get(1)) {
savemsg(animal.getSoundResource());
}
if (CheCked.get(2)) {
savealarm(animal.getSoundResource());
}
// if (CheCked.get(3)) {
// DatabaseHelper.save_bookmarks(itemsList.get(position).getitemName(), R.drawable.play_btn, itemsList.get(position).getItemTuneSt());
// Toast.makeText(ctx, "Item added to favourites", Toast.LENGTH_SHORT).show();
// }
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
dialog.show();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
});
pos=position;
// for animation
// ///////////////////////////
//animate(holder);//Function for animation
//////////////////////////////
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(postionchange==-1)
{
postionchange=position;
}
if(postionchange!=position)
{
holder.play.setBackgroundResource(R.drawable.play_btn);
}
holder.play.setBackgroundResource(R.drawable.pause_btn);// present state
MediaPlayerClass.play(animal.getSoundResource(),ctx);
if(countLike[0] ==0)
{
countLike[0] =1;
}else if(countLike[0] ==1)
{
//if is playing then Pause
if(MediaPlayerClass.mediaPlayer.isPlaying())
MediaPlayerClass.mediaPlayer.stop();
t1.recyclerView.setAdapter(null);
t1.customAdapter=new CustomAdapter(t1.animalList,ctx);
t1.recyclerView.setAdapter(t1.customAdapter);
holder.play.setBackgroundResource(R.drawable.play_btn); //sound stop show play button
countLike[0] =2;
}
if(countLike[0] >1)
{
countLike[0] =0;
holder.play.setBackgroundResource(R.drawable.play_btn); // click to play music, click again to stop music
}
//setOnCompletionListener
MediaPlayerClass.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
holder.play.setBackgroundResource(R.drawable.play_btn);
}
});
// MediaPlayerClass.mediaPlayer.start();
}
});
}
@Override
public int getItemCount() {
return AnimalList.size();
}
//animation
public void animate(RecyclerView.ViewHolder viewHolder) {
final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(ctx, R.anim.bounce_interpolator);
viewHolder.itemView.setAnimation(animAnticipateOvershoot);
}
//////////////////////////////////////////////////////////////////////////////////////////
}
以下是三星手机中的音乐播放器的示例。 当我播放音乐时,它会显示动画,当我播放下一首歌时,它会播放该歌曲并停止之前播放的音乐及其动画。 我知道如何停止媒体播放器,但如何更改停止动画或音乐库等?
答案 0 :(得分:0)
Bellow样本适用于使用RecyclerView的普通列表数据。
之后,当列表刷新时,使用您的实际绑定位置检查您保存的位置,然后根据需要应用更改。
public void onBindViewHolder(final MyViewHolder holder,final int position){
holder.tvGroupName.setText(list.get(position).getGroupName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Save position in item click then refresh list
pos = position;
notifyDataSetChanged();
}
});
// Once all items started refreshing check your save position with actual position then change text
if (pos == position) {
holder.tvGroupName.setText("Testing");
}
}
希望这个样本有用..
和
我也修改了你的班级,检查这个并希望它能起作用..
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private List<Animal> AnimalList;
public Context ctx;
public static int postionchange=-1;
public static Tab1Birds t1;
final int[] countLike = {0};
//public static MediaPlayer mediaPlayer2 = new MediaPlayer();
int pos;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView display;
public TextView nameTextView;
public ImageButton play,options,menubtn;
public MyViewHolder(View view) {
super(view);
nameTextView = (TextView) view.findViewById(R.id.nameTextView);
play=(ImageButton)view.findViewById(R.id.playbtn);
options=(ImageButton)view.findViewById(R.id.menubtn);
display=(ImageView)view.findViewById(R.id.imageview);
menubtn = (ImageButton) view.findViewById(R.id.menubtn);
//MediaPlayerClass.mediaPlayer= new MediaPlayer();
}
}
public CustomAdapter(List<Animal> moviesList,Context ct) {
this.AnimalList = moviesList;
this.ctx=ct;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Animal animal = AnimalList.get(position);
final int[] previous = {position};
holder.nameTextView.setText(animal.getName());
holder.display.setImageResource(animal.getImageResource());
//////////////////////////////////////////////////////////////////////////////////////////////////////////
holder.options.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (MediaPlayerClass.mediaPlayer.isPlaying()) {
holder.play.setBackgroundResource(R.drawable.play_btn);
MediaPlayerClass.mediaPlayer.stop();
}
} catch (Exception ex) {
}
Dialog dialog;
final String[] items = {"SET AS RING TUNE", "SET AS MESSAGE TUNE", "SET AS ALARM TUNE"};
final ArrayList itemsSelected = new ArrayList();
final AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("OPTIONS");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
itemsSelected.add(selectedItemId);
} else if (itemsSelected.contains(selectedItemId)) {
itemsSelected.remove(Integer.valueOf(selectedItemId));
}
}
})
.setPositiveButton("Done!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Toast toast = Toast.makeText(ctx, "successfully selected", Toast.LENGTH_SHORT);
toast.show();
SparseBooleanArray CheCked = ((AlertDialog) dialog).getListView().getCheckedItemPositions();
String str = "";
if (CheCked.get(0)) {
savering(animal.getSoundResource());
}
if (CheCked.get(1)) {
savemsg(animal.getSoundResource());
}
if (CheCked.get(2)) {
savealarm(animal.getSoundResource());
}
// if (CheCked.get(3)) {
// DatabaseHelper.save_bookmarks(itemsList.get(position).getitemName(), R.drawable.play_btn, itemsList.get(position).getItemTuneSt());
// Toast.makeText(ctx, "Item added to favourites", Toast.LENGTH_SHORT).show();
// }
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog = builder.create();
dialog.show();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
});
// for animation
// ///////////////////////////
//animate(holder);//Function for animation
//////////////////////////////
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pos=position;
notifyDataSetChanged();
}
});
if (pos == position){
holder.play.setBackgroundResource(R.drawable.pause_btn);// present state
MediaPlayerClass.play(animal.getSoundResource(),ctx);
if(countLike[0] ==0)
{
countLike[0] =1;
}else if(countLike[0] ==1)
{
//if is playing then Pause
if(MediaPlayerClass.mediaPlayer.isPlaying())
MediaPlayerClass.mediaPlayer.stop();
t1.recyclerView.setAdapter(null);
t1.customAdapter=new CustomAdapter(t1.animalList,ctx);
t1.recyclerView.setAdapter(t1.customAdapter);
holder.play.setBackgroundResource(R.drawable.play_btn); //sound stop show play button
countLike[0] =2;
}
if(countLike[0] >1)
{
countLike[0] =0;
holder.play.setBackgroundResource(R.drawable.play_btn); // click to play music, click again to stop music
}
//setOnCompletionListener
MediaPlayerClass.mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
holder.play.setBackgroundResource(R.drawable.play_btn);
}
});
// MediaPlayerClass.mediaPlayer.start();
}else {
holder.play.setBackgroundResource(R.drawable.play_btn);
}
}
@Override
public int getItemCount() {
return AnimalList.size();
}
//animation
public void animate(RecyclerView.ViewHolder viewHolder) {
final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(ctx, R.anim.bounce_interpolator);
viewHolder.itemView.setAnimation(animAnticipateOvershoot);
}
}
@Rajesh
答案 1 :(得分:0)
这个问题类似于
你想设置游戏&amp;根据链接到该项目的声音文件是否正在播放,暂停图标到列表项目。
// Set a global variable to keep track of which list item is playing.
// if it is -1 then no sound is playing
private int lastPosition = 0;
现在您需要添加isPlaying
变量以保留音频模型
public class VideoModel {
private String audioURL;
private String imageURL;
private boolean isPlaying;
public VideoModel(String audioURL, boolean isSelected,String imageURL) {
this.videoURL = videoURL;
this.isPlaying = isSelected;
}
public boolean isPlaying() {
return isPlaying;
}
public void setPlaying(boolean playing) {
isPlaying = playing;
}
/**
* @return Audio URL
*/
public String getAudioURL() {
return audioURL;
}
/**
* @return Thumbnail of Animal
*/
public String getImageURL() {
return imageURL;
}
}
现在,当用户点击列表中的任何项目时,您需要更新上一个播放项目并将现在播放的项目分配给上次播放项目
videoGridAdapter.setOnCardClickListner(new VideoGridAdapter.OnCardClickListner() {
@Override
public void OnCardClicked(View view, int position) {
//Make Lats Tapped item playing false
listOfAudio.get(lastPosition).setPlaying(false);
//Make current Tapped item playing true
listOfAudio.get(position).setPlaying(true);
// update pointer current tapped audio
lastPosition = position;
//LET RCYCLERVIEW ADAPTER DO THE MAGIC
videoGridAdapter.notifyDataSetChanged();
}
});
您需要管理适配器中的隐藏显示逻辑
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
VideoViewHolder videoHolder = (VideoViewHolder) holder;
if (VideoGridActivity.listOfAudio.get(position).isPlaying()) {
//Playing Video :- Show Pause Button and hide play button
((VideoViewHolder) holder).getPauseButton().setVisibility(View.VISIBLE);
((VideoViewHolder) holder).getPlayButton().setVisibility(View.GONE);
// Play sound and do other stuff
} else {
//Not playing make Hide Pause Button and SHow Play button
((VideoViewHolder) holder).getPauseButton().setVisibility(View.GONE);
((VideoViewHolder) holder).getPlayButton().setVisibility(View.VISIBLE);
}
}
这是一个伪代码,但你知道如何做到这一点。希望能帮助到你。
答案 2 :(得分:-1)
我不太了解你的流程。但我可以给你一点提示。
你有' AnimalList '
尝试使用 notifyItemChanged(int position,Object payload);
并添加someState变量(布尔值)以确定现在正在播放哪个项目。
如下。
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final Animal animal = AnimalList.get(position);
if(animal.isPlayStatus()){
playButton.setSelected(true);
}else{
playButton.setSelected(false);
}
}
public class Animal {
boolean playStatus;
.
.
.
public boolean isPlayStatus() {
return playStatus;
}
public void setPlayStatus(boolean playStatus) {
this.playStatus = playStatus;
}
}
Animal item = AnimalList.get(somePosition);
item.setPlayStatus(true);
notifyItemChanged(somePosition, item);
or
Animal previousItem = AnimalList.get(somePosition);
Animal item = AnimalList.get(somePosition + 1);
previousItem.setPlayStatus(false);
item.setPlayStatus(true);
mAdapter.notifyItemRangeChanged(somePosition, somePosition + 1);
or
int index = 0;
for (Animal item : AnimalList) {
if(somePosition == index ){
item.setPlayStatus(true);
}else{
item.setPlayStatus(false);
}
index++;
}
notifyItemRangeChanged(0, AnimalList.size()); // or
notifyDatasetChanged();