如果遇到if语句,如何更改布局视图(ContentView)?

时间:2018-06-16 16:05:25

标签: android android-layout firebase-realtime-database android-adapter

我有一个移动应用程序,允许人们通过文本或录音进行通信。该应用程序正在按我的意愿工作,但当一个人发送录音时问题就出现了。当发送语音录音我是捡回来的URL代码,但我现在不能玩我想要做的是改变我查看从message_single_layout到audio_message_layout时message_type是“记录”。这是我的代码。我不知道我做错了什么。在此先感谢。

 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;


    }


}




private void copyText() {
}

@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);
    }


}



}

0 个答案:

没有答案