如何在满足if语句条件时更改lContentView?

时间:2018-06-16 17:50:19

标签: android firebase firebase-realtime-database

我有一个移动应用程序,允许人们通过文本或录音进行通信。该应用程序正在按我的意愿工作,但当一个人发送录音时问题就出现了。发送录音时我会检索它的网址代码,但我现在无法播放我想要做的是当message_type为"录制&#34时,将我的视图从message_single_layout更改为audio_message_layout ;。这是我的代码。

  public class MessageAdapter extends 
RecyclerView.Adapter<MessageAdapter.MyMessageViewHolder>{

private ArrayList<Messages> arrayListMessages= new ArrayList<>();
private Context mcontext;
FirebaseAuth mAuth;
private DatabaseReference mUserDatabase;
private ViewGroup parent;


public ArrayAdapter adapter;

public MessageAdapter(Context context, ArrayList<Messages>   arrayListMessages) {
 this.arrayListMessages = arrayListMessages;
mcontext = context;
 }
    @Override

public MyMessageViewHolder onCreateViewHolder(ViewGroup parent, int  viewType ) {



View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent,false);


return new MyMessageViewHolder(view);

}

@Override
  public void onBindViewHolder(final MyMessageViewHolder holder, final int position) {

mAuth = FirebaseAuth.getInstance();
String mCurrentUser = mAuth.getCurrentUser().getUid();

final Messages messages = arrayListMessages.get(position);
String message_type = messages.getType();
String from_user = messages.getFrom();


mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);

if(mCurrentUser.equals(messages.getFrom())){
    holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bg_primary);
    holder.textViewMessage.setTextIsSelectable(true);
    holder.textViewMessage.setGravity(Gravity.RIGHT);
    holder.textViewMessage.setTextColor(Color.WHITE);

    holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view)
        {
            CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
            final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
            builder.setTitle("Select Options");
            builder.setItems(options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    //Click Event for each item.
                    if (i == 0) {

                        String selectedItem = messages.getMessage();
                        arrayListMessages.remove(selectedItem);


                        Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();


                    }


                    if (i == 1)
                    {

                        ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
                        cm.setText(holder.textViewMessage.getText());
                        Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();



                    }

                }
            });


            builder.show();
            return true;
        }
    });
}else{
    holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bd_white);
    holder.textViewMessage.setTextIsSelectable(true);
    holder.textViewMessage.setGravity(Gravity.LEFT);
    holder.textViewMessage.setTextColor(Color.BLACK);

    holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {


            CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
            final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
            builder.setTitle("Select Options");
            builder.setItems(options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    //Click Event for each item.
                    if (i == 0) {

                        String selectedItem = messages.getMessage();
                        arrayListMessages.remove(selectedItem);
                        adapter.notifyDataSetChanged();

                        Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();


                    }


                    if (i == 1)
                    {

                        ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
                        cm.setText(holder.textViewMessage.getText());
                        Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();



                    }

                }
            });


            builder.show();
            return true;

        }

    });
}
holder.textViewMessage.setText(messages.getMessage());

if(message_type.equals("text")) {

    holder.textViewMessage.setText(messages.getMessage());

}
else if(message_type.equals("recording"))
{

//This is where i want to change my inflate from message_single_layout to audio_message_layout

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_message_layout, parent,false);


    return view;


}

}

  @Override
    public int getItemCount() {
   return arrayListMessages.size();
  }

   class MyMessageViewHolder extends RecyclerView.ViewHolder{

   TextView textViewMessage;
CircleImageView UserProfile;


public MyMessageViewHolder(View itemView) {
    super(itemView);

    textViewMessage = itemView.findViewById(R.id.messageTextView);
    UserProfile = itemView.findViewById(R.id.message_profile_layout);
}
}

}

我想在message_type不是文本时更改我的适配器内容视图。我不知道如何继承audio_message_layout中的函数。提前谢谢。

0 个答案:

没有答案