2016-03-30 7 views
1

Ich mache eine Chat-Aktivität für ein Schulprojekt und ich möchte die neuesten Nachrichten am Ende der ListView. Das Hinzufügen des folgenden Codes funktioniert nicht. Ich möchte einfach, dass die neueste Nachricht am unteren Rand erscheint und die listView automatisch zum letzten Listeneintrag scrollt. Danke im Voraus!listView wird nicht von unten ausgefüllt

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

Ich bin mir nicht sicher über 'stackFromBottom', aber Sie können immer Elemente in umgekehrter Reihenfolge hinzufügen und manuell nach unten scrollen – Sourabh

+0

Wie fügen Sie Elemente in umgekehrter Reihenfolge hinzu? – Muhryn

+1

'Collections.reverse (receivedList)'? – Sourabh

Antwort

2

den Code unten versuchen.

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

Es wird bis zum letzten Chat herunterscrollen.

+0

Können Sie mir helfen, wo genau ich diesen Code einfügen muss? – Muhryn

+0

Fügen Sie zuerst ein neues Element in der Liste hinzu und rufen Sie dann 'notifyDatasetChanged()' in Ihrem Adapter auf. Rufen Sie dann die obige Zeile von Ihrer * Aktivität ** oder ** Fragment ** auf. – sanjay

Verwandte Themen