此聊天应用程序有6种消息类型(文本-我的,文本-我的朋友/ Img-我,Img-我的朋友/音频-我,音频-我的朋友)此方法使我困惑,无法在应用中实现消息删除功能。我尝试了onItemLongClick并滑动,但无法成功。以及如何删除Firebase消息ID?
如何在此列表消息适配器上实现删除按钮或滑动功能?
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bphvcg.apho.Fragments.ImageMessageFragment;
import com.bphvcg.apho.Models.Message;
import com.bphvcg.apho.R;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.storage.FileDownloadTask;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import java.io.File;
import java.io.IOException;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class RecyclerListMessageAdapter extends RecyclerView.Adapter<RecyclerListMessageAdapter.ViewHolder> {
private List<Message> listMessages;
private int rMessageMine, rMessageFriend, rImageMessageMine, rImageMessageFriend, rAudioMessageMine, rAudioMessageFriend;
Context context;
private String mFileName = "";
private MediaPlayer mPlayer=null;
private String currentAudio = "";
public static ImageButton btnCurrentPlay = null;
public static MediaPlayer getCurrentMedia = null;
private StorageReference storageReference = FirebaseStorage.getInstance().getReference();
boolean isFinish = false; // kiểm tra audio đã hết chưa
public RecyclerListMessageAdapter(List<Message> listMessages, Context context, int rMessageMine, int rMessageFriend,
int rImageMessageMine, int rImageMessageFriend, int rAudioMessageMine, int rAudioMesageFriend) {
this.listMessages = listMessages;
this.rMessageMine = rMessageMine; // layout tin nhắn của bản thân
this.rMessageFriend = rMessageFriend; // layout tin nhắn của bạn
this.rImageMessageMine = rImageMessageMine; // layout tin nhắn ảnh của bản thân
this.rImageMessageFriend = rImageMessageFriend; // layout tin nhắn ảnh của bạn
this.rAudioMessageMine = rAudioMessageMine;
this.rAudioMessageFriend = rAudioMesageFriend;
this.context = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textViewMessageMine, textViewMessageTimeMine, textViewMessageFriend, textViewMessageTimeFriend;
TextView imageViewMessageMineTime, imageViewMessageFriendTime;
ImageView imageViewMessageMine, imageViewMessageFriend;
CircleImageView avatarSeen, avatarSeenImage, avatarSeenAudio;
ImageButton btnPlayAudioMessageMine, btnPlayAudioMessageFriend;
TextView textViewMessageTimeAudioMine, textViewMessageTimeAudioFriend;
public ViewHolder(View itemView) {
super(itemView);
textViewMessageMine = itemView.findViewById(R.id.textViewMessageMine);
textViewMessageTimeMine = itemView.findViewById(R.id.textViewMessageTimeMine);
textViewMessageFriend = itemView.findViewById(R.id.textViewMessageFriend);
textViewMessageTimeFriend = itemView.findViewById(R.id.textViewMessageTimeFriend);
imageViewMessageMine = itemView.findViewById(R.id.imageViewMessageMine);
imageViewMessageFriend = itemView.findViewById(R.id.imageViewMessageFriend);
imageViewMessageMineTime = itemView.findViewById(R.id.imageViewMessageTimeMine);
imageViewMessageFriendTime = itemView.findViewById(R.id.imageViewMessageTimeFriend);
btnPlayAudioMessageMine = itemView.findViewById(R.id.btnPlayAudioMessageMine);
btnPlayAudioMessageFriend = itemView.findViewById(R.id.btnPlayAudioMessageFriend);
textViewMessageTimeAudioMine = itemView.findViewById(R.id.textViewAudioMessageTimeMine);
textViewMessageTimeAudioFriend = itemView.findViewById(R.id.textViewAudioMessageTimeFriend);
avatarSeen = itemView.findViewById(R.id.avatarSeen);
avatarSeenImage = itemView.findViewById(R.id.avatarSeenImage);
avatarSeenAudio = itemView.findViewById(R.id.avatarSeenAudio);
}
}
@Override
public int getItemViewType(int position) {
if (listMessages.isEmpty())
return 0;
else if (listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
&& !listMessages.get(position).isImage() && !listMessages.get(position).isAudio()) // The message itself is text
return 1;
else if (!listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
&& !listMessages.get(position).isImage() && !listMessages.get(position).isAudio()) // Your message is text
return 2;
else if (listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
&& listMessages.get(position).isImage()) // Your own message is a picture
return 3;
else if (!listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
&& listMessages.get(position).isImage()) //Your message is photo
return 4;
else if (listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
&& listMessages.get(position).isAudio()) { // Your message is audio
return 5;
} else if (!listMessages.get(position).getUidSender().equals(FirebaseAuth.getInstance().getUid())
&& listMessages.get(position).isAudio()) { // Your message is audio news
return 6;
}
return 0;
}
@Override
public RecyclerListMessageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// Create your own text message view
View viewMessageMine = LayoutInflater.from(parent.getContext()).inflate(rMessageMine, parent, false);
// Create your text message view
View viewMessageFriend = LayoutInflater.from(parent.getContext()).inflate(rMessageFriend, parent, false);
// Create your own picture message view
View viewImageMessageMine = LayoutInflater.from(parent.getContext()).inflate(rImageMessageMine, parent, false);
// Create your picture message view
View viewImageMessageFriend = LayoutInflater.from(parent.getContext()).inflate(rImageMessageFriend, parent, false);
View viewAudioMessageMine = LayoutInflater.from(parent.getContext()).inflate(rAudioMessageMine, parent, false);
View viewAudioMessageFriend = LayoutInflater.from(parent.getContext()).inflate(rAudioMessageFriend, parent, false);
ViewHolder viewHolder = null;
switch (viewType) {
case 0:
viewHolder = null;
break;
case 1:
viewHolder = new ViewHolder(viewMessageMine);
break;
case 2:
viewHolder = new ViewHolder(viewMessageFriend);
break;
case 3:
viewHolder = new ViewHolder(viewImageMessageMine);
break;
case 4:
viewHolder = new ViewHolder(viewImageMessageFriend);
break;
case 5:
viewHolder = new ViewHolder(viewAudioMessageMine);
break;
case 6:
viewHolder = new ViewHolder(viewAudioMessageFriend);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerListMessageAdapter.ViewHolder holder, int position) {
if (listMessages.isEmpty()) {
//holder.textViewMessageNow.setVisibility(View.VISIBLE);
} else {
final Message msg = listMessages.get(position);
if (msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && !msg.isImage() && !msg.isAudio()) {
holder.textViewMessageMine.setText(msg.getContent());
holder.textViewMessageTimeMine.setText(msg.getTimeMessage().substring(11, 16));
} else if (!msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && !msg.isImage() && !msg.isAudio()) {
holder.textViewMessageFriend.setText(msg.getContent());
holder.textViewMessageTimeFriend.setText(msg.getTimeMessage().substring(11, 16));
} else if (msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isImage()) {
getImageStorage(holder.imageViewMessageMine, msg.getUidSender(), msg.getUidReceiver(), msg.getContent());
holder.imageViewMessageMineTime.setText(msg.getTimeMessage().substring(11, 16));
final String nameImage = msg.getContent();
holder.imageViewMessageMine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = ((Activity) context).getFragmentManager();
ImageMessageFragment imageMessageFragment = ImageMessageFragment.newInstance(nameImage);
imageMessageFragment.show(fm, null);
}
});
} else if (!msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isImage()) {
getImageStorage(holder.imageViewMessageFriend, msg.getUidSender(), msg.getUidReceiver(), msg.getContent());
holder.imageViewMessageFriendTime.setText(msg.getTimeMessage().substring(11, 16));
final String nameImage = msg.getContent();
holder.imageViewMessageFriend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = ((Activity) context).getFragmentManager();
ImageMessageFragment imageMessageFragment = ImageMessageFragment.newInstance(nameImage);
imageMessageFragment.show(fm, null);
}
});
} else if (msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isAudio()) { // tin am thanh cua ban than
holder.textViewMessageTimeAudioMine.setText(msg.getTimeMessage().substring(11, 16));
holder.btnPlayAudioMessageMine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPlayPressed(holder.btnPlayAudioMessageMine,msg.getUidSender(),
msg.getUidReceiver(),msg.getContent());
}
});
} else if (!msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && msg.isAudio()) { // tin am thanh cua ban
holder.textViewMessageTimeAudioFriend.setText(msg.getTimeMessage().substring(11, 16));
holder.btnPlayAudioMessageFriend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPlayPressed(holder.btnPlayAudioMessageFriend,msg.getUidSender(),
msg.getUidReceiver(),msg.getContent());
}
});
}
//If the message itself and the message location last
if(msg.getUidSender().equals(FirebaseAuth.getInstance().getUid()) && (position == (listMessages.size() - 1))){
if(msg.isLastMessageSeen() && !msg.isAudio() && !msg.isAudio()){
getAvatarSeen(holder.avatarSeen,listMessages.get(listMessages.size() - 1).getUidReceiver());
holder.avatarSeen.setVisibility(View.VISIBLE);
}
else if(msg.isLastMessageSeen() && msg.isImage()){
getAvatarSeen(holder.avatarSeenImage,listMessages.get(listMessages.size() - 1).getUidReceiver());
holder.avatarSeenImage.setVisibility(View.VISIBLE);
}
else if(msg.isLastMessageSeen() && msg.isAudio()){
getAvatarSeen(holder.avatarSeenAudio,listMessages.get(listMessages.size() - 1).getUidReceiver());
holder.avatarSeenAudio.setVisibility(View.VISIBLE);
}
}
}
}
答案 0 :(得分:0)
您可以使用
storageReference.child(key).remove();
您尝试过吗?