1

长话短说:我的活动方法更新并通过 ArrayAdapter 滚动 ListView 就像它应该的那样,但是用于轮询消息(显示在 ListView 中)的内部 TimerTask 方法更新 ListView,但不滚动它。为什么?

很长的故事:

我有这个布局的聊天活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    >
    <ListView android:id="@+id/messageList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
        android:fadeScrollbars="true"
    />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        <EditText android:id="@+id/message"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
        />
        <Button android:id="@+id/button_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"
            android:onClick="sendMessage"
        />
    </LinearLayout>
</LinearLayout>

内部 listView(带有 id messageList)由一个 ArrayAdapter 填充,它扩展下面的 XML 并替换其中的字符串。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:background="#fff"
    android:paddingLeft="2dp"
    android:paddingRight="2dp"
    >
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/date"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="16sp"
     android:textColor="#00F"
     android:typeface="monospace"
     android:text="2010-10-12 12:12:03"
     android:gravity="left"
 />
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/sender"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textSize="16sp"
     android:textColor="#f84"
     android:text="spidey"
     android:gravity="right"
     android:textStyle="bold"
 />
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/body"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textSize="14sp"
     android:padding="1dp"
     android:gravity="left"
     android:layout_below="@id/date"
     android:text="Mensagem muito legal 123 quatro cinco seis."
     android:textColor="#000"
 />
</RelativeLayout>

问题是:在主布局中,我有一个用于聊天消息的 EditText 和一个用于发送消息的 Button。我已经在活动范围内声明了适配器:

public class ChatManager extends Activity{

 private EditText et;
 private ListView lv;
 private Timestamp lastDate = null;
 private long campaignId;
 private ChatAdapter ca;
 private List<ChatMessage> vetMsg = new ArrayList<ChatMessage>();
 private Timer chatPollingTimer;
 private static final int CHAT_POLLING_PERIOD = 10000;
...
}

因此,在 sendMessage(View v) 内部,notifyDataSetChanged() 会相应地滚动 ListView,这样我就可以自动看到最新的聊天消息:

 public void sendMessage(View v) {
  String msg = et.getText().toString();

  if(msg.length() == 0){
   return;
  }

  et.setText("");

  String xml = ServerCom.sendAndGetChatMessages(campaignId, lastDate, msg);
  Vector<ChatMessage> vetNew = Chat.parse(new InputSource(new StringReader(xml)));
  //Pegando a última data
  if(!vetNew.isEmpty()){
   lastDate = vetNew.lastElement().getDateSent();
   //Atualizando a tela
   vetMsg.addAll(vetNew);
   ca.notifyDataSetChanged();
  }
    }

但是在我的 TimerTask 中,我不能。ListView 已更新,但它不会自动滚动。我究竟做错了什么?

 private class chatPollingTask extends TimerTask {
  @Override
  public void run() {
   String xml;

   if(lastDate != null){
    //Chama o Updater
    xml = ServerCom.getChatMessages(campaignId, lastDate);
   }else{
    //Chama o init denovo
    xml = ServerCom.getChatMessages(campaignId);
   }

   Vector<ChatMessage> vetNew = Chat.parse(new InputSource(new StringReader(xml)));
   if(!(vetNew.isEmpty())){
    //TODO: descobrir porque o chat não está rolando quando chegam novas mensagens
    //Descobrir também como forçar o rolamento, enquanto o bug não for corrigido.
    Log.d("CHAT", "New message(s) acquired!");
    lastDate =  vetNew.lastElement().getDateSent();
    vetMsg.addAll(vetNew);
    ca.notifyDataSetChanged();
   }

  }
 }

如何强制滚动到底部?我尝试使用 lv.getBottom()-lv.getHeight() 使用 scrollTo,但没有奏效。这是 Android SDK 中的错误吗?

对不起大量的代码,但我想这样问题就很清楚了。

4

1 回答 1

0

您需要在 UI 线程上调用对数据集的更改 (notifyDataSetChanged())。TimerTask 在不同的线程上被调用。有多种方法可以实现这一点,请参阅此博客文章以获取方法列表。

关于scrollTo,在滚动列表后不用于滚动。它滚动整个 View 元素..不完全是你所期望的。相反,请使用 ListView.setSelection 方法之一。

于 2010-05-19T19:01:38.617 回答