Android UI:ListActivity & ListView

ListActivity
        ListActivity is an Activity class that specifically displays ListView. It has a built-in ListView object. As long as we set the data source, it will be displayed automatically.
 
Use custom view for screen layout
        Although ListActivity has a built-in ListView object, we can still use custom view by calling setContentView(resources id) in onCreate().

        However, it should be noted that in the custom Layout, the id of the ListView object should be set to "@id/android:list"; use android.R.id.list in the Java code.

        In the following example, by adding a TextView whose id is android:empty, when there is no data in the ListView, "No data" will be displayed.

Android UI: ListView - 奥奶- One person, one cigarette~~  

Custom View (listview.xml):

<?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">

        <ListView android:id="@id/android:list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
        />

        <TextView android:id="@id/android:empty"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="No data"
                android:textColor="#ff0000"
        />
</LinearLayout>

Load Layout:

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        //No data source is set
}

 
Row Layout
        officially provides a variety of ListItem Layouts (R.layout). The following are more commonly used. For more information, please refer to R.layout in API DOC http://androidappdocs.appspot.com/reference/android/R.layout .html :

android.R.layout.simple_list_item_1 one line of text
android.R.layout.simple_list_item_2 one line of title, one line of text
android.R.layout.simple_list_item_single_choice radio button
android.R.layout.simple_list_item_multiple_choice multiple choice button
android.R.layout.simple_list_item_checkboxed

We can customize our own Layout (list_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android "
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ImageView android:id="@+id/icon"
                android:layout_width="48dip"
                android:layout_height="48dip" />

        <TextView android:id="@+id/text"
                android:layout_gravity="center_vertical"
                android:layout_width="0dip"
                android:layout_weight="1"
                android:layout_height="wrap_content" />

</LinearLayout>

When using it, just refer to it with R.layout.list_item. You can refer to http://androidappdocs.appspot.com/resources/tutorials/views/hello-listview.html .

Binding data
        can be realized by calling SetListAdapter(ListAdapter adapter). We can implements ListAdapter to customize our own data source. The API has several built-in Adapters that implements ListAdapter: BaseAdapter, SimpleAdapter (stores static data in the form of Map), SimpleCursorAdapter (used for cursor query results) and so on. Usually we extend BaseAdapter more to write our own Adapter class, because the BaseAdapter class is the base class of other Adapter classes. Extending the BaseAdapter class generally requires rewriting the following methods:

    int getCount() Get the number of Items of the current Adapter
    Object getItem(int position) Get the Item at the corresponding position
    long getItemId(int position) Get the row id of the Item at the corresponding position in the List
    View getView(int position, View convertView, ViewGroup parent) Get the View of the data to be displayed at the specified position

        For details, please refer to the method of inheriting android.widget.Adapter of the BaseAdapter class, and sometimes it is necessary to rewrite the boolean isEnabled(int position) of the ListAdapter to achieve certain effects.

        Let's look at a few examples of binding data:

1. Use ArrayAdapter

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Refer to the constructor of ArrayAdapter
        setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1,
                        mStrings));

        //Enter a letter on the ListView, and it will automatically filter out Items that start with this content
        getListView().setTextFilterEnabled(true);
}

private String[] mStrings = {"A", "Android", "机器人", "Google"};

Android UI: ListView - 奥奶- One person, one cigarette~~ Android UI: ListView - 奥奶- One person, one cigarette~~

 2. Use SimpleCursorAdapter
        This is an example of List3 in Sample, which is displayed by reading the information of android.provider.Contacts.Phones in the address book.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get a cursor with all phones
        Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null);
        startManagingCursor(c);
       
        // Map Cursor columns to views defined in simple_list_item_2.xml
        ListAdapter adapter = new SimpleCursorAdapter(this,
                        android.R.layout.simple_list_item_2, c,
                                        new String[] { Phones.NAME, Phones.NUMBER },
                                        new int[] { android.R.id.text1, android.R.id.text2 });
        setListAdapter(adapter);
}


3. ListItem is a radio button

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Refer to the constructor of ArrayAdapter
        setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_single_choice,
                        mStrings));

        final ListView listView = getListView();
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); //Set single choice mode
}

private String[] mStrings = {"A", "Android", "机器人", "Google"};

 

Android UI: ListView - 奥奶- One person, one cigarette~~ Android UI: ListView - 奥奶- One person, one cigarette~~  

4. ListItem is a multi-choice button
        . Set Example 3 to android.R.layout.simple_list_item_multiple_choice and the selection mode ListView.CHOICE_MODE_MULTIPLE.


For more examples, please refer to the official Sample, here is a list of relevant List examples:
List1 - Use ArrayAdapter and setTextFilterEnabled(true)
List2 - Use SimpleCursorAdapter to read the address book People.NAME
List3 - Use SimpleCursorAdapter to read the address book Phones, Two lines display Item
List4 - use a custom Adapter and a custom ItemView
List5 - a ListView with a separator, rewrite boolean isEnabled(int position) through a custom Adapter
List6 - use a custom Adapter and a custom ItemView, the hidden content can be stretched
List7 - Use SimpleCursorAdapter to read data
List8 - Show the effect of using setEmptyView
List9 - Involves OnScrollListener
List10 - ListItem is a radio button
List11 - ListItem is a multi-selection button
List12 - Can dynamically add ListItem
List13 - How to speed up the operation display, during scrolls or flings
List14 - How to write an efficient List Adapter
 
Among them, List14, the official told us:
To work efficiently the adapter implemented here uses two techniques:
 * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
 * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
 
还告诉了我们ViewHolder类的作用:
 * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
 * getView(). This data structures contains references to the views we want to bind data to, thus
 * avoiding calls to findViewById() every time getView() is invoked.
 
    In addition, I learned from the above example that when using a custom Adapter, you need to call notifyDataSetChanged() to refresh the ListView when the data changes, but in the example of List12, using the ArrayAdapter does not call this method, and then write the code yourself, when it happens In the exception, I learned that BaseAdapter and ArrayAdapter will call their own notifyDataSetChanged(). You can check the examples in the following article "Learning about Android Threads"!
 
The event response of ListView
    usually responds to the click event of ListItem: protected void onListItemClick(ListView l, View v, int position, long id), I won’t go into details here, just understand the meaning of the parameters in this function.

Guess you like

Origin blog.csdn.net/cz285933169/article/details/6560995