我使用动物图像构建应用程序,点击动物后,它会播放该动物的声音。
我使用Recyclerview显示所有动物物品。
我的问题是我不知道如何处理点击事件以播放每只动物的声音。我知道如何在recyclerview中为每个项目播放相同的声音,但我不知道如何处理每个项目的特定声音。
这是我处理数据的类的代码,其中我为每个对象放置了所有动物名称,图像和声音。
package com.example.android.animalsoundsforchildren;
import java.util.ArrayList;
public class Zivotinje {
private String mAnimal_name;
private int mAnimal_slika;
private int mSound;
public Zivotinje(String animalName, int slika, int sound) {
this.setmAnimal_name(animalName);
this.setmAnimal_slika(slika);
this.setmSound(sound);
}
public String getmAnimal_name() {
return mAnimal_name;
}
public void setmAnimal_name(String mAnimal_name) {
this.mAnimal_name = mAnimal_name;
}
public int getmAnimal_slika() {
return mAnimal_slika;
}
public void setmAnimal_slika(int mAnimal_slika) {
this.mAnimal_slika = mAnimal_slika;
}
public int getmSound() {
return mSound;
}
public void setmSound(int mSound) {
this.mSound = mSound;
}
//putting data
public static ArrayList<Zivotinje> createZivotinjeList() {
ArrayList<Zivotinje> animals = new ArrayList<Zivotinje>();
animals.add(new Zivotinje("Sheep", R.drawable.domestic_sheep, R.raw.gun));
animals.add(new Zivotinje("Sparrow", R.drawable.birds_sparrow, R.raw.birds_sparrow));
animals.add(new Zivotinje("Dolphin", R.drawable.sea_dolphin, R.raw.dolphin));
animals.add(new Zivotinje("Bumblebee", R.drawable.insects_bumblebee, R.raw.bumblebee));
animals.add(new Zivotinje("Tractor", R.drawable.cars_tractor, R.raw.tractor));
animals.add(new Zivotinje("Baby Cry", R.drawable.laugh_baby_cry, R.raw.baby_cray));
animals.add(new Zivotinje("Wind", R.drawable.nature_wind, R.raw.wind));
animals.add(new Zivotinje("Golf", R.drawable.effects_golf, R.raw.golf));
animals.add(new Zivotinje("Clock", R.drawable.others_clock, R.raw.clock));
return animals;
}
}
&#13;
这是适配器的代码,在这里我处理点击,当我点击项目时,它播放相同的声音,但如何为每个动物播放不同的声音,而不是像这里一样的声音。在这里我总是播放声音R.raw.clock
package com.example.android.animalsoundsforchildren;
import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {
@Override
public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {
Zivotinje zivotinjeIme = mZivotinje.get(position);
// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
textView.setText(zivotinjeIme.getmAnimal_name());
ImageView slika_source = viewHolder.imageView;
slika_source.setImageResource(zivotinjeIme.getmAnimal_slika());
}
@Override
public int getItemCount() {
return mZivotinje.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView nameTextView;
public ViewHolder(final View itemView) {
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
imageView = (ImageView) itemView.findViewById(R.id.slika_id);
//It works when i play for each item sound but how to play specific sound for each animal
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), R.raw.clock);
mediaPlayer.start();
}
});
}
}
private List<Zivotinje> mZivotinje;
public ZivotinjeAdapter(List<Zivotinje> animals) {
mZivotinje = animals;
}
}
&#13;
答案 0 :(得分:0)
你可以做的一件事是使用一个switch语句来检查点击了哪只动物,然后采取相应行动。
如果您拥有大量动物,请让所有动物从Animal类继承,该类将具有抽象的makeSound()方法。在子类中重写该方法以使它们发出自己的声音。在onClick()方法中,调用makeSound()方法。
答案 1 :(得分:0)
您可以通过调用getAdapterPosition()来访问ViewHolder中的位置
一旦你有位置,你就可以在那个位置得到物品(动物物体),比如把适配器中的动物列表保存起来。现在,您可以轻松访问要播放的对象中的声音。
答案 2 :(得分:0)
请参阅链接https://gist.github.com/grantland/cd70814fe4ac369e3e92
您可以参考项目点击事件以及如何获取点击事件
上的项目请参阅以下代码。我做了改变
public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {
@Override
public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {
Zivotinje zivotinjeIme = mZivotinje.get(position);
// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
textView.setText(zivotinjeIme.getmAnimal_name());
ImageView slika_source = viewHolder.imageView;
slika_source.setImageResource(zivotinjeIme.getmAnimal_slika());
viewHolder.setItem(mZivotinje[position]);
}
@Override
public int getItemCount() {
return mZivotinje.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView nameTextView;
private Zivotinje mItem;
public ViewHolder(final View itemView) {
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
imageView = (ImageView) itemView.findViewById(R.id.slika_id);
public void setItem(Zivotinje item) {
mItem = item;
}
//It works when i play for each item sound but how to play specific sound for each animal
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// here you will get mItem and from that you can get the sound
MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), R.raw.clock);
mediaPlayer.start();
}
});
}
}
private List<Zivotinje> mZivotinje;
public ZivotinjeAdapter(List<Zivotinje> animals) {
mZivotinje = animals;
}
}
答案 3 :(得分:0)
//您可以通过以下方式在recyclerview item的每个位置上实现音乐
// 在您的适配器中将意图传递给您正在播放音乐的下一个活动 绑定您的适配器类的视图
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str_pos = String.valueOf(position);
Intent intent = new Intent(v.getContext(), PlayerActivity.class);
intent.putExtra("myKey", str_pos);
v.getContext().startActivity(intent);
}
});
public class PlayerActivity extends AppCompatActivity {
ImageButton buttonPlay;
MediaPlayer mPlayer;
AudioManager audioManager;
boolean isPlaying = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
buttonPlay = findViewById(R.id.buttonPlay);
//getting the intent
Bundle extras = getIntent().getExtras();
String tmp = extras.getString("myKey");
int position = Integer.parseInt(tmp);
//applying the condition on position of each
if (position == 0)
{
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.r1);
mPlayer.start();
mPlayer.setLooping(true);
}
else if (position == 1){
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.r2);
mPlayer.start();
mPlayer.setLooping(true);
}
else if (position == 2){
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mm);
mPlayer.start();
mPlayer.setLooping(true);
}
else if (position == 3){
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.r2);
mPlayer.start();
mPlayer.setLooping(true);
}
else if (position == 4){
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mm);
mPlayer.start();
mPlayer.isLooping();
mPlayer.setLooping(true);
}
else if (position == 5){
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mm);
mPlayer.start();
mPlayer.setLooping(true);
}
buttonPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPlaying) {
mPlayer.pause();
buttonPlay.setImageResource(R.drawable.ic_play);
isPlaying = false;
}
else {
mPlayer.start();
isPlaying = true;
buttonPlay.setImageResource(R.drawable.ic_pause);
}
}
});
//perfoam on resume etc
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
stopPlayer();
finish();
}
private void stopPlayer() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
@Override
protected void onStop() {
super.onStop();
stopPlayer();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onPause() {
super.onPause();
if (mPlayer !=null){
mPlayer.pause();
}
}
@Override
protected void onResume() {
super.onResume();
if (mPlayer !=null){
mPlayer.start();
}
else {
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mm);
mPlayer.start();
mPlayer.isLooping();
buttonPlay.setImageResource(R.drawable.ic_pause);
}
}