android - ListView从下往上添加消息?

标签 android listview listadapter

我按照这个例子创建了一个聊天: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/

相关文章:

c++ - Qt:将 C++ 中的列表连接到 QML 中的 ListView

android - Listview + BaseAdapter - 如何通知单个项目的变化?

android - webview 在 android 4.0 (ICS) 上不显示管视频

java - 从我的 ListView 中添加 ItemOnClick,使用 JSON 从 MYSQL 中获取数据

android - android中的 ListView 菜单

android - 如何在ListView中显示 "More"列表项?

android - 如何完成来自适配器的 Activity ..?

php - 将图像上传到数据库并根据 Android 中的用户请求进行检索(通过 PHP)

android - onClickListener 在 Activity 上设置

android - UnityPlayerActivity 没有收到 onCreate() 日志