Android UI 학습 - ListView (android. R. layot. simple list item 1 은 무엇 입 니까)
17645 단어 android ui
Android UI - ListView
2010-06-20 18:21:35
:Android UI ListView ListActivity
, , 、 。 。http://android.blog.51cto.com/268543/336162
ListActivity
ListActivity ListView Activity , ListView , , 。
custom view for screen layout
ListActivity ListView , custom view, onCreate() setContentView(resources id)。
, Layout , ListView id "@android:id/list"; Java android.R.id.list。
, id android:empty TextView, ListView data , "No data"。
nodata
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>
Layout:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// data source
}
Row Layout
ListItem Layout (R.layout), , API DOC R.layout http://androidappdocs.appspot.com/reference/android/R.layout.html:
◾android.R.layout.simple_list_item_1 text
◾android.R.layout.simple_list_item_2 title, text
◾android.R.layout.simple_list_item_single_choice
◾android.R.layout.simple_list_item_multiple_choice
◾android.R.layout.simple_list_item_checked checkbox
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>
, R.layout.list_item 。 http://androidappdocs.appspot.com/resources/tutorials/views/hello-listview.html。
SetListAdapter(ListAdapter adapter) 。 implements ListAdapter 。API implements ListAdapter Adapter:BaseAdapter,SimpleAdapter ( Map ),SimpleCursorAdapter ( ) 。 extends BaseAdapter Adapter , BaseAdapter Apdater 。 BaseAdapter :
int getCount() Adapter Items
Object getItem(int position) position Item
long getItemId(int position) position Item List row id
View getView(int position, View convertView, ViewGroup parent) position data View
BaseAdapter android.widget.Adapter , ListAdapter boolean isEnabled(int position) 。
:
1. ArrayAdapter
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ArrayAdapter
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
mStrings));
// ListView , Item
getListView().setTextFilterEnabled(true);
}
private String[] mStrings = {"A", "Android", " ", "Google"};
ArrayAdapter ArrayAdapter
2. SimpleCursorAdapter
Sample List3 , android.provider.Contacts.Phones , 。
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
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 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); //
}
private String[] mStrings = {"A", "Android", " ", "Google"};
singliechoice singliechoice
4. ListItem
3 android.R.layout.simple_list_item_multiple_choice ListView.CHOICE_MODE_MULTIPLE。
Sample, List :
List1 - ArrayAdapter setTextFilterEnabled(true)
List2 - SimpleCursorAdapter People.NAME
List3 - SimpleCursorAdapter Phones, Item
List4 - Adapter ItemView
List5 - separator ListView, Adapter, boolean isEnabled(int position)
List6 - Adapter ItemView,
List7 - SimpleCursorAdapter
List8 - setEmptyView
List9 - OnScrollListener
List10 - ListItem
List11 - ListItem
List12 - ListItem
List13 - ,during scrolls or flings
List14 - List Adapter
List14, :
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.
, Adapter, notifyDataSetChanged() ListView, List12 , ArrayAdapter , , BaseAdapter,ArrayAdapter notifyDataSetChanged()。 《 Android 》 !
ListView
ListItem :protected void onListItemClick(ListView l, View v, int position, long id) , , 。
“ Android” , http://android.blog.51cto.com/268543/336162