ejemplo android header listactivity

ejemplo - Android: agregar encabezado estático a la parte superior de un ListActivity



listactivity android ejemplo (5)

Agregar un encabezado estático es fácil, solo haga una vista relativa separada que tenga el atributo alignParentTop (o inferior, derecho o izquierdo) establecido en verdadero.

Actualmente tengo una clase que está extendiendo la clase ListActivity. Necesito poder agregar algunos botones estáticos encima de la lista que siempre están visibles. He intentado capturar el ListView usando getListView () desde dentro de la clase. Luego utilicé addHeaderView (View) para agregar un diseño pequeño a la parte superior de la pantalla.

Header.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" > <Button android:id="@+id/testButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Income" android:textSize="15dip" android:layout_weight="1" /> </LinearLayout>

Antes de configurar el adaptador hago:

ListView lv = getListView(); lv.addHeaderView(findViewById(R.layout.header));

Esto resulta en que nada le pase a ListView, excepto que se rellene desde mi base de datos. Ningún botón aparece encima de él.

Otro enfoque que probé fue agregar relleno en la parte superior de ListView. Cuando hice esto, se movió hacia abajo con éxito, sin embargo, si agregué algo por encima, empujó el ListView. No importa lo que haga, parece que no puedo colocar algunos botones encima de ListView cuando utilizo ListActivity.

Gracias por adelantado.

Synic, probé tu sugerencia previamente. Lo intenté de nuevo solo por cordura, y el botón no se mostró. A continuación se muestra el archivo de diseño de la actividad y el código que he implementado en oncreate ().

// Mi lista de actividad Estoy tratando de agregar el encabezado a

public class AuditActivity extends ListActivity { Budget budget; @Override public void onCreate(Bundle savedInstanceState) { Cursor test; super.onCreate(savedInstanceState); setContentView(R.layout.audit); ListView lv = getListView(); LayoutInflater infalter = getLayoutInflater(); ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false); lv.addHeaderView(header); budget = new Budget(this); /* try { test = budget.getTransactions(); showEvents(test); } finally { } */ // switchTabSpecial(); }

Layout.xml para la actividad:

<?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"> <ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@android:id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty" /> </LinearLayout>


Aquí está la solución más simple:

<?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="@color/background"> <include layout="@layout/actionbar"/> <ListView android:id="@+id/tasklist_TaskListView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:textColor="@color/baseFont"/> <include layout="@layout/bottombar"/> </LinearLayout>

o

<?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="@color/background"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content"/> <ListView android:id="@+id/tasklist_TaskListView" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:textColor="@color/baseFont"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>

En lugar de botón puede agregar otro diseño lineal horizontal


Después de algunas investigaciones, pude descubrir que al mezclar TableLayout y LinearLayout dentro de mi documento XML ListActivity, pude agregar un encabezado al documento. A continuación se muestra mi documento XML si alguien está interesado en verlo. Si bien el enfoque de Synic es probablemente el enfoque correcto después del trabajo con su solución, en algún momento no pude hacer que funcionara como quería.

AuditTab.java

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audittab); getListView().setEmptyView(findViewById(R.id.empty)); }

audittab.xml

<?xml version="1.0" encoding="utf-8"?> <TableLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent"> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/btnFromDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_weight="1" /> <Button android:id="@+id/btnToDate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_toRightOf="@+id/btnFromDate" android:layout_weight="1" /> <Button android:id="@+id/btnQuery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Query" android:layout_toRightOf="@+id/btnToDate" android:layout_weight="1" /> </LinearLayout> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@android:id/list" android:layout_width="300dip" android:layout_height="330dip" android:scrollbars="none" /> <TextView android:id="@+id/empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="10dip" android:text="- Please select a date range and press query." /> </LinearLayout> </TableRow> </TableLayout>

AuditItem.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:orientation="horizontal" android:padding="10sp"> <TextView android:id="@+id/transactionDateLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date: " /> <TextView android:id="@+id/transactionDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/transactionDateLabel" /> <TextView android:id="@+id/transactionTypeLabel" android:layout_below="@id/transactionDate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Type: " /> <TextView android:id="@+id/transactionType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionTypeLabel" /> <TextView android:id="@+id/transactionAmountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:text="Amount: " android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionType" /> <TextView android:id="@+id/transactionAmount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionDate" android:layout_toRightOf="@id/transactionAmountLabel" /> <TextView android:id="@+id/transactionCategoryLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Category: " android:layout_below="@id/transactionAmountLabel" /> <TextView android:id="@+id/transactionCategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionAmountLabel" android:layout_toRightOf="@id/transactionCategoryLabel" /> <TextView android:id="@+id/transactionToAccountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="To Account: " android:layout_below="@id/transactionCategoryLabel" /> <TextView android:id="@+id/transactionToAccount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/transactionCategoryLabel" android:layout_toRightOf="@id/transactionToAccountLabel" /> <TextView android:id="@+id/transactionFromAccountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="From Account: " android:layout_below="@id/transactionToAccountLabel" /> <TextView android:id="@+id/transactionFromAccount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionToAccountLabel" android:layout_toRightOf="@id/transactionFromAccountLabel" /> <TextView android:id="@+id/transactionNoteLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Note: " android:layout_below="@id/transactionFromAccountLabel" /> <TextView android:id="@+id/transactionNote" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/transactionFromAccountLabel" android:layout_toRightOf="@id/transactionNoteLabel" /> <Button android:id="@+id/editTransactionBtn" android:layout_width="wrap_content" android:layout_height="40sp" android:visibility="gone" android:text="Edit" android:layout_below="@id/transactionNoteLabel"/> <Button android:id="@+id/deleteTransactionBtn" android:layout_width="wrap_content" android:layout_height="40sp" android:text="Delete" android:layout_below="@+id/transactionNoteLabel" android:visibility="gone" android:layout_toRightOf="@+id/editTransactionBtn" android:ellipsize="end"/> </RelativeLayout>


La respuesta de ListView anterior es útil pero se desplaza con la lista y no mantiene los gráficos de encabezado en la parte superior. La mejor solución que he encontrado es establecer un título personalizado para la actividad. Aquí es como se ve mi constructor:

public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.your_listview_layout); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.your_header); ...

Donde your_listview_layout.xml configura un ListView, y your_header.xml contiene cualquier diseño de encabezado personalizado que desee. Solo tenga en cuenta que las tres líneas anteriores deben llamarse exactamente en ese orden para no causar problemas de tiempo de ejecución.

El tutorial que me ayudó fue http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ y puede encontrar muchas páginas relacionadas en buscando el término " setFeatureInt "


findViewById() solo funciona para encontrar subvistas del objeto View. No funcionará en un ID de diseño.

Tendrá que usar el diseño de inflado para convertir el xml a sus componentes de Vista correspondientes. Algo como esto:

ListView lv = getListView(); LayoutInflater inflater = getLayoutInflater(); View header = inflater.inflate(R.layout.header, lv, false); lv.addHeaderView(header, null, false);

No estoy seguro de por qué su código no estaba simplemente lanzando un error. findViewById() probablemente estaba devolviendo un null , por lo que no se agregó ningún encabezado a su lista.