The ListView is almost ubiquitous in app development, and its importance is self-evident!
XML Code:
<listview android:id= "@+id/mylistview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:entries= "@array/ctype" >
</ListView>
Common Properties:
Android:divider is used to set a separator bar for a list instance, either by color or by using drawable resources.
Android:dividerheight used to set the height of the separator bar
data Sources for the android:entries list
Android:footerdividerenabled is used to set whether a separator bar is drawn before footer View, and the default value is
True, when set to False, indicates that the property will not be drawn when using the Liswview component provided by the
The Addfooterview () method sets the footer View for the ListView
Android:headerdividersenabled is used to set whether a separator bar is drawn after the header View, and the default
A value of true, which is set to False, indicates that it will not be drawn, and that the property needs to be provided through the Liswview component.
The Addheaderview () method sets the header View for the ListView
use of the ListView and (array/simple) Adapter
1.ArrayAdapter:
The data source is an array or a collection
Constructor :new Arrayadapter (This, XML, data source );
String [] ctype={};
arrayadapter<string> adapter = new Arrayadapter<string> (This,
Android. R.layout.simple_list_item1, CType);
Arrayadapter<charsequence>
Adapter2=arrayadapter.createfromresource (This, R.array.ctype,
Android. R.LAYOUT.SIMPLE_LIST_ITEM1);
2.simpleadapter:
data source is List<map<string,?>>
constructor : New simpleadapter (this, data source, Xml,new string[]{key1,key2},new
Int[]{r.id.image,r.id.textview});
description:
①key the key for the data source Map
②string[] and int[] must be one by one corresponding
Android. R.layout.simple_list_item_multiple_choice: Each list item has multiple marquee text
Android. R.layout.simple_list_item_1 Each list item is a normal text
Android. R.layout.simple_list_item_2 Each list item is a plain text (slightly larger font size)
Android. R.layout.simple_list_item_checked each list item has a checked list item
Android. R.layout.simple_list_item_single_choice each list item is text with a radio button
Common methods of monitoring
Listview.setonitemclicklistener (New Onitemclicklistener ()
Lv.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {
String res = parent.getitematposition (position). ToString ();
Toast.maketext (Mainactivity.this, Res, toast.length_short). Show ();
}
});
The difference between Adapter and Adapterview
Adapter: Assigning a layout to a given data source
Adapterview: provides a View(listview,gridview,spinner) for Adapter
Description
How to remove the ListView scroll bar
Lv.setverticalscrollbarenabled (FALSE);
How to remove a ListView split line
Lv.setdivider (NULL);
The optimization idea of the ListView:
When a system display list (ListView) is first instantiated, an adapter is instantiated, and this article instantiates a custom
The adapter. To implement a custom adapter, you must manually map the data, then you need to override the GetView () method , and the system
This method is called when a row is in place.
when the ListView starts drawing , the system automatically calls the GetCount () function, which returns the length of the ListView according to the function,
then, based on this length, call GetView () to draw each line one at a.
Refer to the following code for specific use, just remember
Android Custom ListView Three steps:First step: Prepare the main layout file, component layout file, etc. using Layoutinfiter dynamic loading
Step two: Get and organize your data
The third part: binding data, here we write by ourselves Adapter class to do
1. Create a class that inherits BaseAdapter2. overriding method
@Override
public int GetCount () {
Returns the size of the data source
Return GetList (). Size ();
}
@Override
Public Object GetItem (int position) {
Returns an item object based on position
Return GetList (). get (position);
}
@Override
Public long Getitemid (int position) {
Returns the number of rows according to position
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
The wrong wording of wasted resources
Layoutinflater inflater = Getlayoutinflater ();
View view = Inflater.inflate (R.layout.items, NULL);
TextView TV = (TextView) View.findviewbyid (R.ID.TEXTVIEW1);
Tv.settext (GetList (). get (position));
System.out.println ("position-->" + position);
Correct wording
View view = null;
if (Convertview = = null) {
Layoutinflater inflater = Getlayoutinflater ();
View = Inflater.inflate (R.layout.items, NULL);
}else{
If Convertview is not empty, the view is loaded before, so it is directly assigned to view, which is repeated
Use to avoid creating new view waste resources
view = Convertview;
}
TextView TV = (TextView) View.findviewbyid (R.ID.TEXTVIEW1);
Tv.settext (GetList (). get (position));
System.out.println ("position-->" + position);
return view;
}
The asynctask of the ListView loads the interface of the network data-xml&json1, the city list json:
Http://127.0.0.1/DataServer/CityServlet?type=json
2. Interface of city list xml:
Http://127.0.0.1/DataServer/CityServlet?type=xml
"Extended"listactivity
Definition: An Activity that contains a ListView
Second, use:
1. You do not need to define an XML layout file
2. In the Activity's OnCreate () method, the Setcontentview () method is not required
3. Use the same mode as the ListView, note that when setting Adapter, the Setlistadapter method is used
For example: Setlistadapter (adapter);
4. Add a Click event
① Setting up listeners
Getlistview.setonitemclicklistener () ...
② using callback methods
Onlistitemclick ()
public class Mainactivity extends Listactivity {
@Override
Protectedvoid onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
To create an adapter for a ListView specified list item
string[] CType = new string[] {"Story mode", "Theme Mode", "Phone", "program
Management "};
arrayadapter<string> adapter = new Arrayadapter<string> (This, Andro
Id. R.layout.simple_list_item_1, CType);
Setlistadapter (adapter);
}
}
"Extended"Expandablelistview
Function: is a level two list, divided into group and child,group to represent a level list, child represents a level two list
Unlike a ListView, it is a two-level scrolling list view, and each group can be expanded to show some children,
These items come as far as Expandablelistadapter subclasses, that is, to implement adding items to the inside, you must write a
Subclasses implement the Expandablelistadapter interface or use the system for us to implement the sub-class
Baseexpandablelistadapter,
Simpleexpandablelistadapter
Adapter
Data
Private list<string> groupdata;//Defining group data
Private list<list<string>> childrendata;//Define child data in a group
Context
Private context context;
Layout file Loader
Private Layoutinflater Inflater;
/**
* Constructor
*
* @param groupdata
* @param childrendata
* @param context
*/
Public Myexpandablelistadapter (list<string> Groupdata, List<list<strin
G>> Childrendata, Context context) {
Super ();
This.groupdata = Groupdata;
This.childrendata = Childrendata;
This.context = context;
This.inflater = Layoutinflater.from (context);
}
/**
* Get the number of groups
*/
@Override
Publicint GetGroupCount () {
Returngroupdata! = null? Groupdata.size (): 0;
}
/**
* Get the number of sub-categories under which group
*/
@Override
public int Getchildrencount (intgroupposition) {
Returnchildrendata.get (groupposition)! = null? Childrendata.get (GR
oupposition). Size (): 0;
}
/**
* Get Group
*/
@Override
Public Object Getgroup (intgroupposition) {
Returnthis.groupData.get (groupposition);
}
/**
* Get the following subkey in the group
*/
@Override
Public Object Getchild (intgroupposition, intchildposition) {
Returnthis.childrenData.get (groupposition). get (Childposition);
}
/**
* Get Group position
*/
@Override
Publiclong Getgroupid (intgroupposition) {
Returngroupposition;
}
/**
* Sub-position below group
*/
@Override
Publiclong Getchildid (intgroupposition, intchildposition) {
Returnchildposition;
}
@Override
Publicboolean Hasstableids () {
Returnfalse;
}
@Override
Public View Getgroupview (intgroupposition, booleanisexpanded, view Conv
Ertview, ViewGroup parent) {
TextView TV = null;
if (Convertview = = null) {
TV = (TextView) inflater.inflate (Android. R.layout.simple_list_i
Tem_1,null);
Convertview = TV;
} else {
TV = (TextView) Convertview;
}
Setting up data
Tv.settext ("+groupdata.get (groupposition)");
Returntv;
}
@Override
Publicview Getchildview (intgroupposition, Intchildposition, Booleanisla
Stchild, View Convertview,
ViewGroup parent) {
TextView TV = null;
if (Convertview = = null) {
TV = (TextView) inflater.inflate (Android. R.layout.simple_list_i
Tem_1,null);
Convertview = TV;
} else {
TV = (TextView) Convertview;
}
Setting up data
Tv.settext ("" +childrendata.get (groupposition). Get (Childposit
ION));
return TV;
}
/**
*//child can be clicked, True can point, false can not point
*/
@Override
Publicboolean ischildselectable (intgroupposition, intchildposition) {
return true;
}
Listener
Onchildclicklistener: Clicking Child triggers the Listener
Exlistview.setonchildclicklistener (New Onchildclicklistener () {
@Override
public Boolean Onchildclick (expandablelistview parent, View V,
int groupposition, int childposition, long id) {
Toast.maketext (Getapplicationcontext ()
, Child.get (groupposition). Get (Childposition) + "", Toast.length_short). Show ();
return false;
}
});
When using the ListView paging, it is inappropriate to download and display all the data on the server side, we need to
The data is paginated, and when the user pulls down to the bottom, the next page of content is updated
1. Server side:
Paging data, differentiating data by page numbers, such as
First page
Second page
2. Client
(1) Idea: The client first needs to load the first page content, when the user drop down to the bottom of the ListView to update the next page of content,
and displays
(2) Steps:
① First downloads the first page of content through an asynchronous task and updates to the ListView
② the user down to the bottom of the ListView, you need to use Onscrolllistener
③ down to the bottom to display the loaded progress bar, the data is loaded to hide
Lv.setonscrolllistener (New Onscrolllistener () {
/**
* Back to the user swipe the status of the phone screen, specifically divided into three kinds of scroll_state_idle:0, users stop sliding, that is, let go
* Scroll_state_touch_scroll:1, the user is sliding, the finger has been touching the screen
* Scroll_state_fling:2, the user is sliding and accompanying the inertial movement
*/
@Override
public void onscrollstatechanged (Abslistview view, int scrollstate) {
SYSTEM.OUT.PRINTLN ("------" + scrollstate);
if (isbottom && scrollstate = = Onscrolllistener.scroll_state_idle) {
Toast.maketext (Getapplicationcontext (), "Downloading data ... ", Toast.length_short). Show ();
Foodview.setvisibility (view.visible);
New Myasynctask (). Execute ();
}
}
/**
* Firstvisibleitem: Returns the index of items that have been seen but are now moved out of the screen
* VisibleItemCount: Displays the index of the item displayed on the current screen Totalitemcount: Returns the total number of all items
* Note: when Firstvisibleitem + VisibleItemCount
* When =totalitemcount, the description has been slid to the bottom of the screen
*/
@Override
public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount)
{
Isbottom = (Firstvisibleitem + visibleitemcount = = Totalitemcount);
}});
Listview+checkbox: In the Click event of the Android system, the Item's Click event Priority in the ListView is higher than
CheckBox's Click event, Onitemclicklistener will expire when a checkbox is added to the layout file
Second, the solution:
1, use a CheckBox to get the focus
<checkbox
Android:id= "@+id/checkbox"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:clickable= "false"-------here
Android:focusable= "false"-------here
Android:focusableintouchmode= "false"/>
2, define a collection to store which ones are selected, which are not selected
Used to control the check status of a CheckBox
Private Hashmap<integer, boolean> isSelected;
Simple use of Bitmap and Bitmapfactory and Bitmap class
The Bitmap class represents a bitmap, which is one of the most important classes of image processing in an Android system and uses it not only to
Get image file information, make image cut, rotate, zoom and so on, and can also specify format to save image file.
Common methods of Bitmap class
Second, Bitmapfactory class
The class is a tool class that is used to parse from different data sources and create BitMap objects.
Common methods are as follows:
Method description
DecodeFile (String pathName) is used to parse, create from a file specified by a given path.
Building Bitmap Objects
The Decoderesource (resources res, int ID) is used to extract from the specified resource based on the given resource ID
Analyze, create Bitmap objects
Decodestream (InputStream is) for parsing, creating Bitmap from the specified input stream
Object
Iii. How to parse a picture document from an SD card string path= "/sdcard/picture/bccd/img01.jpg"
Bitmap bm=bitmapfactory.decodefile (path);
Parse a picture from a resource file
Bitmap Bm=bitmapfacotry.decoderesource (MainActivity.this.getResources (), r.drawable.img02);
asynchronous loading picture and text of the ListView in the actual development, for the ListView will display pictures, title, time, etc., such as NetEase news client
Steps
1. Create a custom layout file
2, request network JSON
3, loading data
4, for each piece of data and then start a thread to download the picture
use of the GridView1. Definition: Display two-dimensional, grid-like view and ListView belong to the same level, inheriting sub Abslistview
XML syntax
<gridview
Android:id= "@+id/gridview1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:stretchmode= "ColumnWidth"
android:numcolumns= "4" >
</GridView>
Common Properties
①android:columnwidth: Column width
②android:gravity: Alignment, generally center
③android:horizontalspacing: Horizontal blank, spacing between columns
④android:verticalspacing: Vertical white space, spacing between lines
⑤android:numcolumns: Number of columns, Auto_fit: Adaptive, which determines the number of columns depending on the width of the screen
⑥android:stretchmode: Stretch mode with the following properties:
Stretch_column_width: Stretched according to column widths, used with gravity to center the view
Stretch_spacing: Stretch According to white space, use with gravity to align the view left
Stretch_spacing_uniform: Uniform blank stretch, used with gravity to align view right
data load to use adapter
1,arrayadapter
2,simpleadapter
3, Custom Adapter
Common events
Onitemclicklistener
In fact, the GridView and the ListView except the display way, the others are the same
ScrollViewScrolling view is used to add scroll bars to other components, and, by default, when the contents of a form are more than one screen
When the display does not appear, the excess part cannot be seen by the user. Because the Android layout manager itself does not provide a scrolling screen
The function of the screen. If you want to scroll, use the scrolling view Scrllview.
Scrolling view is a subclass of Framelayout, so in a scrolling view, you can add any component that you want to put in, but
is a scrolling view can only put one component, if you want to place more than one, you can first put a storage layout manager. And then we're going to put
Placed components into the layout manager, in the scrolling view, use more of the linear layout manager.
XML Syntax:
<scrollview
Android:id= "@+id/myscollview"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
Put the components, only one OH
</ScrollView>
Common Properties
Android:scrollbars Sets the scroll bar display. None (hidden), horizontal (horizontal), vertical (vertical).
Android:scrollbarfadeduration set the scrollbar fade effect (from having to slowly fade to vanishing) time,
in milliseconds. Android2.2 scroll bar will disappear after scrolling, then scroll again, in version 1.5, 1.6
The inside will always show.
Android:scrollbarsize sets the width of the scroll bar.
Android:scrollbarstyle sets the style and position of the scroll bar. Setting values: Insideoverlay, Insideinset,
Outsideoverlay, Outsideinset
Android:scrollbarthumbhorizontal sets the drawable of the horizontal scroll bar.
Android:scrollbarthumbvertical Setting the drawable of the vertical scroll bar
Android:scrollbartrackhorizontal sets the color drawable of the horizontal scrollbar background (track)
Android:soundeffectsenabled Set the sound effect when tapping or touching
Horizontalscrollview horizontal scrolling View Horizontalscrollview such as the title of NetEase News
XML syntax
Android:id= "@+id/scrollview1"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >
<textview
Android:id= "@+id/textview1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "TextView"/>
</HorizontalScrollView>
Android 017ListView & GridView & ScrollView