Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok finally got this working. Maximus and OcuS, thanks for all your help. I was a big help. Also got lots of info and examples from these two websites:</p> <ul> <li><a href="http://android-journey.blogspot.com/2009/12/android-selectors.html" rel="nofollow noreferrer">http://android-journey.blogspot.com/2009/12/android-selectors.html</a></li> <li><a href="http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/" rel="nofollow noreferrer">http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/</a></li> </ul> <p>Here is a working example. Hope it helps out anyone else who is trying to do the same thing.</p> <p><img src="https://i.stack.imgur.com/n8Abg.png" alt="scrolling listview with fixed header/footer"></p> <p><strong>The files to do it:</strong></p> <hr> <ul> <li><code>myList.java</code></li> </ul> <p>The main activity. Extends ListActivity and includes a custom adapter which is used to populate the list.</p> <hr> <ul> <li><code>drawable/xml_listitem_shape.xml</code></li> </ul> <p>This controls the Pressed, Selected and Normal states of the list items using gradient 'shapes' instead of images. The gradients allow for faster rendering, and are not specific to the device, so it gets you away from the multiple-image mess (hdpi, mdp, ldpi...)</p> <hr> <ul> <li><code>layout/main.xml</code></li> </ul> <p>Contains the layout for the Header, Footer and Listview objects. Does not use any selector files, but declares <strong>android:id/list</strong> as an ID for the listview object, which is required. Android will complain about not finding this ID if you do not do it.</p> <hr> <ul> <li><code>layout/menu_item.xml</code></li> </ul> <p>Contains only a TextView object for use by the dynAdap class (no layout needed). This file declares the <strong>xml_listitem_shape</strong> selector file as it's background, which defines how the listitem will appear in it's various states. </p> <hr> <ul> <li><code>values/colors.xml</code></li> </ul> <p>Color definitions used throughout the application. You can hard code your colors, but this file keeps things much cleaner.</p> <hr> <p><strong>myList.java</strong></p> <pre><code>package com.test.listview; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class myList extends ListActivity { public final String TAG = "** myList **"; String[] mNames = new String[] { "Linux", "Plan9", "Eclipse", "Java","Ubuntu", "Next", "Android", "Xoom", "Pascal", "Assembly", "C++", "Perl", "Bash", "Korn", "Int3", "CS:IP" }; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView (R.layout.main); Button b1 = (Button) findViewById (R.id.button1); Button b2 = (Button) findViewById (R.id.button2); Button b3 = (Button) findViewById (R.id.button3); Button b4 = (Button) findViewById (R.id.button4); ListView listView = getListView(); setListAdapter (new dynAdap (this, android.R.layout.simple_list_item_1, mNames)); listView.setOnItemClickListener (oicl); b1.setOnClickListener (ocl); b2.setOnClickListener (ocl); b3.setOnClickListener (ocl); b4.setOnClickListener (ocl); } /* * listener for buttons */ OnClickListener ocl = new OnClickListener() { @Override public void onClick (View v) { String b = new String (""); switch (v.getId ()) { case R.id.button1: b = "button1"; break; case R.id.button2: b = "button2"; break; case R.id.button3: b = "button3"; break; case R.id.button4: b = "button4"; break; } Toast.makeText (myList.this, b, Toast.LENGTH_SHORT).show(); } }; /* * listener for listview clicks - pop up toast to show what was selected */ OnItemClickListener oicl = new OnItemClickListener() { @Override public void onItemClick (AdapterView&lt;?&gt; parent, View view, int index, long id) { Toast.makeText (myList.this, mNames[index], Toast.LENGTH_SHORT).show(); } }; /* * This is a custom list adapter to set the color and text content of each list item */ public class dynAdap extends ArrayAdapter&lt;String&gt; { String [] list; public dynAdap (Context context, int textViewResourceId, String [] objects) { super (context, textViewResourceId, objects); list = objects; } @Override public View getView (int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater (); // return the view associated with the TextView in the menu_item.xml file View row = inflater.inflate (R.layout.menu_item, parent, false); TextView tv1 = (TextView) row.findViewById (R.id.tv_item); tv1.setText (list[position]); return row; } } } </code></pre> <p><strong>xml_listitem_shape.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;!-- pressed state of item --&gt; &lt;item android:state_pressed="true" &gt; &lt;shape&gt; &lt;gradient android:startColor="@color/DkRed" android:endColor="@color/Red" android:angle="270" /&gt; &lt;stroke android:width="3dp" android:color="@color/LightGreen" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;!-- focused state of item --&gt; &lt;item android:state_selected="true" &gt; &lt;shape&gt; &lt;gradient android:endColor="@color/Silver" android:startColor="@color/Gray" android:angle="270" /&gt; &lt;stroke android:width="3dp" android:color="@color/Red" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;!-- normal state of item --&gt; &lt;item&gt; &lt;shape&gt; &lt;gradient android:endColor="@color/White" android:startColor="@color/Silver" android:angle="270" /&gt; &lt;stroke android:width="3dp" android:color="@color/LightBlue" /&gt; &lt;corners android:radius="3dp" /&gt; &lt;padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /&gt; &lt;/shape&gt; &lt;/item&gt; &lt;/selector&gt; </code></pre> <p><strong>main.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"&gt; &lt;RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/top_control_bar"&gt; &lt;TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="@color/LightBlue" android:gravity="center" android:padding="5dip"&gt; &lt;Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt; &lt;Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt; &lt;Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt; &lt;/TableRow&gt; &lt;/RelativeLayout&gt; &lt;LinearLayout android:id="@+id/bottom_control_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/LightBlue" android:padding="10dip"&gt; &lt;Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Add Item" android:id="@+id/button4" /&gt; &lt;/LinearLayout&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="0dip" android:choiceMode="multipleChoice" android:layout_below="@id/top_control_bar" android:layout_above="@id/bottom_control_bar" android:background="@color/Silver"&gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>menu_item.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold" android:paddingTop="20dip" android:paddingBottom="20dip" android:layout_gravity="center" android:gravity="center" android:textColor="#000000" android:background="@drawable/xml_listitem_shape" android:text="Fooooooo" android:textSize="22dip" android:id="@+id/tv_item" /&gt; </code></pre> <p><strong>colors.xml</strong></p> <pre><code>&lt;resources&gt; &lt;color name="transparent"&gt;#00000000&lt;/color&gt; &lt;!-- colors used in application --&gt; &lt;color name="Black"&gt;#000000&lt;/color&gt; &lt;color name="DkRed"&gt;#660000&lt;/color&gt; &lt;color name="Red"&gt;#b70101&lt;/color&gt; &lt;color name="White"&gt;#f7f5e8&lt;/color&gt; &lt;color name="Silver"&gt;#c8c5bb&lt;/color&gt; &lt;color name="Gray"&gt;#6e6a5b&lt;/color&gt; &lt;color name="Yellow"&gt;#f6f900&lt;/color&gt; &lt;color name="Orange"&gt;#ff9000&lt;/color&gt; &lt;color name="LightGreen"&gt;#00ff00&lt;/color&gt; &lt;color name="Green"&gt;#085c00&lt;/color&gt; &lt;color name="Gold"&gt;#ccaf00&lt;/color&gt; &lt;color name="LightBlue"&gt;#0077ff&lt;/color&gt; &lt;color name="Blue"&gt;#000077&lt;/color&gt; &lt;color name="LightCyan"&gt;#00ffff&lt;/color&gt; &lt;color name="Cyan"&gt;#007777&lt;/color&gt; &lt;/resources&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload