我按照这个例子创建了一个聊天:http://warting.se/2012/06/04/chat-bubbles-in-android/ 工作正常,但我希望当我添加一条新消息时,消息显示在 ListView 的底部而不是添加在顶部,例如 whatsapp。
我做了
<RelativeLayout 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/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:divider="@android:color/transparent"
android:dividerHeight="0dp">
</ListView>
<RelativeLayout
android:id="@+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/chatText"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/buttonSend"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enviar"
android:id="@+id/buttonSend"
android:layout_alignBottom="@+id/chatText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@color/action_bar"
android:textColor="#FFF"/>
</RelativeLayout>
列表适配器
public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {
private TextView countryName;
private List<OneComment> countries = new ArrayList<OneComment>();
private LinearLayout wrapper;
public DiscussArrayAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public void add(OneComment object) {
countries.add(object);
super.add(object);
}
public int getCount() {
return this.countries.size();
}
public OneComment getItem(int index) {
return this.countries.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
OneComment coment = getItem(position);
countryName = (TextView) row.findViewById(R.id.comment);
countryName.setText(coment.comment);
countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
}
最佳答案
取而代之的是:
countries.add(object);
super.add(object);
使用这个:
countries.add(0,object);
super.insert(0,object);
0
表示在数组中的位置,即开头。
关于android - ListView从下往上添加消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27528653/