Show items from a list from the bottom up on Android

1

I have a custom listview in my android application, but I have a question I want to know how I show the data I add to that list from the bottom up like chat apps.

Below I share my code

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

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lista_todos_mensajes" />

</LinearLayout> 

custom item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c0303b47"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@mipmap/icono"
            android:id="@+id/imageView"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:orientation="horizontal"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/imageView"
            android:layout_toEndOf="@+id/imageView">

            <TextView
                android:id="@+id/txt_sms_nombre_ruta"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Nombre ruta"
                android:textColor="#fabd05"
                android:textSize="15sp"
                android:typeface="sans"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp">
                <TextView
                    android:id="@+id/txt_sms_conductor"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:text="Mensaje"
                    android:textColor="@color/blanco"
                    android:textSize="15sp"
                    android:typeface="sans" />

                <TextView
                    android:id="@+id/txt_sms_hora"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginRight="10dp"
                    android:text="Fecha y hora"
                    android:textColor="@color/blanco"
                    android:textSize="12sp" />
            </LinearLayout>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

and in my activity I do the following:

adaptador = new Adaptador_mensajes_estudiante(this,datos);
lista_todos_mensajes.setAdapter(adaptador);

Adapter:

public class Adaptador_mensajes_estudiante extends BaseAdapter {
    protected Activity activity;
    protected ArrayList<Datos_mensajes_estudiante> items;
    public Adaptador_mensajes_estudiante(Activity activity, ArrayList<Datos_mensajes_estudiante> items){
        this.activity = activity;
        this.items = items;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if(convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflater.inflate(R.layout.item_mensaje_estudiante, null);
        }
        Datos_mensajes_estudiante datos_mensajes_estudiante = items.get(position);
        TextView txt_sms_nombre_ruta = (TextView)v.findViewById(R.id.txt_sms_nombre_ruta);
        TextView txt_sms_conductor = (TextView)v.findViewById(R.id.txt_sms_conductor);
        TextView txt_sms_hora = (TextView)v.findViewById(R.id.txt_sms_hora);
        txt_sms_nombre_ruta.setText(datos_mensajes_estudiante.getNombre_ruta());
        txt_sms_conductor.setText(datos_mensajes_estudiante.getMensaje_conductor());
        txt_sms_hora.setText(datos_mensajes_estudiante.getHora_mensaje());
        return v;
    }
}

I already try to place android: stackFromBottom="true" in my listview and nothing is always shown from top to bottom

Thank you in advance

    
asked by Dimoreno 18.05.2017 в 17:59
source

3 answers

1

You should use the reverse method:

adaptador = new Adaptador_mensajes_estudiante(this,datos);
lista_todos_mensajes.setAdapter(Collections.reverse(adaptador));

If that does not work, in the JAVA code of your adapter returns the reverse of it.

Another possibility is to insert the last item in position 0 by overloading insert(T,int) of the insert method:

 insert("Nuevoelemento", 0)

Another possible Approximation is to overwrite the GetItem method of the Adapter, so that it returns the last one instead of the first:

@Override
public Item getItem(int position) {
    return super.getItem(getCount() - position - 1);
}
    
answered by 18.05.2017 в 18:06
0

Use Collections.reverse() to change the order of the data that feeds your Adapter:

//*** invierte el orden de los datos.
Collections.reverse(datos); 
//instancia Adapter
adaptador = new Adaptador_mensajes_estudiante(this, datos);
    
answered by 01.06.2017 в 23:01
0

A little late, but good if someone serves you:

    lv = (ListView)findViewById(R.id.listView);
    Adaptador_mensajes_estudiante adapter = new Adaptador_mensajes_estudiante(this,datos);
    lv.setAdapter(adapter);
    lv.setSelection(datos.size() - 1);  //Esta es la linea clave. Indicamos que el focus al listView se realize en el último elemento de este.

It worked for me.

    
answered by 11.08.2017 в 20:05