Friday, January 9, 2015

ListView

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.

Extends Activity or ListActivity?

ListActivity it's very trivial and doesn't offer any advantage, only that saves time and making the code a little more cleaner, because it offers shortcut methods. For example, you get the onListItemClick() method which is called whenever you click a item which saves you from creating a separate listener.
However it's also said that: "ListActivity has some bugs which manifest themselves in certain use case. So ListActivity is conditonally usable on very very basic use case. And in that case is simple enough to use Activity and ListView."
http://stackoverflow.com/questions/2032417/android-activity-vs-listactivity-which-one-should-my-activity-class-extend

1. Building the UI
First you can create xml for the style of the list. Put this files inside the drawable folder (It is enough if you only paste it on the drawable-hdpi folder for example). Por lists, it's recommended to have 3 xml:
     - Row background. This provides a gradient color:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
      android:startColor="@color/list_row_start_color"
      android:endColor="@color/list_row_end_color"
      android:angle="270" />

</shape>

     - Color for when the row is focused

     - Color for when the row is selected

     - A final list selector that unites the 3 files:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/list_row_bg" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/list_row_bg_hover" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_row_bg_hover" android:state_pressed="false" android:state_selected="true"/>

</selector>

    - The list view layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />


</RelativeLayout>

tools:context=".MainActivity" Records which activity the layout is associated with (at designtime, since obviously a layout can be used by more than one layout). This will for example be used by the layout editor to guess a default theme, since themes are defined in the Manifest and are associated with activities, not layouts. This will help with things such as rendering the action bar (which is associated with the activity), a place to add onClick handlers, etc.

    - For the row, it's necessary to take into account this:



  

No comments:

Post a Comment