Here again to learn to write Android's ListView, in fact, we have used the ListView, just like the phone's contact, is the use of the ListView. The following is the implementation of the simple ListView Bar, the first is to add the relevant code in the XML file:
<?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 "> < ListView android:id= "@+id/list_view" android:layout_height= "match_parent" android:layout_width= " Match_parent "/></linearlayout>
It is then implemented in the Mainactivity code as follows:
Package Com.example.jared.uilistview;import Android.os.bundle;import Android.support.v7.app.actionbar;import Android.support.v7.app.appcompatactivity;import Android.widget.arrayadapter;import Android.widget.ListView; public class Mainactivity extends Appcompatactivity {private string[] data = {"Apple", "banana", "pear", "watermelon", "blueberry", "Grape", "orange" , "Sugar cane", "dragon fruit", "grapefruit", "Litchi", "Kiwi"}; Private ListView ListView; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); ActionBar ActionBar = Getsupportactionbar (); Actionbar.hide (); Setcontentview (R.layout.activity_main); arrayadapter<string> adapter = new Arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_1, data); ListView = (ListView) Findviewbyid (R.id.list_view); Listview.setadapter (adapter); }}
This defines the Arrayadapter adapter, which is suitable for use with a ListView and can be generic programming, where only string is used.
Run the code as follows:
Here is the simplest implementation of the text, the following in the add a picture to try.
First, create a new fruit class with the following code:
Package com.example.jared.uilistview;/** * Created by Jared on 16/2/9. */public class Fruit { private String name; private int imageId; Public Fruit (String name, int imageId) { this.name = name; This.imageid = imageId; } Public String GetName () { return name; } public int Getimageid () { return imageId; }}
The fruit class mainly provides a name and a picture, so it provides an interface to get the name and imageID.
Then create a new fruit.xml to provide ImageView and TextView, of course, you can extend the code as follows:
<?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 "> < ImageView android:id= "@+id/fruit_image" android:layout_height= "wrap_content" android:layout_width= "Wrap_content"/> <textview android:id= "@+id/fruit_name" android:layout_height= "Wrap_content" android:layout_width= "wrap_content" android:layout_gravity= "center" android:layout_marginleft= " 10dip "/></linearlayout>
To create a custom adapter Fruitadapter, the code is as follows:
Package Com.example.jared.uilistview;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.arrayadapter;import Android.widget.imageview;import android.widget.textview;import java.util.list;/** * Created by Jared on 16/2/9. */public class Fruitadapter extends arrayadapter<fruit> {private int resourceId; Public Fruitadapter (context context, int Textviewresourceid, list<fruit> objects) { Super (context, Textviewresourceid, objects); ResourceId = Textviewresourceid; Public View GetView (int position, View Convertview, ViewGroup parent) {Fruit Fruit = GetItem (position); View view = Layoutinflater.from (GetContext ()). Inflate (resourceId, NULL); ImageView fruitimage = (ImageView) View.findviewbyid (r.id.fruit_image); TextView fruitname = (TextView) View.findviewbyid (r.id.fruit_name); Fruitimage.setimageresource (Fruit.getimageid ()); Fruitname.settext (Fruit.getname ()); return view; }}
Finally, modify the Mainactivity code as follows:
Package Com.example.jared.uilistview;import Android.os.bundle;import Android.support.v7.app.actionbar;import Android.support.v7.app.appcompatactivity;import Android.widget.listview;import Java.util.ArrayList;import Java.util.list;public class Mainactivity extends Appcompatactivity {private list<fruit> fruitlist = new ArrayLis T<fruit> (); Private ListView ListView; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); ActionBar ActionBar = Getsupportactionbar (); Actionbar.hide (); Setcontentview (R.layout.activity_main); Initfruits (); Fruitadapter adapter = new Fruitadapter (Mainactivity.this, R.layout.fruit, fruitlist); ListView = (ListView) Findviewbyid (R.id.list_view); Listview.setadapter (adapter); } private void Initfruits () {Fruit Apple = new Fruit ("Apple", r.drawable.apple); Fruitlist.add (Apple); Fruit banana = new Fruit ("Banana", r.drawable. Banana); Fruitlist.add (banana); Fruit pear = new Fruit ("Pear", r.drawable.pear); Fruitlist.add (pear); Fruit watermelon = new Fruit ("Watermelon", R.drawable.watermelon); Fruitlist.add (watermelon); Fruit grape = new Fruit ("Grape", r.drawable.grape); Fruitlist.add (grape); Fruit orange = new Fruit ("Orange", r.drawable.orange); Fruitlist.add (orange); Fruit Litchi = new Fruit ("Litchi", R.drawable.litchi); Fruitlist.add (Litchi); Fruit Strawberry = new Fruit ("Strawberry", R.drawable.strawberry); Fruitlist.add (strawberry); Fruit pineapple = new Fruit ("Pineapple", r.drawable.pineapple); Fruitlist.add (pineapple); }}
The results of the operation look as follows:
Pictures and text are displayed, then how to achieve the effect of the click? That's the next lesson.
The first is to add the following code to the mainactivity:
Package Com.example.jared.uilistview;import Android.os.bundle;import Android.support.v7.app.actionbar;import Android.support.v7.app.appcompatactivity;import Android.view.view;import Android.widget.adapterview;import Android.widget.listview;import Android.widget.toast;import Java.util.arraylist;import Java.util.List;public class Mainactivity extends Appcompatactivity {private list<fruit> fruitlist = new arraylist<fruit> (); Private ListView ListView; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); ActionBar ActionBar = Getsupportactionbar (); Actionbar.hide (); Setcontentview (R.layout.activity_main); Initfruits (); Fruitadapter adapter = new Fruitadapter (Mainactivity.this, R.layout.fruit, fruitlist); ListView = (ListView) Findviewbyid (R.id.list_view); Listview.setadapter (adapter); Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () { @Override public void Onitemclick (adapterview<?> adapterview, view view, int I, long l) { Fruit Fruit = Fruitlist.get (i); Toast.maketext (Mainactivity.this, Fruit.getname (), Toast.length_short). Show (); } }); } private void Initfruits () {Fruit Apple = new Fruit ("Apple", r.drawable.apple); Fruitlist.add (Apple); Fruit banana = new Fruit ("banana", R.drawable.banana); Fruitlist.add (banana); Fruit pear = new Fruit ("Pear", r.drawable.pear); Fruitlist.add (pear); Fruit watermelon = new Fruit ("Watermelon", R.drawable.watermelon); Fruitlist.add (watermelon); Fruit grape = new Fruit ("Grape", r.drawable.grape); Fruitlist.add (grape); Fruit orange = new Fruit ("Orange", r.drawable.orange); Fruitlist.add (orange); Fruit Litchi = new Fruit ("Litchi", R.drawable.litchi); Fruitlist.add (Litchi); Fruit Strawberry = new Fruit ("Strawberry", R.drawable.strawberry); Fruitlist.add (strawberry); Fruit pineapple = new Fruit ("Pineapple", r.drawable.pineapple); Fruitlist.add (pineapple); }}
The Setonitemclicklistener method is added here, and then it is similar to the button. Run to see the effect as follows:
Viewlist is basically learning this knowledge.
Android Development Learning Path--ui's listview