如何使列表视图中的最新条目元素始终可见

时间:2013-09-14 11:07:39

标签: android listview

我正在创建一个聊天应用程序,我在其中使用列表视图来显示每个聊天消息。我以这样的方式创建了xml布局,使列表顶部有一个编辑文本和一个按钮。 我的问题是,当聊天消息超出屏幕大小时,它会低于编辑文本,因此我必须向上滚动才能看到消息..我希望以前的所有消息都显示在上方,而最新消息应该在上面显示edittext视图.. 任何人都可以帮我解决这个问题.. 我的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> 

java代码:

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");
            }
        });

    }

}

2 个答案:

答案 0 :(得分:0)

使用 RelativeLayout 作为根布局

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

使用 android:layout_above="@+id/llout"

将ListView置于包含EditText的布局顶部

这样,当条目超出屏幕大小时,您的列表将自动滚动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>

答案 1 :(得分:0)

使用android:transcriptMode on your ListView,将其设置为normalalwaysScroll