我在android studio中创建了一个简单的聊天应用程序。最初,当我发送文本时,recyclerview的最后一项始终处于焦点状态,但是当我隐藏软键盘然后再次单击editext发送消息时,recyclerview的最后一项则没有焦点。当软键盘对准焦点时,我需要向下滚动以查看最后一条消息。
下面是布局和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(),但是仅当从一开始就可以看到软键盘时,它才可以开始使用。但是,如果我隐藏软键盘然后将其聚焦,则列表中的最后一项将不清晰。

最佳答案

您应该在RecyclerView的布局管理器上执行此操作。下面的代码将从RecyclerView的底部堆叠每个项目,并将焦点保持在底部。

LinearLayoutManager llm = new LinearLayoutManager(getContext());
llm.setReverseLayout(true);
llm.setStackFromEnd(true);
myRecyclerView.setLayoutManager(llm);

07-26 06:35