有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java listView不会从底部填充

我正在为一个学校项目做一个聊天活动,我想在列表视图的底部显示最新消息。添加下面的代码不起作用。我只是希望最新的消息出现在底部,并使listView自动滚动到最后一个列表项。提前谢谢

安卓:transcriptMode="alwaysScroll"
安卓:stackFromBottom="true"

Link to image of current situation

XML:

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


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

            />

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


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


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


</RelativeLayout>

爪哇

package com.example.muhryn.resonatem;


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

import 安卓.content.Context;
import 安卓.view.inputmethod.InputMethodManager;
import 安卓.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) 个答案

  1. # 1 楼答案

    请尝试下面的代码

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

    它将向下滚动至最新聊天记录