2016-03-30 120 views
1

我正在爲學校項目進行聊天活動,我希望listView底部的最新消息。添加下面的代碼不起作用。我只是希望最新的消息出現在底部,並使listView自動滾動到最後一個列表項。提前致謝!listView將不會從底部填充

android:transcriptMode="alwaysScroll" 
android:stackFromBottom="true" 

Link to image of current situation

XML:

<android.support.v7.widget.Toolbar 
     android:id="@+id/my_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     /> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#1a1a1a" 
     android:id="@+id/relativeLayout_Messages" 
     android:layout_below="@id/my_toolbar" 
     android:layout_alignBottom="@+id/imageView_Separator"> 
     <ListView 
      android:id="@+id/listView_Messages" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:stackFromBottom="true" 

      /> 

    </RelativeLayout> 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView_Separator" 
     android:background="#1a1a1a" 
     android:src="@drawable/seperator" 
     android:layout_above="@+id/relativeLayout_ChatInput" 
     /> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:id="@+id/relativeLayout_ChatInput" 
     android:background="#1a1a1a"> 


     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:id="@+id/editText_ChatInput" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginStart="5dp" 
      android:background="@drawable/search_field" 
      android:layout_toStartOf="@+id/imageButton_Send" 
      android:layout_alignBottom="@+id/imageButton_Send" 
      android:inputType="textPersonName" 
      android:hint="@string/chat_field_hint" 
      android:textColor="#f4f5f6" 
      android:textColorHint="#f4f5f6"/> 


     <ImageButton 
      android:layout_width="32dp" 
      android:layout_height="32dp" 
      android:id="@+id/imageButton_Send" 
      android:background="@android:color/transparent" 
      android:src="@drawable/send_button" 
      android:scaleType="fitCenter" 
      android:layout_marginTop="10dp" 
      android:layout_marginStart="10dp" 
      android:layout_marginEnd="6dp" 
      android:layout_marginBottom="8dp" 
      android:layout_alignParentEnd="true" 
      android:onClick="sendButtonClicked" 
      /> 
    </RelativeLayout> 


</RelativeLayout> 

JAVA

package com.example.muhryn.resonatem; 


import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ListView; 

import android.content.Context; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.*; 
import java.util.*; 

public class ChatActivity extends AppCompatActivity{ 

    public void hideKeyboard() { 
     try { 
      InputMethodManager imm=(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0); 
     } catch (Exception ex) { } 
    } 

    private ListView chatListView; 
    private Button submitButton; 
    private EditText chatEditText; 
    private Chat chat; 
    private ArrayList receivedList=new ArrayList(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chat); 
     chatListView=(ListView)findViewById(R.id.listView_Messages); 
     chatEditText=(EditText)findViewById(R.id.editText_ChatInput); 
     chat=new Chat("A","Resonate"); 
     chat.setListener(new Chat.Listener(){ 
      public void received(final String received) { 
       System.out.println("Received: "+received); 
       runOnUiThread(new Runnable() { 
        public void run() { 
         hideKeyboard(); 
         receivedList.add(0, received); 
         chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList)); 
        } 
       }); 
       //////((ArrayAdapter)chatListView.getAdapter()).insert(received,0); 
      } 
     }); 
    } 

    public void sendButtonClicked(View view){ 
     String textToSubmit=chatEditText.getText().toString().trim(); 
     if(textToSubmit.isEmpty()) 
      return; 
     if(chat.submitted(textToSubmit)) { 
      receivedList.add(0,"Sent: "+textToSubmit); 
      chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList)); 
     } else 
      Toast.makeText(this,"Failed to submit "+textToSubmit+".",Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     chat.stop(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.chat_menu, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.view_profile: 
       finish(); 
       return true; 
      case R.id.report_match: 
       finish(); 
       return true; 
      case R.id.add_match: 
       finish(); 
       return true; 
      case R.id.unmatch: 
       finish(); 
       return true; 


      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 






} 
+0

我不確定'stackFromBottom',但你可以隨時添加相反順序的項目並手動滾動到底部 – Sourabh

+0

如何添加相反順序的項目? – Muhryn

+1

'Collections.reverse(receivedList)'? – Sourabh

回答

2

嘗試下面的代碼。

chatListView.smoothScrollByOffset(receivedList.size()-1); 

它會向下滾動到最新的聊天。

+0

你能幫我一下,我必須把這段代碼放在哪裏嗎? – Muhryn

+0

首先在列表中添加新項目,然後在適配器內調用'notifyDatasetChanged()',然後從您的** Activity **或** Fragment **中調用上述行。 – sanjay