listView不会从底部填充

时间:2016-03-30 15:04:29

标签: java android xml 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);
        }
    }






}

1 个答案:

答案 0 :(得分:2)

请尝试以下代码。

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

它将向下滚动到最新的聊天。