2016-11-15 77 views
2

我在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(但它僅適用最初當軟鍵盤從一開始就是可見的。但是,如果我隱藏然後聚焦軟鍵盤,則列表中的最後一項未聚焦。

回答

1

你應該在你的RecyclerView的佈局管理器上做到這一點。下面的代碼將堆疊RecyclerView底部的每個項目,並將焦點保持在底部。

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

其打印以相反的順序聊天列表建議。 –

+0

我想使它像whatsapp。當您點擊editext時,焦點將移至聊天室中的最後一個文本。 –

+0

除了有一個'OnFocusChangeListener'嘗試添加一個'OnClickListener'到'EditText'並做平滑的滾動嗎? –

0

您可以嘗試以下選項:

  1. 使用AdjustResize在必要的活動在AndroidManifest.xml(Adjust Resize),也可以使用AdjustPan(這將在頂部隱藏工具欄雖然)
  2. 如果調整調整沒有奏效,那麼如果你是

LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity); layoutManager.setStackFromEnd(true);(爲我工作)

(如果setReverseLayout(真),則使用列表將顯示以相反的順序)

另一個答案已經使用onClickListener和

mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount() - 1); 
相關問題