我在android studio中创建了一个简单的聊天应用程序。最初,当我发送文本时,recyclerview的最后一项始终处于焦点,但是当我隐藏软键盘然后再次单击editext发送消息时,recyclelerview中的最后一项不在焦点上。当软键盘处于焦点时,我需要向下滚动才能看到最后一条消息。 下面是布局和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()但它最初只在软键盘从头开始可见时起作用。但是,如果我隐藏然后聚焦软键盘,则列表中的最后一项不在焦点上。
答案 0 :(得分:0)
您应该在RecyclerView的布局管理器上执行此操作。下面的代码将堆叠RecyclerView底部的每个项目,并将焦点保持在底部。
LinearLayoutManager llm = new LinearLayoutManager(getContext());
llm.setReverseLayout(true);
llm.setStackFromEnd(true);
myRecyclerView.setLayoutManager(llm);
答案 1 :(得分:0)
您可以尝试以下选项:
LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
layoutManager.setStackFromEnd(true);
(为我工作)
(如果使用setReverseLayout(true),则List将以相反的顺序显示)
另一个答案建议使用onClickListener和
mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1);
答案 2 :(得分:-1)
您可以尝试以下方法:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);