Android Basics Getting Started tutorial--2.4.2 ListView Simple and practical
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
We've finished writing the chapter on the Android Web-based programming, and we're going to continue to learn about the UI Control section that's not finished.
Recalling the previous section, we introduced the concept of the adapter adapter and then learned the use of the three simplest adapters:
Arrayadapter,simpleadapter and Simplecursoradapter, and this section explains the first
The UI controls to be used with adapter: ListView, but replaced by Recyclerview this new control in the version!
List as one of the most commonly used controls, or it is necessary to study hard, this section with a beginner's perspective to learn
Listview,listview properties, as well as baseadapter simple definitions, as for the ListView to optimize these,
We take a step by step ~ Mo Urgent!
1. Customize the Baseadapter and then bind the ListView to the simplest example
Let's look at what we're going to achieve:
A very simple listview, write the item yourself, then load the point data so ~
Paste the key code below:
Animal.java:
/** * Created by Jay on 2015/9/18 0018. * * Public class Animal { PrivateString AName;PrivateString Aspeak;Private intAIcon; Public Animal() { } Public Animal(String aName, String aspeak,intAIcon) { This. aName = AName; This. aspeak = Aspeak; This. AIcon = AIcon; } PublicStringGetaname() {returnAName; } PublicStringGetaspeak() {returnAspeak; } Public int Getaicon() {returnAIcon; } Public void Setaname(String aName) { This. aName = AName; } Public void Setaspeak(String aspeak) { This. aspeak = Aspeak; } Public void Setaicon(intAIcon) { This. AIcon = AIcon; }}
Animaladapter.java: Custom baseadapter:
/** * Created by Jay on 2015/9/18 0018. * * Public class animaladapter extends baseadapter { PrivateLinkedlist<animal> Mdata;PrivateContext Mcontext; Public Animaladapter(linkedlist<animal> mdata, Context mcontext) { This. Mdata = Mdata; This. Mcontext = Mcontext; }@Override Public int GetCount() {returnMdata.size (); }@Override PublicObjectGetItem(intPosition) {return NULL; }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Convertview = Layoutinflater.from (Mcontext). Inflate (r.layout.item_l Ist_animal,parent,false); ImageView Img_icon = (ImageView) Convertview.findviewbyid (R.id.img_icon); TextView txt_aname = (TextView) Convertview.findviewbyid (r.id.txt_aname); TextView txt_aspeak = (TextView) Convertview.findviewbyid (r.id.txt_aspeak); Img_icon.setbackgroundresource (Mdata.get (position). Getaicon ()); Txt_aname.settext (Mdata.get (position). Getaname ()); Txt_aspeak.settext (Mdata.get (position). Getaspeak ());returnConvertview; }}
Finally, the Mainactivity.java:
Public class mainactivity extends appcompatactivity { PrivateList<animal> Mdata =NULL;PrivateContext Mcontext;PrivateAnimaladapter Madapter =NULL;PrivateListView List_animal;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Mcontext = mainactivity. This; List_animal = (ListView) Findviewbyid (r.id.list_animal); Mdata =NewLinkedlist<animal> (); Mdata.add (NewAnimal ("The dog said.","Are you a dog?", R.mipmap.ic_icon_dog)); Mdata.add (NewAnimal ("The bull says.","Are you a cow?", R.mipmap.ic_icon_cow)); Mdata.add (NewAnimal ("Duck says","Are you a duck?", R.mipmap.ic_icon_duck)); Mdata.add (NewAnimal ("Fish says.","Are you a fish?", R.mipmap.ic_icon_fish)); Mdata.add (NewAnimal ("said the horse.","Are you a horse?", R.mipmap.ic_icon_horse)); Madapter =NewAnimaladapter ((linkedlist<animal>) mdata, Mcontext); List_animal.setadapter (Madapter); }}
Well, customizing Baseadapter and completing data binding is as simple as that.
Don't ask me to take the code of the example, just start learning will write this code, I just demonstrate the next process, so that everyone familiar with
Familiarity ~ Also, prepare for the following attribute validation ~
2. Table header footer Split line settings:
ListView as a list control, like a regular list, can set the header and footer of the table himself:
As well as the split line, the following properties are available for us to set:
- footerdividersenabled: Whether to draw a separator bar before Footerview (footer), which is true by default
- headerdividersenabled: Whether to draw a separator bar before Headerview (footer), which is true by default
- Divider: Set the Separator bar, you can use color segmentation, you can also use drawable resource segmentation
- dividerheight: Sets the height of the separator bar
Go through the API discovery does not have the ability to directly set the ListView header or footer properties, only write code in Java
is set up so that we can invoke the following methods:
- Addheaderview (View v): Add Headview (header), the argument in parentheses is a View object
- Addfooterview (View v): Add Footerview (footer), the argument in parentheses is a View object
- Addheaderview (Headview, NULL, FALSE): The difference from the previous: sets whether the header can be selected
- Addfooterview (view,view,false): Ibid.
By the way, use this Addheaderview method must be placed in front of listview.setadapter, otherwise will be error.
Examples of Use :
Run :
Code Implementation :
First write the following table header and footer layout:
View_header.xml (table header), like the end of the table, will not be affixed:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Match_parent"android:layout_height="Match_parent" Android:orientation="vertical"android:gravity="center"> <TextViewandroid:layout_width="Match_parent"android:layout_height= "48DP" android:textsize="18sp"android:text= "table header"android:gravity=" Center "android:background=" #43BBEB "android:textcolor="#FFFFFF " /> </linearlayout>
Mainactivty.java:
Public class mainactivity extends appcompatactivity implements Adapterview. Onitemclicklistener { PrivateList<animal> Mdata =NULL;PrivateContext Mcontext;PrivateAnimaladapter Madapter =NULL;PrivateListView List_animal;PrivateLinearLayout ly_content;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Mcontext = mainactivity. This; List_animal = (ListView) Findviewbyid (r.id.list_animal);//dynamic load top view and bottom view FinalLayoutinflater Inflater = Layoutinflater.from ( This); View Headview = inflater.inflate (R.layout.view_header,NULL,false); View Footview = inflater.inflate (R.layout.view_footer,NULL,false); Mdata =NewLinkedlist<animal> (); Mdata.add (NewAnimal ("The dog said.","Are you a dog?", R.mipmap.ic_icon_dog)); Mdata.add (NewAnimal ("The bull says.","Are you a cow?", R.mipmap.ic_icon_cow)); Mdata.add (NewAnimal ("Duck says","Are you a duck?", R.mipmap.ic_icon_duck)); Mdata.add (NewAnimal ("Fish says.","Are you a fish?", R.mipmap.ic_icon_fish)); Mdata.add (NewAnimal ("said the horse.","Are you a horse?", R.mipmap.ic_icon_horse)); Madapter =NewAnimaladapter ((linkedlist<animal>) mdata, Mcontext);//Add header and footer need to be written before Setadapter method call!!! List_animal.addheaderview (Headview); List_animal.addfooterview (Footview); List_animal.setadapter (Madapter); List_animal.setonitemclicklistener ( This); }@Override Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Toast.maketext (Mcontext,"You clicked on the first"+ Position +"Item", Toast.length_short). Show (); }}
OK, the code is still relatively simple, from the above we can see a point to pay attention to, that is:
After adding the footer of the header, we find that Positon is calculated from the table header, that is, the first data you added is the postion
0, but this time has become 0, because the table head is counted!!
3. The list starts at the bottom: stackfrombottom
If you want the list to show the bottom of your list, then you can use this property to Stackfrombottom
property is set to true, after setting the following:
4. Set the click Color Cachecolorhint
If you set a picture for the ListView as a background, when you drag or click the ListView blank position you will find
The item turns black, and it's time for us to set the color to transparent with this cachecolorhint : #00000000
5. Hide Slide Bar
We can set by: android:scrollbars= "None" or setverticalscrollbarenabled (true);
Solve this problem!
This section summarizes:
Well, about the basic usage of the ListView is probably that, but there are other things besides the above attributes,
Actually encountered again check it ~ here know how to rewrite Baseadapter and complete data binding just fine, next we come
Teach you how to optimize the writing of this baseadapter ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--2.4.2 ListView Simple and practical