List class components
(1) ListView component: Lists items that need to be displayed in a vertical list
Related properties:
A. Android:divider used to set the separator bar for a list view, you can use color or picture resources
B. Android:dividerheight setting the height of the separator bar
C. android:entries specifying list items for a ListView through array resources
D. android:footerdividersenabled sets whether the separator bar is drawn before Footerview, which defaults to true.
E. android:headerdividersenabled sets whether the separator bar is drawn after Headerview, which defaults to true.
(2) ListView Use steps:
A. Drag into a ListView control and change the height to match_parent
B. Create a new layout XML file that sets the style for each line display
C. Create a class myadapter inherit Baseadapter
GetView () Method: Sets the display of each row of data and the display style of each row
GetCount () Method: Displays the number of rows, when the return value is 0 o'clock, the GetView () method is not called, and is displayed as blank
D. Creating an Adapter object
Myadapter adapter = new Myadapter ();
E. Setting up an adapter for a ListView
Listview.setadapter (adapter);
(3) Optimization of the ListView
Swipe up and down will constantly trigger the GetView () method
Workaround: Reuse row layouts to reduce control lookups
Create a class that declares several variables (the variable type and quantity are consistent with the controls contained in the layout XML file)
Class viewholder{
View View1;
View View2;
......
}
Declaring member variables
View inflate;
Viewholder Holder;
Optimization
if (convertview==null) {
Create a Viewholder object
Get the Layout Converter object
Layoutinflater inflater = Getlayoutinflate ();
To convert a layout XML file to a layout object
Inflate = Inflater.inflate (r.layout.listitem,null);
Find controls
Holder.view1 = Inflate.findviewbyid (r.id.view1);
Holder.view2 = Inflate.findviewbyid (R.ID.VIEW2);
Set a label
Infalte.settag (holder);
}else{
Replace, re-use row layouts
Inflate=convertview;
Holder = (viewholder) inflate.gettag ();
}
To set the contents of a control based on control type
......
(3) Data update
When data changes, the adapter notifies the ListView to update the data
Adapter.notifydatasetchanged ();
(4) Click events for ListView
A. Setting up tap-listening
Listview.setonitemclicklistener (new Onitemclicklistener) {
@Override
public void Onitemclick (adapterview<?> parent,view view,int position,long ID) {
......
}
}
Note: When a control with a preemption focus exists in the row layout (such as a button), the click of the ListView is invalidated
How to resolve:
Set properties on the control that preempted the focus in the row layout: Android:focusable = "false"
Set in row layout so that all the controls that can preempt the focus do not preempt the focus
android:descendantfocusability = "Blocksdescendants"
B. Set long press Tap to listen
Listview.setonitemlongclicklistener (new Onitemlongclicklistener) {
@Override
public void Onitemlongclick (adapterview<?> parent,view view,int position,long ID) {
......
}
}