Gallery: Scrolling Sideways list
Mainactivity.java
Packagecom.sxt.day05_01;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.AdapterView.OnItemLongClickListener;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Gallery;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageView;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast;Importcom.sxt.day05_01.entity. Generalbean; Public classMainactivityextendsActivity {Gallery mgallery; List<GeneralBean> mgenerals;//represents a collection of 10 strategistsGeneraladapter Madapter; int[] resid={R.drawable.baiqi,r.drawable.caocao,r.drawable.chengjisihan, r.drawable.hanxin,r.drawable.lishimin,r.dr Awable.nuerhachi, R.drawable.sunbin,r.drawable.sunwu,r.drawable.yuefei, R.drawable.zhuyuanzhang}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData ();//Initializing DataInitview (); Setlistener (); } Private voidSetlistener () {Setonitemclicklistener (); Setonitemlongclicklistener (); } Private voidSetonitemlongclicklistener () {Mgallery.setonitemlongclicklistener (NewOnitemlongclicklistener () {@Override Public BooleanOnitemlongclick (adapterview<?>Parent, view view,intPositionLongID) {toast.maketext (mainactivity. This, Mgenerals.get (position). GetName () + "Be Long pressed", 2000). Show (); return true; } }); } Private voidSetonitemclicklistener () {Mgallery.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, view view,intPositionLongID) {toast.maketext (mainactivity. This, Mgenerals.get (position). GetName () + "Short Press", 2000). Show (); } }); } Private voidInitview () {mgallery=(Gallery) Findviewbyid (r.id.gallerygeneral); Madapter=NewGeneraladapter ();//Create an adapterMgallery.setadapter (Madapter);//Correlation Adapter } Private voidInitData () {//converts the number of string groups in a resource to a Java arrayString[] Names=getresources (). Getstringarray (R.array.generals); Mgenerals=NewArraylist<generalbean>(); for(inti = 0; i < names.length; i++) {Generalbean Bean=NewGeneralbean (Resid[i], names[i]); Mgenerals.add (Bean); } } //Adapter classGeneraladapterextendsbaseadapter{@Override Public intGetCount () {returnInteger.max_value;//set the length of the gallery to 2.1 billion so that the horizontal scrolling list goes to the end and starts with the first one .} @Override PublicGeneralbean GetItem (intposition) { returnMgenerals.get (position); } @Override Public LongGetitemid (intposition) { returnposition; } @Override//Scroll to show a single layout of position position in gallery, position from 0 to 2.1 billion, so that the horizontal scrolling list starts at the end of the first PublicView GetView (intposition, View Convertview, ViewGroup parent) { //get the layout of ListViewItem, convert to object of view typeView layout=view.inflate (mainactivity. This, R.layout.item_generals,NULL); ImageView Ivthumb=(ImageView) Layout.findviewbyid (R.ID.IVTHUMB); TextView Tvname=(TextView) Layout.findviewbyid (r.id.tvname); //position%mgenerals.size () makes the list of horizontal scrolling go to the end and starts with the first one .Generalbean Bean=mgenerals.get (position%mgenerals.size ()); Ivthumb.setimageresource (Bean.getresid ()); Tvname.settext (Bean.getname ()); returnLayout//returns a single layout of position position, that is, Item_generals.xml linearlayout } }}
Main.xml
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <GalleryAndroid:id= "@+id/gallerygeneral"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:spacing= "2DP"/></Relativelayout>
Item_generals.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" > <ImageViewAndroid:id= "@+id/ivthumb"Android:layout_width= "80DP"the word android:layout_height= "80DP"Android:scaletype= "Fitxy"android:src= "@drawable/baiqi"/> <TextViewAndroid:id= "@+id/tvname"Android:layout_width= "80DP"Android:layout_height= "Wrap_content"Android:text= "White Up"android:textsize= "20SP"android:gravity= "Center_horizontal"/></LinearLayout>
Generalbean.java
Public class Generalbean { privateint resid; The ID value of the picture is private String name; Military Strategist's name public int getresid () {return resid; }
Android Gallery: Scrolling sideways list