• 1000 1002 *, потому что, на мой взгляд, невозможно, чтобы функция MediaPlayer.create была помещена в метод onClick, мой вопрос:
где должна быть размещена функция MediaPlayer.create? Наконец, я помещаю ее в onBindView метод, но приложение принудительно закрывается.
это последний код, который я изменил в методе onBindView
@Override
public void onBindViewHolder(@NonNull IqraAdapter.ViewHolder holder, int position) {
IqraItem iqraItem = mData.get(position);
holder.txHuruf.setText(iqraItem.getHuruf());
holder.txHurufEx.setText(iqraItem.getExHuruf());
holder.txSifat.setText(iqraItem.getSifat());
boolean isEkspans = mData.get(position).isExpanded();
mediaPlayer = MediaPlayer.create(context, iqraItem.getAudio());
holder.expandable.setVisibility(isEkspans ? View.VISIBLE : View.GONE);
holder.btnplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
holder.btnplay.setImageResource(R.drawable.ic_play_circle_filled_black_24dp);
} else {
mediaPlayer.start();
holder.btnplay.setImageResource(R.drawable.ic_pause_circle_filled_black_24dp);
}
}
});
}
, и это общий код в классе адаптера:
public class IqraAdapter extends RecyclerView.Adapter<IqraAdapter.ViewHolder> {
private static final String TAG = "IqraAdapter";
private List<IqraItem> mData;
private Context context;
MediaPlayer mediaPlayer;
public IqraAdapter(List<IqraItem> mData, Context context) {
this.mData = mData;
this.context = context;
}
@NonNull
@Override
public IqraAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_iqra_hijaiyah_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull IqraAdapter.ViewHolder holder, int position) {
IqraItem iqraItem = mData.get(position);
holder.txHuruf.setText(iqraItem.getHuruf());
holder.txHurufEx.setText(iqraItem.getExHuruf());
holder.txSifat.setText(iqraItem.getSifat());
boolean isEkspans = mData.get(position).isExpanded();
mediaPlayer = MediaPlayer.create(context, iqraItem.getAudio());
holder.expandable.setVisibility(isEkspans ? View.VISIBLE : View.GONE);
holder.btnplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
holder.btnplay.setImageResource(R.drawable.ic_play_circle_filled_black_24dp);
} else {
mediaPlayer.start();
holder.btnplay.setImageResource(R.drawable.ic_pause_circle_filled_black_24dp);
}
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private static final String TAG = "ViewHolder";
ConstraintLayout expandable;
TextView txHuruf, txHurufEx, txSifat;
ImageButton btnplay;
Typeface typehj = Typeface.createFromAsset(itemView.getContext().getAssets(),"font/Poppins-Medium.otf");
public ViewHolder(@NonNull View itemView) {
super(itemView);
txHuruf = (TextView) itemView.findViewById(R.id.hurufhijaiyah);
txHuruf.setTypeface(typehj);
txHurufEx = (TextView) itemView.findViewById(R.id.txHuruf);
txSifat = (TextView) itemView.findViewById(R.id.txsifat);
btnplay = (ImageButton) itemView.findViewById(R.id.btnPlay);
expandable = itemView.findViewById(R.id.expandHijaiyah);
txHuruf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
IqraItem items = mData.get(getAdapterPosition());
items.setExpanded(!items.isExpanded());
notifyItemChanged(getAdapterPosition());
}
});
}
}