我在android studio中创建了一个简单的聊天应用程序。最初,当我发送文本时,回收站视图的最后一项始终处于焦点状态,但是当我隐藏软键盘时,然后再次单击editext以发送消息时,回收站视图中的最后一项未处于焦点状态。我需要向下滚动才能看到软键盘对焦时的最后一条消息。 下面是布局,在MainActivity代码:当软键盘在聊天应用程序中可见时,保持回收站的最后一项可见
EditText messageEdt;
ImageView sendBtn;
RecyclerView recyclerView;
CustomAdapter adapter;
List<Model> chatList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageEdt = (EditText) findViewById(R.id.messageText);
sendBtn = (ImageView) findViewById(R.id.sendBtn);
recyclerView = (RecyclerView) findViewById(R.id.recyclerChatList);
chatList = new ArrayList<>();
adapter = new CustomAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
messageEdt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b && adapter.getItemCount()>1){
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,null,adapter.getItemCount()-1);
}
}
});
getSupportActionBar().hide();
}
private void getData() {
String messageStr = messageEdt.getText().toString().replaceAll("[ ]","%20");
Log.e("messageStr",messageStr);
Model model = new Model();
model.setMessage(messageEdt.getText().toString());
model.setUser("User");
chatList.add(model);
messageEdt.setText("");
adapter.getChatList(chatList);
adapter.notifyDataSetChanged();
if(adapter.getItemCount()>1){
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,null,adapter.getItemCount()-1);
}
url = "http://chat.vicz.in/get/";
url = url.concat(messageStr);
Log.e("Url", url);
StringRequest getData = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
url = "http://chat.vicz.in/get/";
Log.e("Response", response);
JSONObject responseObj = new JSONObject(response);
String stringResponse = responseObj.getString("response");
Model data = new Model();
data.setMessage(stringResponse);
data.setUser("Bot");
chatList.add(data);
recyclerView.smoothScrollToPosition(chatList.size()-1);
adapter.getChatList(chatList);
adapter.notifyDataSetChanged();
if(adapter.getItemCount()>1){
recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView,null,adapter.getItemCount()-1);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
url = "http://chat.vicz.in/get/";
}
});
getData.setRetryPolicy(new DefaultRetryPolicy(30000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Volley.newRequestQueue(this).add(getData);
}
public void sendMessage(View view) {
getData();
}
这里是我的聊天应用程序的布局:)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.admin.chatbot.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerChatList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@null"
android:divider="@null"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/messageText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="80"
android:hint="Enter Message" />
<ImageView
android:id="@+id/sendBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:onClick="sendMessage"
android:src="@android:drawable/ic_menu_send" />
</LinearLayout>
我已经使用smoothScrollToPosition(但它仅适用最初当软键盘从一开始就是可见的。但是,如果我隐藏然后聚焦软键盘,则列表中的最后一项未聚焦。
其打印以相反的顺序聊天列表建议。 –
我想使它像whatsapp。当您点击editext时,焦点将移至聊天室中的最后一个文本。 –
除了有一个'OnFocusChangeListener'尝试添加一个'OnClickListener'到'EditText'并做平滑的滚动吗? –