Android uses gallery to create an infinite loop menu

Source: Internet
Author: User

Android uses gallery to create an infinite loop menu

1. Create a common class to inherit Gallery, in order to remove the inertia of gallery Rolling

package cn.com.y2m;   import android.content.Context;   import android.util.AttributeSet;   import android.view.MotionEvent;   import android.widget.Gallery;   public class MyGallery extends Gallery {  public MyGallery(Context context) {   super(context);  }  public MyGallery(Context context, AttributeSet attrs, int defStyle) {   super(context, attrs, defStyle);  }  public MyGallery(Context context, AttributeSet attrs) {   super(context, attrs);  }  @Override  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,   float velocityY) {   return true;  }  }

Ii. layout file writing. Labels use classes inherited from gallery

   cn.com.y2m.MyGallery  android:id="@+id/gallery_view"  android:layout_width="fill_parent"  android:layout_height="wrap_content"   /

3. Writing sub-layout files

   ImageView android:id="@+id/ItemImage"    android:layout_width="40px"   android:layout_height="30px"   android:layout_marginLeft="25px"   android:layout_marginTop="6px"   /   TextView   android:id="@+id/ItemText"   android:layout_width="89px"   android:layout_height="20px"    android:gravity="center"   android:textColor="#ffffff"   android:layout_marginBottom="4px"   /

4. code writing. Like listview, I like to inherit baseadapter for extended filling.

// Set the class myadapter extends baseadapter {private layoutinflater minflater; Public myadapter (context) {This. minflater = layoutinflater. from (context) ;}@ override public int getcount () {return integer. max_value ;}@ override public object getitem (INT arg0) {return arg0 ;}@ override public long getitemid (INT position) {return position ;}@ override public view getview (INT position, view convertview, viewgroup parent) {viewholder holder = NULL; If (convertview = NULL) {holder = new viewholder (); convertview = minflater. inflate (R. layout. footer_item, null); holder. image = (imageview) convertview. findviewbyid (R. id. itemimage); holder.txt = (textview) convertview. findviewbyid (R. id. itemtext); convertview. settag (holder);} else {holder = (viewholder) convertview. gettag ();} holder. image. setimageresource (menuitems. get (listmenu. get (position % listmenu. size (); holder.txt. settext (listmenu. get (position % listmenu. size (); If (ARG = position) convertview. setbackgroundresource (R. drawable. main_tab_frame_tabspec_background_current); Return convertview;} class viewholder {public imageview image; Public textview txt;} // fill in the data to the gallery = (Gallery) findviewbyid (R. id. gallery_view); // The default selected item Arg = integer. max_value/2 + 1; myadapter MA = new myadapter (this); Gallery. setadapter (MA); Gallery. setselection (ARG); Gallery. setonitemclicklistener (New adapterview. onitemclicklistener () {@ override public void onitemclick (adapterview arg0, view arg1, int arg2, long arg3) {toast. maketext (this, "you clicked" + (I + 1) + "No. Menu", 1 );}});

Reprint Source: http://blog.tianya.cn/blogger/post_read.asp? Blogid = 3624254 & postid = 33569888

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.