[英]don't work onClick of view that is in recyclerView
我有一個recyclerView聊天! 我想錄制聲音並在聊天中播放...為此,我有一個播放和暫停聲音的布局...我需要在recyclerview中放置聲音才能播放它。
我嘗試了兩種方法:1- msgRecyclerView.addOnItemTouchListener,但是用這種方式我無法通過一個按鈕控制播放和暫停聲音2-這樣播放按鈕的onClick和暫停按鈕的onClick我在recyclerView中沒有聲音位置
pos=msgRecyclerView.getChildLayoutPosition(view);
停止申請
msgRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getApplicationContext(), msgRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
List<Messages> messages = new DB_Helper(getApplicationContext()).getListOfMessages("MsgPos = " + position + " and DriverID = " + 2 + " and OperatorID = " + id);
String locPath = messages.get(0).getLocalPath();
int msgType = messages.get(0).getMsgType();
if (msgType == 1) {
Intent intent = new Intent(getApplicationContext(), ImageActivity.class);
intent.putExtra("picture", messages.get(0).getImage());
startActivity(intent);
} else if (msgType == 2) {
//video
//String VideoUri = String.valueOf(d.getDuration());
Intent intent = new Intent(getApplicationContext(), VideoActivity.class);
intent.putExtra("Video", locPath);
startActivity(intent);
} else if (msgType == 3) {
pos=position;
}
@Override
public void onLongItemClick(View view, int position) {
// do whatever
}
})
);
public void playVoice(View view)
{
if(mediaPlayer!=null)
mediaPlayer.stop();
List<Messages> messages = new DB_Helper(getApplicationContext()).getListOfMessages("MsgPos = " + pos + " and DriverID = " + 2 + " and OperatorID = " + id);
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(messages.get(0).getLocalPath()));
mediaPlayer.start();
}
public void pauseVoice(View view)
{
// pos=msgRecyclerView.getChildLayoutPosition(view);
List<Messages> messages = new DB_Helper(getApplicationContext()).getListOfMessages("MsgPos = " + pos + " and DriverID = " + 2 + " and OperatorID = " + id);
mediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(messages.get(0).getLocalPath()));
mediaPlayer.stop();
}
在這種模式下,單擊功能不起作用。
請幫助我播放和停止聊天中的聲音
您可以在適配器中嘗試onclick方法。 像這樣;
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.myView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//your code
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.