¡¤ÄúÏÖÔÚµÄλÖ㺠ÔÆÒíÍøÂç >> ÎÄÕÂÖÐÐÄ >> ÍøÕ¾½¨Éè >> appÈí¼þ¿ª·¢ >> Android¿ª·¢ >> Adapterģʽʵս֮Öع¹ºèÑó¼¯ÍŵÄAndroidÔ²Ðβ˵¥½¨ÐÐ
¶ÔÓںܶ࿪·¢ÈËÔ±À´Ëµ£¬ìÅ¿áµÄUIЧ¹ûÊÇ×îÎüÒýËûÃÇ×¢ÒâÁ¦µÄ£¬ºÜ¶àÈËÒ²ÒòΪÕâЩìÅ¿áµÄЧ¹û¶øȥѧϰһЩ±È½ÏÖªÃûµÄUI¿â¡£¶ø×ö³öìÅ¿áЧ¹ûµÄÇ°ÌáÊÇÄã±ØÐë¶Ô×Ô¶¨ÒåViewÓÐËùÀí½â£¬×÷Ϊ90µÄСÃñ×ÔȻҲ²»ÀýÍâ¡£Ìرð¶ÔÓÚ¸Õ´¦ÔÚ¿ª·¢³õÆÚµÄСÃñ£¬¶ÔÓÚ×Ô¶¨ÒåViewÕâ¼þʾõµÃÓÖÉñÃØÓÖ˧Æø£¬ÓÚÊÇСÃñ¾ö¶¨ÉîÈëÑо¿×Ô¶¨ÒåViewÒÔ¼°Ïà¹ØµÄ֪ʶµã¡£
ÔÚ´Ë֮ǰÎÒÃÇÏÈÀ´¿´¿´ÑóÉñµÄÔ°æЧ¹ûͼ:
¼ÇµÃÄÇÊÇ2014ÄêµÄµÚÒ»³¡Ñ©£¬±ÈÒÔÍùʱºòÀ´µÃÉÔÍíһЩ¡£Ð¡ÃñµÄͬÊÂÑóÊåÊÇһλ×ÊÉîµÄÑз¢ÈËÔ±£¬Éó¤Ð´UIÌØЧ£¬ÔÚ¿ª·¢ÁìÓòÖªÃû¶ÈÆĸߡ£×î½üÑóÊå¸Õ·¢²¼ÁËÒ»¸öЧ¹û²»´íµÄÔ²Ðβ˵¥£¬Õâ¸ö²Ëµ¥µÄÿ¸öItem»·ÐÎÅŲ¼£¬²¢ÇÒ¿ÉÒÔת¶¯¡£Ð¡Ãñ¾ö¶¨·ÂÕÕÑóÊåµÄЧ¹ûʵÏÖÒ»±é£¬µ«ÊǶÔÓÚСÃñÕâ¸ö½×¶ÎÀ´ËµÖ»ÒªÊµÏÖ»·Ðβ¼¾Ö¾Í²»´íÁË£¬×ª¶¯²¿·Ö×÷Ϊϸö°æ±¾¹¦ÄÜ£¬¾Íµ±×÷×Ô¶¨ÒåViewµÄÁ·Ï°ÁË¡£
ÔÚgoogleÁË×Ô¶¨ÒåViewÏà¹ØµÄ֪ʶµãÖ®ºó£¬Ð¡Ãñ¾ÍдºÃÁËÕâ¸öÔ²Ðβ˵¥²¼¾ÖÊÓͼ£¬ÎÒÃÇÒ»²½Ò»²½À´½²½â£¬´úÂëÈçÏÂ:
// Ô²Ðβ˵¥ public class CircleMenuLayout extends ViewGroup { // Ô²ÐÎÖ±¾¶ private int mRadius; // ¸ÃÈÝÆ÷ÄÚchild itemµÄĬÈϳߴç private static final float RADIO_DEFAULT_CHILD_DIMENSION = 1 / 4f; // ¸ÃÈÝÆ÷µÄÄڱ߾à,ÎÞÊÓpaddingÊôÐÔ£¬ÈçÐè±ß¾àÇëÓøñäÁ¿ private static final float RADIO_PADDING_LAYOUT = 1 / 12f; // ¸ÃÈÝÆ÷µÄÄڱ߾à,ÎÞÊÓpaddingÊôÐÔ£¬ÈçÐè±ß¾àÇëÓøñäÁ¿ private float mPadding; // ²¼¾ÖʱµÄ¿ªÊ¼½Ç¶È private double mStartAngle = 0; // ²Ëµ¥ÏîµÄÎı¾ private String[] mItemTexts; // ²Ëµ¥ÏîµÄͼ±ê private int[] mItemImgs; // ²Ëµ¥µÄ¸öÊý private int mMenuItemCount; // ²Ëµ¥²¼¾Ö×ÊÔ´id private int mMenuItemLayoutId = R.layout.circle_menu_item; // MenuItemµÄµã»÷ʼþ½Ó¿Ú private OnItemClickListener mOnMenuItemClickListener; public CircleMenuLayout(Context context, AttributeSet attrs) { super(context, attrs); // ÎÞÊÓpadding setPadding(0, 0, 0, 0); } // ÉèÖò˵¥ÌõÄ¿µÄͼ±êºÍÎı¾ public void setMenuItemIconsAndTexts(int[] images, String[] texts) { if (images == null && texts == null) { throw new IllegalArgumentException("²Ëµ¥ÏîÎı¾ºÍͼƬÖÁÉÙÉèÖÃÆäÒ»"); } mItemImgs = images; mItemTexts = texts; // ³õʼ»¯mMenuCount mMenuItemCount = images == null ? texts.length : images.length; if (images != null && texts != null) { mMenuItemCount = Math.min(images.length, texts.length); } // ¹¹½¨²Ëµ¥Ïî buildMenuItems(); } // ¹¹½¨²Ëµ¥Ïî private void buildMenuItems() { // ¸ù¾ÝÓû§ÉèÖõIJÎÊý£¬³õʼ»¯menu item for (int i = 0; i < mMenuItemCount; i++) { View itemView = inflateMenuView(i); // ³õʼ»¯²Ëµ¥Ïî initMenuItem(itemView, i); // Ìí¼Óviewµ½ÈÝÆ÷ÖÐ addView(itemView); } } private View inflateMenuView(final int childIndex) { LayoutInflater mInflater = LayoutInflater.from(getContext()); View itemView = mInflater.inflate(mMenuItemLayoutId, this, false); itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnMenuItemClickListener != null) { mOnMenuItemClickListener.onClick(v, childIndex); } } }); return itemView; } private void initMenuItem(View itemView, int childIndex) { ImageView iv = (ImageView) itemView .findViewById(R.id.id_circle_menu_item_image); TextView tv = (TextView) itemView .findViewById(R.id.id_circle_menu_item_text); iv.setVisibility(View.VISIBLE); iv.setImageResource(mItemImgs[childIndex]); tv.setVisibility(View.VISIBLE); tv.setText(mItemTexts[childIndex]); } // ÉèÖÃMenuItemµÄ²¼¾ÖÎļþ£¬±ØÐëÔÚsetMenuItemIconsAndTexts֮ǰµ÷Óà public void setMenuItemLayoutId(int mMenuItemLayoutId) { this.mMenuItemLayoutId = mMenuItemLayoutId; } // ÉèÖÃMenuItemµÄµã»÷ʼþ½Ó¿Ú public void setOnItemClickListener(OnItemClickListener listener) { this.mOnMenuItemClickListener = listener; } // ´úÂëÊ¡ÂÔ }
СÃñµÄ˼·´óÖÂÊÇÕâÑùµÄ£¬Ê×ÏÈÈÃÓû§Í¨¹ýsetMenuItemIconsAndTextsº¯Êý½«²Ëµ¥ÏîµÄͼ±êºÍÎı¾´«µÝ½øÀ´£¬¸ù¾ÝÕâЩͼ±êºÍÎı¾¹¹½¨²Ëµ¥Ï²Ëµ¥ÏîµÄ²¼¾ÖÊÓͼÓÉmMenuItemLayoutId´æ´¢ÆðÀ´£¬Õâ¸ömMenuItemLayoutIdĬÈÏΪcircle_menu_item.xml£¬Õâ¸öxml²¼¾ÖΪһ¸öImageViewÏÔʾÔÚÒ»¸öÎı¾¿Ø¼þµÄÉÏÃ档ΪÁ˲˵¥ÏîµÄ¿É¶¨ÖÆÐÍ£¬Ð¡Ãñ»¹Ìí¼ÓÁËÒ»¸ösetMenuItemLayoutIdº¯ÊýÈÃÓû§¿ÉÒÔÉèÖò˵¥ÏîµÄ²¼¾Ö£¬Ï£ÍûÓû§¿ÉÒÔ¶¨ÖƸ÷ÖÖ¸÷ÑùµÄ²Ëµ¥Ñùʽ¡£ÔÚÓû§ÉèÖÃÁ˲˵¥ÏîµÄÏà¹ØÊý¾ÝÖ®ºó£¬Ð¡Ãñ»á¸ù¾ÝÓû§ÉèÖýøÀ´µÄͼ±êºÍÎı¾ÊýÁ¿À´¹¹½¨¡¢³õʼ»¯ÏàµÈÊýÁ¿µÄ²Ëµ¥Ï²¢ÇÒ½«ÕâЩ²Ëµ¥ÏîÌí¼Óµ½Ô²Ðβ˵¥CircleMenuLayoutÖС£È»ºóÌí¼ÓÁËÒ»¸ö¿ÉÒÔÉèÖÃÓû§µã»÷²Ëµ¥ÏîµÄ´¦Àí½Ó¿ÚµÄsetOnItemClickListenerº¯Êý£¬Ê¹µÃ²Ëµ¥µÄµã»÷ʼþ¿ÉÒÔ±»Óû§×Ô¶¨Òå´¦Àí¡£
ÔÚ½«²Ëµ¥ÏîÌí¼Óµ½CircleMenuLayoutÖ®ºó¾ÍÊÇÒª¶ÔÕâЩ²Ëµ¥Ïî½øÐгߴçÕÉÁ¿ºÍ²¼¾ÖÁË£¬ÎÒÃÇÏÈÀ´¿´ÕÉÁ¿³ß´çµÄ´úÂ룬ÈçÏ :
//ÉèÖò¼¾ÖµÄ¿í¸ß£¬²¢²ßÂÔmenu item¿í¸ß @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // ÕÉÁ¿×ÔÉí³ß´ç measureMyself(widthMeasureSpec, heightMeasureSpec); // ÕÉÁ¿²Ëµ¥Ïî³ß´ç measureChildViews(); } private void measureMyself(int widthMeasureSpec, int heightMeasureSpec) { int resWidth = 0; int resHeight = 0; // ¸ù¾Ý´«ÈëµÄ²ÎÊý£¬·Ö±ð»ñÈ¡²âÁ¿Ä£Ê½ºÍ²âÁ¿Öµ int width = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); // Èç¹û¿í»òÕ߸ߵIJâÁ¿Ä£Ê½·Ç¾«È·Öµ if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) { // Ö÷ÒªÉèÖÃΪ±³¾°Í¼µÄ¸ß¶È resWidth = getSuggestedMinimumWidth(); // Èç¹ûδÉèÖñ³¾°Í¼Æ¬£¬ÔòÉèÖÃΪÆÁÄ»¿í¸ßµÄĬÈÏÖµ resWidth = resWidth == 0 ? getDefaultWidth() : resWidth; resHeight = getSuggestedMinimumHeight(); // Èç¹ûδÉèÖñ³¾°Í¼Æ¬£¬ÔòÉèÖÃΪÆÁÄ»¿í¸ßµÄĬÈÏÖµ resHeight = resHeight == 0 ? getDefaultWidth() : resHeight; } else { // Èç¹û¶¼ÉèÖÃΪ¾«È·Öµ£¬ÔòÖ±½ÓȡСֵ£» resWidth = resHeight = Math.min(width, height); } setMeasuredDimension(resWidth, resHeight); } private void measureChildViews() { // »ñµÃ°ë¾¶ mRadius = Math.max(getMeasuredWidth(), getMeasuredHeight()); // menu itemÊýÁ¿ final int count = getChildCount(); // menu item³ß´ç int childSize = (int) (mRadius * RADIO_DEFAULT_CHILD_DIMENSION); // menu item²âÁ¿Ä£Ê½ int childMode = MeasureSpec.EXACTLY; // µü´ú²âÁ¿ for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } // ¼ÆËãmenu itemµÄ³ß´ç£»ÒÔ¼°ºÍÉèÖúõÄģʽ£¬È¥¶Ôitem½øÐвâÁ¿ int makeMeasureSpec = -1; makeMeasureSpec = MeasureSpec.makeMeasureSpec(childSize, childMode); child.measure(makeMeasureSpec, makeMeasureSpec); } mPadding = RADIO_PADDING_LAYOUT * mRadius; }
´úÂë±È½Ï¼òµ¥£¬¾ÍÊÇÏȲâÁ¿CircleMenuLayoutµÄ³ß´ç£¬È»ºó²âÁ¿Ã¿¸ö²Ëµ¥ÏîµÄ³ß´ç¡£³ß´ç»ñÈ¡ÁËÖ®ºó¾Íµ½Á˲¼¾ÖÕâÒ»²½£¬ÕâÒ²ÊÇÕû¸öÔ²Ðβ˵¥µÄºËÐÄËùÔÚ¡£´úÂëÈçÏ :
// ²¼¾Ömenu itemµÄλÖà @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int childCount = getChildCount(); int left, top; // menu item µÄ³ß´ç int itemWidth = (int) (mRadius * RADIO_DEFAULT_CHILD_DIMENSION); // ¸ù¾Ýmenu itemµÄ¸öÊý£¬¼ÆËãitemµÄ²¼¾ÖÕ¼ÓÃµÄ½Ç¶È float angleDelay = 360 / childCount; // ±éÀúËùÓв˵¥ÏîÉèÖÃËüÃǵÄλÖà for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } // ²Ëµ¥ÏîµÄÆðʼ½Ç¶È mStartAngle %= 360; // ¼ÆË㣬ÖÐÐĵ㵽menu itemÖÐÐĵľàÀë float distanceFromCenter = mRadius / 2f - itemWidth / 2 - mPadding; // distanceFromCenter cosa ¼´menu itemÖÐÐĵãµÄleft×ø±ê left = mRadius / 2 + (int)Math.round(distanceFromCenter * Math.cos(Math.toRadians(mStartAngle)) * - 1 / 2f * itemWidth); // distanceFromCenter sina ¼´menu itemµÄ×Ý×ø±ê top = mRadius / 2 + (int) Math.round(distanceFromCenter * Math.sin( Math.toRadians(mStartAngle) ) * - 1 / 2f * itemWidth); // ²¼¾Öchild view child.layout(left, top, left + itemWidth, top + itemWidth); // µþ¼Ó³ß´ç mStartAngle += angleDelay; } }
onLayoutº¯Êý¿´ÆðÀ´ÉÔÏÔ¸´ÔÓ£¬µ«ËüµÄº¬Òå¾ÍÊǽ«ËùÓв˵¥Ïî°´ÕÕÔ²»¡µÄÐÎʽ²¼¾Ö¡£Õû¸öԲΪ360¶È£¬Èç¹ûÿ¸ö²Ëµ¥ÏîÕ¼ÓõĽǶÈΪ60¶È£¬ÄÇôµÚÒ»¸ö²Ëµ¥ÏîµÄ½Ç¶ÈΪ0~60£¬ÄÇôµÚ¶þ¸ö²Ëµ¥ÏîµÄ½Ç¶È¾ÍÊÇ60~120£¬ÒÔ´ËÀàÍƽ«ËùÓв˵¥Ïî°´ÕÕÔ²Ðβ¼¾Ö¡£Ê×ÏÈҪȥ¼ÆËãÿ¸ö²Ëµ¥ÏîµÄleft ºÍ topλÖà £¬¼ÆË㹫ʽµÄͼÐλ¯±íʾÈçͼËùʾ¡£
ÉÏͼÓÒϽÇÄǸöСԲ¾ÍÊÇÎÒÃǵIJ˵¥ÏÄÇôËûµÄleft×ø±ê¾ÍÊÇmRadius / 2 + tmp * coas , top×ø±êÔòÊÇmRadius / 2 + tmp * sina ¡£ÕâÀïµÄtmp¾ÍÊÇÎÒÃÇ´úÂëÖеÄdistanceFromCenter±äÁ¿¡£µ½ÁËÕâÒ»²½Ö®ºóСÃñµÄµÚÒ»°æÔ²Ðβ˵¥ËãÊÇÍê³ÉÁË¡£
ÏÂÃæÎÒÃǾÍÀ´¼¯³ÉÒ»ÏÂÕâ¸öÔ²Ðβ˵¥¡£
´´½¨Ò»¸ö¹¤³ÌÖ®ºó£¬Ê×ÏÈÔÚ²¼¾ÖxmlÖÐÌí¼ÓÔ²Ðβ˵¥¿Ø¼þ£¬´úÂëÈçÏ :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" android:gravity="center" android:orientation="horizontal" > <com.dp.widgets.CircleMenuLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/id_menulayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/circle_bg" /> </LinearLayout>
ΪÁ˸üºÃµÄÏÔʾЧ¹û£¬ÔÚ²¼¾ÖxmlÖÐÎÒÃÇΪԲÐβ˵¥µÄÉÏÒ»²ãÒÔ¼°Ô²Ðβ˵¥±¾Ê鶼Ìí¼ÓÁËÒ»¸ö±³¾°Í¼¡£È»ºóÔÚMainActivityÖÐÉèÖò˵¥ÏîÊý¾ÝÒÔ¼°µã»÷ʼþµÈ¡£´úÂëÈçÏÂËùʾ :
public class MainActivity extends Activity { private CircleMenuLayout mCircleMenuLayout; // ²Ëµ¥±êÌâ private String[] mItemTexts = new String[] { "°²È«ÖÐÐÄ ", "ÌØÉ«·þÎñ", "Ͷ×ÊÀí²Æ", "תÕË»ã¿î", "ÎÒµÄÕË»§", "ÐÅÓÿ¨" }; // ²Ëµ¥Í¼±ê Private int[] mItemImgs = new int[] { R.drawable.home_mbank_1_normal, R.drawable.home_mbank_2_normal, R.drawable.home_mbank_3_normal, R.drawable.home_mbank_4_normal, R.drawable.home_mbank_5_normal, R.drawable.home_mbank_6_normal }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ³õʼ»¯Ô²Ðβ˵¥ mCircleMenuLayout = (CircleMenuLayout) findViewById(R.id.id_menulayout); // ÉèÖò˵¥Êý¾ÝÏî mCircleMenuLayout.setMenuItemIconsAndTexts(mItemImgs, mItemTexts); // ÉèÖò˵¥Ïîµã»÷ʼþ mCircleMenuLayout.setOnItemClickListener(new OnItemClickListener() { @Override public void onClick(View view, int pos) { Toast.makeText(MainActivity.this, mItemTexts[pos], Toast.LENGTH_SHORT).show(); } }); } }
ÔËÐÐЧ¹ûÈçÇ°ÎĵĶ¯Í¼Ëùʾ¡£
СÃñµÃÒâÑóÑóµÄ±Ä³öÁËÒ»¸ö×Ö£ºÕæ¿á£¡Í¬Ê±Ò²Îª×Ô¼ºµÄѧϰÄÜÁ¦¸Ðµ½½¾°Á£¬Á³ÉÏдÂúÁËÂú×ãÓë×ÔºÀ£¬¸Ð¾õ×Ô¼ºÓÖ³¯¸ß¼¶¹¤³ÌʦÂõ½üÁËÒ»²½¡£
¡°Õâ²»ÊÇÑóÊåдµÄÔ²Ðβ˵¥ÂСÃñÒ²ÏÂÔØÁË£¿¡±Õû×¼±¸Ï°àµÄÖ÷¹Ü¿´µ½Õâ¸öUIЧ¹ûÎʵÀ¡£Ð¡ÃñÖ»ºÃ°ÑÆäÖеÄÔµÓÉ¡¢ÊµÏÖ·½Ê½Ò»Ò»Ëµ¸øÖ÷¹ÜÌý£¬Ð¡Ãñ»¹ÌصØÇ¿µ÷ÁËCircleMenuLayoutµÄ¿É¶¨ÖÆÐÍ£¬Í¨¹ýsetMenuItemLayoutIdº¯ÊýÉèÖò˵¥ÏîµÄ²¼¾Öid£¬ÕâÑù²Ëµ¥ÏîµÄUIЧ¹û¾Í¿ÉÒÔ±»Óû§¶¨ÖÆ»¯ÁË¡£Ö÷¹ÜɨÊÓÁËСÃñµÄ´úÂ룬Ëƺõ²ì¾õ³öÁËʲô¡£ÓÚÊÇתÉíÕÒÀ´»¹ÔÚÂñÍ·Ñо¿´úÂëµÄÑóÊ壬²¢ÇÒ°ÑСÃñµÄʵÏÖ¼òµ¥½éÉÜÁËÒ»±é£¬ÑóÊåÀÏʦÔÚɨÊÓÁËÒ»±é´úÂëÖ®ºó¾Í·¢ÏÖÁËÆäÖеÄÎÊÌâËùÔÚ¡£
¡°Ð¡ÃñÄÅ£¬Äã¸Õ²Å˵Óû§Í¨¹ýsetMenuItemLayoutIdº¯Êý¿ÉÒÔÉ趨²Ëµ¥ÏîµÄUIЧ¹û¡£ÄÇôÎÊÌâÀ´ÁË£¬ÔÚÄãµÄCircleMenuLayoutÖÐĬÈÏʵÏÖµÄÊÇcircle_menu_item.xmlµÄÂß¼£¬±ÈÈç¼ÓÔز˵¥Ïî²¼¾ÖÖ®ºó»áͨ¹ýfindViewByIdÕÒµ½²¼¾ÖÖеĸ÷¸ö×ÓÊÓͼ£¬²¢ÇÒ½øÐÐÊý¾Ý°ó¶¨¡£ÀýÈçÉèÖÃͼ±êºÍÎÄ×Ö£¬µ«ÕâÊÇÕë¶Ôcircle_menu_item.xmlÕâ¸ö²¼¾ÖµÄ¾ßÌåʵÏÖ¡£Èç¹ûÓû§ÉèÖò˵¥Ïî²¼¾ÖΪother_menu_item.xml£¬²¢ÇÒÿ¸ö²Ëµ¥ÏîÐÞ¸ÄΪ¾ÍÊÇÒ»¸öButton£¬ÄÇô´ËʱËû±ØÐëÐÞ¸ÄCircleMenuLayoutÖгõʼ»¯²Ëµ¥ÏîµÄ´úÂë¡£ÒòΪ²¼¾Ö±äÁË£¬²Ëµ¥ÏîÀïÃæµÄ×ÓViewÀàÐÍÒ²±ä»¯ÁË£¬²Ëµ¥ÐèÒªµÄÊý¾ÝÒ²·¢ÉúÁ˱仯¡£ÀýÈç²Ëµ¥Ïî²»ÔÙÐèҪͼ±ê£¬Ö»ÐèÒªÎÄ×Ö¡£ÕâÑùÒ»À´£¬Óû§Ã¿»»Ò»Öֲ˵¥Ñùʽ¾ÍÐèÒªÐÞ¸ÄÒ»´ÎCircleMenuLayoutÀàÒ»´Î£¬²¢ÇÒÉèÖò˵¥Êý¾ÝµÄ½Ó¿ÚÒ²ÐèÒª¸Ä±ä¡£ÕâÑù¾ÍûÓж¨ÖÆÐÍ¿ÉÑÔÁËÂ¶øÇÒÃ÷ÏÔÎ¥·´ÁË¿ª±ÕÔÔò¡£·´¸´¶ÔCircleMenuLayout½øÐÐÐ޸IJ»Ãâ»áÒýÈë¸÷ÖÖ¸÷ÑùµÄÎÊÌâ¡¡¡±ÑóÊåÀÏʦ¹ûȻһÕë¼ûѪ£¬Éî¿Ì°¡£¡Ð¡ÃñÕâ²Å·¢ÏÖÁËÎÊÌâËùÔÚ£¬ÓÚÊÇÇë½ÌÑóÊåÀÏʦӦ¸ÃÈçºÎ´¦Àí±È½ÏºÏÊÊ¡£
¡°ÕâÖÖÇé¿öÄãÓ¦¸ÃʹÓÃAdapter£¬¾ÍÏñListViewÖеÄAdapterÒ»Ñù£¬ÈÃÓû§À´×Ô¶¨Òå²Ëµ¥ÏîµÄ²¼¾Ö¡¢½âÎö¡¢Êý¾Ý°ó¶¨µÈ¹¤×÷£¬ÄãÐèÒªÖªµÀµÄ½ö½öÊÇÿ¸ö²Ëµ¥ÏÊÇÒ»¸öView¡£ÕâÑùÒ»À´¾Í½«±ä»¯Í¨¹ýAdapter²ã¸ôÀë³öÈ¥£¬ÄãÒÀÀµµÄÖ»ÊÇAdapterÕâ¸ö³éÏó¡£Ã¿¸öÓû§¿ÉÒÔÓв»Í¬µÄʵÏÖ£¬ÄãÖ»ÐèҪʵÏÖÔ²Ðβ˵¥µÄÕÉÁ¿¡¢²¼¾Ö¹¤×÷¼´¿É¡£ÕâÑù¾Í¿ÉÒÔÓµ±§±ä»¯£¬¿É¶¨ÖÆÐԾ͵õ½Á˱£Ö¤¡£µ±È»£¬Äã¿ÉÒÔÌṩһ¸öĬÈϵÄAdapter£¬Ò²¾ÍÊÇʹÓÃÄãµÄ circle_menu_item.xml²¼¾ÖʵÏֵIJ˵¥£¬ÕâÑùûÓж¨ÖÆÐèÇóµÄÓû§¾Í¿ÉÒÔʹÓÃÕâ¸öĬÈϵÄʵÏÖÁË¡£¡±Ð¡ÃñƵƵµãÍ·£¬ÂÅÂųÆÊÇ¡£¡°ÕâȷʵÊÇÎÒ֮ǰûÓп¼ÂǺã¬Ò²ÊǾÑéȷʵ²»×㣬ÎÒÔٺúÃÖع¹Ò»Ï¡£¡±Ð¡Ãñ·¢ÏÖÎÊÌâÖ®ºóÒ²³ÐÈÏÁË×Ô¼ºµÄ²»×㣬Á½Î»Ç°±²¿´Ð¡ÃñÕâôºÃѧ¾ÍÅã×ÅСÃñÒ»¿éÖع¹´úÂë¡£
ÔÚÁ½Î»Ç°±²µÄÖ¸µãÏ£¬¾¹ý²»µ½Îå·ÖÖÓÖع¹£¬Ð¡ÃñµÄCircleMenuLayout³ÉÁËÏÂÃæÕâÑù¡£
// Ô²Ðβ˵¥ public class CircleMenuLayout extends ViewGroup { // ×Ö¶ÎÊ¡ÂÔ // ÉèÖÃAdapter public void setAdapter(ListAdapter mAdapter) { this.mAdapter = mAdapter; } // ¹¹½¨²Ëµ¥Ïî private void buildMenuItems() { // ¸ù¾ÝÓû§ÉèÖõIJÎÊý£¬³õʼ»¯menu item for (int i = 0; i < mAdapter.getCount(); i++) { final View itemView = mAdapter.getView(i, null, this); final int position = i; itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnMenuItemClickListener != null) { mOnMenuItemClickListener.onClick(itemView, position); } } }); // Ìí¼Óviewµ½ÈÝÆ÷ÖÐ addView(itemView); } } @Override protected void onAttachedToWindow() { if (mAdapter != null) { buildMenuItems(); } super.onAttachedToWindow(); } // ÕÉÁ¿¡¢²¼¾Ö´úÂëÊ¡ÂÔ }
ÏÖÔÚµÄCircleMenuLayout°Ñ½âÎöxml¡¢³õʼ»¯²Ëµ¥ÏîµÄ¾ßÌ幤×÷ÒƳý£¬Ìí¼ÓÁËÒ»¸öAdapter£¬ÔÚÓû§ÉèÖÃÁËAdapterÖ®ºó£¬ÔÚonAttachedToWindowº¯ÊýÖе÷ÓÃAdapterµÄgetCountº¯Êý»ñÈ¡²Ëµ¥ÏîµÄÊýÁ¿£¬È»ºóͨ¹ýgetViewº¯Êý»ñȡÿ¸öView£¬×îºó½«ÕâЩ²Ëµ¥ÏîµÄViewÌí¼Óµ½Ô²Ðβ˵¥ÖУ¬Ô²Ðβ˵¥²¼¾ÖÔÙ½«ËûÃDz¼¾Öµ½Ìض¨µÄλÖü´¿É¡£
ÎÒÃÇ¿´ÏÖÔÚʹÓÃCircleMenuLayoutÊÇÔõÑùµÄÐÎʽ¡£Ê×Ïȶ¨ÒåÁËÒ»¸öʵÌåÀàMenuItemÀ´´æ´¢²Ëµ¥Ïîͼ±êºÍÎı¾µÄÐÅÏ¢£¬´úÂëÈçÏ :
static class MenuItem { public int imageId; public String title; public MenuItem(String title, int resId) { this.title = title; imageId = resId; } }
È»ºóÔÙʵÏÖÒ»¸öAdapter£¬Õâ¸öAdapterµÄÀàÐ;ÍÊÇListAdapter¡£ÎÒÃÇÐèÒªÔÚgetViewÖмÓÔز˵¥Ïîxml¡¢°ó¶¨Êý¾ÝµÈ£¬Ïà¹Ø´úÂëÈçÏ :
static class CircleMenuAdapter extends BaseAdapter { List<MenuItem> mMenuItems; public CircleMenuAdapter(List<MenuItem> menuItems) { mMenuItems = menuItems; } // ¼ÓÔز˵¥Ïî²¼¾Ö£¬²¢ÇÒ³õʼ»¯Ã¿¸ö²Ëµ¥ @Override public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater mInflater = LayoutInflater.from(parent.getContext()); View itemView = mInflater.inflate(R.layout.circle_menu_item, parent, false); initMenuItem(itemView, position); return itemView; } // ³õʼ»¯²Ëµ¥Ïî private void initMenuItem(View itemView, int position) { // »ñÈ¡Êý¾ÝÏî final MenuItem item = getItem(position); ImageView iv = (ImageView) itemView .findViewById(R.id.id_circle_menu_item_image); TextView tv = (TextView) itemView .findViewById(R.id.id_circle_menu_item_text); // Êý¾Ý°ó¶¨ iv.setImageResource(item.imageId); tv.setText(item.title); } // Ê¡ÂÔ»ñÈ¡item countµÈ´úÂë }
ÕâÓëÎÒÃÇÔÚListViewÖÐʹÓÃAdapterÊÇÒ»Öµģ¬ÊµÏÖgetView¡¢getCountµÈº¯Êý£¬ÔÚgetViewÖмÓÔØÿһÏîµÄ²¼¾ÖÎļþ£¬²¢ÇÒ°ó¶¨Êý¾ÝµÈ¡£×îÖÕ½«²Ëµ¥View·µ»Ø£¬È»ºóÕâ¸öView¾Í»á±»Ìí¼Óµ½CircleMenuLayoutÖС£ÕâÒ»²½µÄ²Ù×÷ÔÀ´ÊÇ·ÅÔÚCircleMenuLayoutÖеģ¬ÏÖÔÚ±»¶ÀÁ¢³öÀ´£¬²¢ÇÒͨ¹ýAdapter½øÐÐÁ˸ôÀë¡£ÕâÑù¾Í½«Ò×±äµÄ²¿·Öͨ¹ýAdapter³éÏó¸ôÀ뿪À´£¬¼´Ê¹Óû§ÓгÉǧÉÏÍòÖв˵¥ÏîUIЧ¹û£¬ÄÇôͨ¹ýAdapter¾Í¿ÉÒÔºÜÈÝÒ׵ĽøÐÐÀ©Õ¹¡¢ÊµÏÖ£¬¶ø²»ÐèҪÿ´Î¶¼ÐÞ¸ÄCircleMenuLayoutÖеĴúÂë¡£CircleMenuLayout²¼¾ÖÀàÏ൱ÓÚÌṩÁËÒ»¸öÔ²Ðβ¼¾Ö³éÏó£¬ÖÁÓÚÿһ¸ö×ÓViewÊÇɶÑùµÄËü²¢²»ÐèÒª¹ØÐÄ¡£Í¨¹ýAdapter¸ôÀë±ä»¯£¬Óµ±§±ä»¯£¬¾ÍÊÇÕâô¼òµ¥¡£
¡°ÔÀ´ListView¡¢RecyclerViewͨ¹ýÒ»¸öAdapterÊÇÕâ¸öÔÒò£¬Í¨¹ýAdapter½«Ò×±äµÄ²¿·Ö¶ÀÁ¢³öÈ¥½»¸øÓû§´¦Àí¡£ÓÖͨ¹ý¹Û²ìÕßģʽ½«Êý¾ÝºÍUI½âñîºÏ£¬Ê¹µÃViewÓëÊý¾ÝûÓÐÒÀÀµ£¬Ò»·ÝÊý¾Ý¿ÉÒÔ×÷ÓÃÓÚ¶à¸öUI£¬Ó¦¶ÔUIµÄÒ×±äÐÔ¡£ÔÀ´Èç´Ë£¡¡±Ð¡Ãñ×îºó×ܽáµÀ¡£
ÀýÈ磬µ±ÎÒÃǵIJúÆ··¢Éú±ä»¯£¬ÐèÒª½«Ô²Ðβ˵¥ÐÞ¸ÄΪÆÕͨµÄListViewÑùʽ£¬ÄÇôÎÒÃÇÒª×öµÄʺܼòµ¥£¬¾ÍÊǽ«xml²¼¾ÖÖеÄCircleMenuLayoutÐÞ¸ÄΪListView£¬È»ºó½«AdapterÉèÖøøListView¼´¿É¡£´úÂëÈçÏ :
public class MainActivity extends Activity { private ListView mListView; List<MenuItem> mMenuItems = new ArrayList<MenuItem>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Ä£ÄâÊý¾Ý mockMenuItems(); mListView = (ListView) findViewById(R.id.id_menulayout); // ÉèÖÃÊÊÅäÆ÷ mListView.setAdapter(new CircleMenuAdapter(mMenuItems)); // ÉèÖõã»÷ʼþ mListView.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, mMenuItems.get(position).title, Toast.LENGTH_SHORT).show(); } }); }
ÕâÑùÎÒÃǾÍÍê³ÉÁËUIÌæ»»£¬³É±¾ºÜµÍ£¬Ò²»ù±¾²»»áÒý·¢ÆäËû´íÎó¡£ÕâÒ²¾ÍÊÇΪʲôÎÒÃÇÔÚCircleMenuLayoutÖÐҪʹÓÃListAdapterµÄÔÒò£¬¾ÍÊÇΪÁËÓëÏÖÓеÄListView¡¢GridViewµÈ×é¼þ½øÐмæÈÝ£¬µ±È»ÎÒÃÇҲûÓÐɶ±ØÒªÖØÐÂÔÙ¶¨ÒåÒ»¸öAdapterÀàÐÍ£¬´Ó´ËÎÒÃǾͿÉÒÔÈÎÒâÐÞ¸ÄÎÒÃǵIJ˵¥ItemÑùʽÁË£¬±£Ö¤ÁËÕâ¸ö×é¼þµÄÁé»îÐÔ!! Ì滻ΪListViewµÄЧ¹ûÈçÏÂËùʾ:
¡°×ߣ¬ÎÒÇëÁ½Î»Ç°±²³Ô¿¾ÓãÈ¥£¡¡±Ð¡ÃñÔÚÖع¹ÍêCircleMenuLayoutÖ®ºóÉî¸ÐÊÕ»ñÆĶ࣬ΪÁ˱¨´ðÖ÷¹ÜºÍÑóÊåµÄÖ¸µãÈÂÈÂ×ÅÒªÇë³Ô·¹¡£¡°ÄǾÍ×ß°É£¡¡±Ö÷¹Üµ¹ÊÇˬ¿ìµÄ´ðÓ¦ÁË£¬ÑóÊåÀÏʦҲÊÇÁ¢ÂíÓ¦ÔÊ£¬ÈýÈËÊÕÊ°ºÃµçÄÔºó¾Í³¯×Å¥ϵÄÎ×ɽ¿¾Óãµê×ßÈ¥¡£
20.9×ܽá
AdapterģʽµÄ¾µäʵÏÖÔÚÓÚ½«Ô±¾²»¼æÈݵĽӿÚÈÚºÏÔÚÒ»Æð£¬Ê¹Ö®Äܹ»ºÜºÃµÄ½øÐкÏ×÷¡£µ«ÊÇÔÚʵ¼Ê¿ª·¢ÖУ¬AdapterģʽҲÓÐһЩÁé»îµÄʵÏÖ¡£ÀýÈçListViewÖеĸôÀë±ä»¯£¬Ê¹µÃÕû¸öUI¼Ü¹¹±äµÃ¸üÁé»î£¬Äܹ»Óµ±§±ä»¯¡£AdapterģʽÔÚ¿ª·¢ÖÐÔËÓ÷dz£¹ã·º£¬Òò´ËÕÆÎÕAdapterģʽÊǷdz£±ØÒªµÄ¡£
¹ØÓÚAdapterģʽʵս֮Öع¹ºèÑó¼¯ÍŵÄAndroidÔ²Ðβ˵¥½¨ÐеÄÏà¹Ø֪ʶ¾Í¸ø´ó¼Ò½éÉܵ½ÕâÀϣÍû¶Ô´ó¼ÒÓÐËù°ïÖú£¡