Например, когда пользователь коснется где-нибудь, он отобразит Hey Sam, затем еще раз на Touch, он должен отобразить Hey John и далее, как это.
Ниже мой код для адаптера
package com.techjd.chatapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ChatAppMsgAdapter extends RecyclerView.Adapter<ChatAppMsgViewHolder> {
private List<ChatAppMsgDTO> msgDtoList = null;
public ChatAppMsgAdapter(List<ChatAppMsgDTO> msgDtoList) {
this.msgDtoList = msgDtoList;
}
@Override
public void onBindViewHolder(ChatAppMsgViewHolder holder, int position) {
ChatAppMsgDTO msgDto = this.msgDtoList.get(position);
if(msgDto.MSG_TYPE_RECEIVED.equals(msgDto.getMsgType()))
{
holder.leftMsgTextView.setText(msgDto.getMsgContent());
holder.rightMsgLayout.setVisibility(LinearLayout.GONE);
}
else if(msgDto.MSG_TYPE_SENT.equals(msgDto.getMsgType()))
{
holder.rightMsgTextView.setText(msgDto.getMsgContent());
holder.leftMsgLayout.setVisibility(LinearLayout.GONE);
}
}
@Override
public ChatAppMsgViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.chat_layout, parent, false);
return new ChatAppMsgViewHolder(view);
}
@Override
public int getItemCount() {
if(msgDtoList==null)
{
msgDtoList = new ArrayList<ChatAppMsgDTO>();
}
return msgDtoList.size();
}
}
Ниже мой код для RecyclerView
package com.techjd.chatapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView msgRecyclerView = (RecyclerView)findViewById(R.id.chat_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(linearLayoutManager);
final List<ChatAppMsgDTO> msgDtoList = new ArrayList<ChatAppMsgDTO>();
ChatAppMsgDTO msgDto = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "Hello Sam");
ChatAppMsgDTO msgDTO = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "Hello John");
ChatAppMsgDTO msgDto1 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "How are you doing today ?");
ChatAppMsgDTO msgDTO1 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "I am doing Great");
ChatAppMsgDTO msgDto2 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "Where do you live?");
ChatAppMsgDTO msgDTO2 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "I live in India");
ChatAppMsgDTO msgDto3 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "What's your Age ? ");
ChatAppMsgDTO msgDTO3 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "I am 17 years old");
ChatAppMsgDTO msgDto4 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "What's your birth Date?");
ChatAppMsgDTO msgDTO4 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "7th April 2002");
ChatAppMsgDTO msgDto5 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "You are pretty Young");
ChatAppMsgDTO msgDTO5 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "Yeah !");
ChatAppMsgDTO msgDto6 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "Will you be able to work");
ChatAppMsgDTO msgDTO6 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "Yes , i can assure you that .");
ChatAppMsgDTO msgDto7 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "Can you prove us ?");
ChatAppMsgDTO msgDTO7 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "Based on my skills , I can easily help you .");
ChatAppMsgDTO msgDto8 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_RECEIVED, "Do you trust yourself ?");
ChatAppMsgDTO msgDTO8 = new ChatAppMsgDTO(ChatAppMsgDTO.MSG_TYPE_SENT, "Yes , Sir");
msgDtoList.add(msgDto);
msgDtoList.add(msgDTO);
msgDtoList.add(msgDto1);
msgDtoList.add(msgDTO1);
msgDtoList.add(msgDto2);
msgDtoList.add(msgDTO2);
msgDtoList.add(msgDto3);
msgDtoList.add(msgDTO3);
msgDtoList.add(msgDto4);
msgDtoList.add(msgDTO4);
msgDtoList.add(msgDto5);
msgDtoList.add(msgDTO5);
msgDtoList.add(msgDto6);
msgDtoList.add(msgDTO6);
msgDtoList.add(msgDto7);
msgDtoList.add(msgDTO7);
msgDtoList.add(msgDto8);
msgDtoList.add(msgDTO8);
final ChatAppMsgAdapter chatAppMsgAdapter = new ChatAppMsgAdapter(msgDtoList);
msgRecyclerView.setAdapter(chatAppMsgAdapter);
}
}
Прикрепление изображения для справки: https://i.stack.imgur.com/5fzeU.png
Как видите, это отображается отлично. Но когда пользователь откроет действие, оно будет сначала пустым. Затем, как только пользователь щелкнет в любом месте действия, он начнет отображать эти тексты один за другим касанием пользователя.
Как мне этого добиться?
Я знаю, что мне нужно реализовать RecyclerView.OnTouchListener, но я не знаю, что делать после этого? I