1

我正在创建一个聊天应用程序,在该应用程序中我使用列表视图来显示每条聊天消息。我以这样一种方式制作了 xml 布局,即在列表顶部向下放置一个编辑文本和一个按钮。我的问题是,当聊天消息超出屏幕大小时,它会低于编辑文本,因此我必须向上滚动才能看到消息。我希望所有以前的消息都放在上面,而最新消息应该显示在上方编辑文本视图..任何机构都可以帮我解决这个问题..我的xml布局代码如下所示..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
     >

  <ListView 
  android:id="@+id/listView"
  android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  android:layout_weight="1.0"
  ></ListView>


<LinearLayout 
       android:id="@+id/llout"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"

       android:gravity="bottom">
     <EditText
        android:id="@+id/txt_inputText"
        android:layout_width="125px"
        android:layout_height="wrap_content"
        android:text = "Text here"
        android:layout_weight="0.8"
       />
     <Button
        android:id="@+id/btn_Send"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:text="Send"
        />
</LinearLayout>
  </LinearLayout> 

爪哇代码:

public class AndyChatActivity extends Activity {

    private Handler handler = new Handler();
    public ListView msgView;
    List<String> values=new ArrayList<String>();
    LazyAd l;
    public ArrayAdapter<String> msgList;
//  public ArrayAdapter<String> msgList=new ArrayAdapter<String>(this,
//          android.R.layout.simple_list_item_1);;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listtest);

         msgView = (ListView) findViewById(R.id.listView);

        msgList = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1);
        msgView.setAdapter(msgList);

//      msgView.smoothScrollToPosition(msgList.getCount() - 1);

        Button btnSend = (Button) findViewById(R.id.btn_S);

        receiveMsg();
        btnSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(AndyChatActivity.this, "here", Toast.LENGTH_SHORT).show();
                final EditText txtEdit = (EditText) findViewById(R.id.txt_i);
                String msg=txtEdit.getText().toString();
                Toast.makeText(AndyChatActivity.this, msg, Toast.LENGTH_SHORT).show();
                //msgList.add(txtEdit.getText().toString());
                Log.d("checking", "below botton");
                sendMessageToServer(txtEdit.getText().toString());
                msgView.smoothScrollToPosition(msgList.getCount() - 1);

            }           
        });

        //receiveMsg();
        //----------------------------
        //server msg receieve
        //-----------------------



        //End Receive msg from server//
    }
    public void sendMessageToServer(String str) {

        final String str1=str;
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                //String host = "https://www.tudens.in";
                String host="192.168.1.4";
                //Toast.makeText(AndyChatActivity.this, "thread Opened", Toast.LENGTH_SHORT).show();

                //String host2 = "127.0.0.1";
                PrintWriter out;
                try {

                    Socket socket = new Socket(host, 8008);
                    out = new PrintWriter(socket.getOutputStream());
                    Log.d("1", "good");

                    // out.println("hello");
                    out.println(str1);
                    Log.d("2", "gd");
                    out.flush();
                    String str="Me : "+str1;
                    displayMsg(str);

                    Log.d("", "hello");
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.d("", "hello222");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Log.d("", "hello4333");
                }

            }
        }).start();
            }



    public void receiveMsg()
    {
        new Thread(new Runnable()
        {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                Log.d("checking", "inside tread");

                final  String host="192.168.1.4";
                //final String host="10.0.2.2";
                //final String host="localhost";
                Socket socket = null ;
                BufferedReader in = null;
                try {
                    socket = new Socket(host,8008);
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                while(true)
                {
                    String msg = null;
                    try {
                        msg = in.readLine();
                        Log.d("","MSGGG:  "+ msg);

                        //msgList.add(msg);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if(msg == null)
                    {
                        break;
                    }
                    else
                    {
                        displayMsg(msg);
                    }
                }

            }
        }).start();


    }

    public void displayMsg(String msg)
    { 
        final String mssg=msg;
        handler.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                values.add(mssg);
                l=new LazyAd(AndyChatActivity.this,values);
            //  msgList.add(mssg);
                msgView.setAdapter(l);
                msgView.smoothScrollToPosition(msgList.getCount() - 1);
                Log.d("","hi");
            }
        });

    }

}
4

2 回答 2

0

用作RelativeLayout布局

使用 .将 EditText 放在 RelativeLayout 的底部android:layout_alignParentBottom="true"

将 ListView 放在包含 EditText 的布局的顶部,使用android:layout_above="@+id/llout"

这样,当条目超出屏幕大小时,您的 List 将自动在 EditText 顶部滚动。

事情应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@+id/listView"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_above="@+id/llout"></ListView>
    <LinearLayout
        android:id="@+id/llout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">
        <EditText
            android:id="@+id/txt_inputText"
            android:layout_width="125px"
            android:layout_height="wrap_content"
            android:text="Text here"
            android:layout_weight="0.8" />
        <Button
            android:id="@+id/btn_Send"
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="Send" />
    </LinearLayout>
</RelativeLayout>
于 2013-09-14T11:11:51.423 回答
0

android:transcriptMode在您的 上ListView使用,将其设置为normalalwaysScroll

于 2013-09-14T11:17:20.923 回答