如何插入RecyclerView 2视图(一个用于发送消息,一个用于接收消息)

时间:2018-05-08 11:48:01

标签: java android android-recyclerview

我正在尝试在我的大学项目中为Android创建一个ChatBot,我正在尝试在聊天中发送一条消息(RecyclerView)并且它显示给我(好的部分)但我不喜欢我不知道如何向RecyclerView添加另一个代表ChatBot模块响应的视图(当我创建它时)。

我的适应课:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
    private static final String TAG = "RecyclerViewAdapter";
    private ArrayList<String> LIST_MESSAGES = new ArrayList<>();

    private String text_message_name = new String();

    private Context nContext;
    private String typeHumanOrBot;

    public RecyclerViewAdapter(ArrayList<String> MESSAGE_BODY, String USER_NAME, Context nContext,String typeHumanOrBot) {
        Log.d("Activitati","<<<<< IN RecyclerViewAdapter() >>>>");
        this.LIST_MESSAGES = MESSAGE_BODY;
        this.text_message_name = USER_NAME;
        this.nContext = nContext;
        this.typeHumanOrBot= typeHumanOrBot;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if(typeHumanOrBot.equals("HUMAN")){
             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_send,parent, false);
        }else{
             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_receive,parent, false);
        }

        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d(TAG,"onBindViewHolder: CALLED");
        holder.text_message_body.setText(LIST_MESSAGES.get(position));
        holder.text_message_name.setText(text_message_name);
    }

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

    /**
     * Tine widget-urile in memorie pentru fiecare widget ...
     */
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView image_message_profile;
        TextView text_message_name;
        TypeWriter text_message_body;
        public ViewHolder(View item){
            super(item);
            image_message_profile = item.findViewById(R.id.image_message_profile);
            text_message_name = item.findViewById(R.id.text_message_name);
            text_message_body = item.findViewById(R.id.text_message_body);
        }
    }

我称之为:

private String HUMAN = "Human";
private String BOT = "Ben";
private ArrayList<String> questions = new ArrayList<>();
private ArrayList<String> responses = new ArrayList<>();

final RecyclerView recyclerView = findViewById(R.id.list_of_messages);

a_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String questionSTr =questionRAW.getText().toString();
                questionRAW.setText("");
                questions.add(questionSTr);
                RecyclerViewAdapter adapter = new RecyclerViewAdapter(questions,HUMAN,getApplicationContext(),"HUMAN");
                recyclerView.setAdapter(adapter);
                responses.add("Hmm... neah");
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

            }

        });

我只能按照自己的意愿一次显示一种View not 2,我仍然是RecyclerView的新手,我必须做些什么才能一次插入2个视图?

0 个答案:

没有答案