-1
我正在開發一個演示聊天應用程序,如果我發送消息,則在2秒後收到默認消息作爲回覆。 recyclerview將自動滾動到最後一個位置。一切正常。根據當前滾動位置在Recyclerview中自動滾動
但我有一個要求,如果我滾動到頂部/中間或任何其他地方,除了結束,如果我收到來自另一方的任何消息,我不想自動滾動結束。爲了簡化工作,應該和Whatsup相同。請找到我的Activity類。
public class ChatRoomActivity extends AppCompatActivity {
private String TAG = ChatRoomActivity.class.getSimpleName();
private RecyclerView recyclerView;
private ChatRoomThreadAdapter mAdapter;
private ArrayList<Message> messageArrayList;
private EditText inputMessage;
private ImageView btnSend;
String selfUserId ="100";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_chat);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
inputMessage = (EditText) findViewById(R.id.etChatMessage);
btnSend = (ImageView) findViewById(R.id.btnYou);
getSupportActionBar().setTitle(getResources().getString(R.string.driver_prof_name));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
recyclerView = (RecyclerView) findViewById(R.id.rvChatList);
messageArrayList = new ArrayList<>();
// self user id is to identify the message owner
mAdapter = new ChatRoomThreadAdapter(this, messageArrayList, selfUserId);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
new CountDownTimer(2000, 1000) {
public void onFinish() {
// When timer is finished
// Execute your code here
fetchChatThread();
}
public void onTick(long millisUntilFinished) {
// millisUntilFinished The amount of time until finished.
}
}.start();
}
});
}
/**
* Handling new push message, will add the message to
* recycler view and scroll it to bottom
* */
/**
* Posting a new message in chat room
* will make an http call to our server. Our server again sends the message
* to all the devices as push notification
* */
private void sendMessage() {
final String strmessage = this.inputMessage.getText().toString().trim();
if (TextUtils.isEmpty(strmessage)) {
Toast.makeText(getApplicationContext(), "Enter a message", Toast.LENGTH_SHORT).show();
return;
}
this.inputMessage.setText("");
String commentId = "message_id";
String commentText = strmessage;
Calendar c = Calendar.getInstance();
SimpleDateFormat dateformat = new SimpleDateFormat("hh:mm aa");
String datetime = dateformat.format(c.getTime());
String createdAt = datetime;
String userId = selfUserId;
String userName = "name";
User user = new User(userId, userName, null);
Message message = new Message();
message.setId(commentId);
message.setMessage(commentText);
message.setCreatedAt(createdAt);
message.setUser(user);
messageArrayList.add(message);
mAdapter.notifyDataSetChanged();
if (mAdapter.getItemCount() > 1) {
// scrolling to bottom of the recycler view
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, mAdapter.getItemCount() - 1);
}
}
/**
* Fetching all the messages of a single chat room
* */
private void fetchChatThread() {
String commentId = "message_id";
String commentText = "Replay from Other";
Calendar c = Calendar.getInstance();
SimpleDateFormat dateformat = new SimpleDateFormat("hh:mm aa");
String datetime = dateformat.format(c.getTime());
String createdAt = datetime;
String userId = "user_id";
String userName = "username";
User user = new User(userId, userName, null);
Message message = new Message();
message.setId(commentId);
message.setMessage(commentText);
message.setCreatedAt(createdAt);
message.setUser(user);
messageArrayList.add(message);
mAdapter.notifyDataSetChanged();
if (mAdapter.getItemCount() > 1)
{
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, mAdapter.getItemCount() - 1);
}
}
適配器類是
public class ChatRoomThreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static String TAG = ChatRoomThreadAdapter.class.getSimpleName();
private String userId;
private int SELF = 100;
private static String today;
private Context mContext;
private ArrayList<Message> messageArrayList;
public class ViewHolder extends RecyclerView.ViewHolder {
TextView message, timestamp;
public ViewHolder(View view) {
super(view);
message = (TextView) itemView.findViewById(R.id.tvChatMessage);
timestamp = (TextView) itemView.findViewById(R.id.tvChatTimeStamp);
}
}
public ChatRoomThreadAdapter(Context mContext, ArrayList<Message> messageArrayList, String userId) {
this.mContext = mContext;
this.messageArrayList = messageArrayList;
this.userId = userId;
Calendar calendar = Calendar.getInstance();
today = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
// view type is to identify where to render the chat message
// left or right
if (viewType == SELF) {
// self message
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_driver_chat_itself, parent, false);
} else {
// others message
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_driver_chat_others, parent, false);
}
return new ViewHolder(itemView);
}
@Override
public int getItemViewType(int position) {
Message message = messageArrayList.get(position);
if (message.getUser().getId().equals("100")) {
return SELF;
}
return position;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Message message = messageArrayList.get(position);
((ViewHolder) holder).message.setText(message.getMessage());
// String timestamp = getTimeStamp(message.getCreatedAt());
//
// if (message.getUser().getName() != null)
// timestamp = message.getUser().getName() + ", " + timestamp;
((ViewHolder) holder).timestamp.setText(message.getCreatedAt());
}
@Override
public int getItemCount() {
return messageArrayList.size();
}
public static String getTimeStamp(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = "";
today = today.length() < 2 ? "0" + today : today;
try {
Date date = format.parse(dateStr);
SimpleDateFormat todayFormat = new SimpleDateFormat("dd");
String dateToday = todayFormat.format(date);
format = dateToday.equals(today) ? new SimpleDateFormat("hh:mm a") : new SimpleDateFormat("dd LLL, hh:mm a");
String date1 = format.format(date);
timestamp = date1.toString();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
它的作品,我upvoted因爲它的有用,但只有當它,只能滾動到最後的位置,沒有變化的作品!我用另一個,int finalVisiblePosition = layoutManager.findLastVisibleItemPosition(); if(finalVisiblePosition == mAdapter.getItemCount() - 2) recyclerView.getLayoutManager()。smoothScrollToPosition(recyclerView,null,mAdapter.getItemCount() - 1); }這並不完美,但更接近我的要求。需要改進一個完全像whatsup。 – Noufal
在你的問題中,你說'除了結束之外的任何地方'。無論如何,很高興你知道了。 – Sohaib
我沒有想出正確的!它只是關閉..你知道當我們在Whatsup中滾動到任何其他位置時,我們從別人那裏得到了一個重播,你知道你的改變..確切地說,這就是我需要的.. – Noufal