本文講述了Android自定義橫向滑動(dòng)菜單的實(shí)現(xiàn)。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),寧強(qiáng)企業(yè)網(wǎng)站建設(shè),寧強(qiáng)品牌網(wǎng)站建設(shè),網(wǎng)站定制,寧強(qiáng)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,寧強(qiáng)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
前言
開(kāi)發(fā)安卓過(guò)程中,經(jīng)常會(huì)用到標(biāo)題欄的樣式,有時(shí)候傳統(tǒng)方式不能滿足開(kāi)發(fā)者的需要,這時(shí)候就需要自定義控件來(lái)實(shí)現(xiàn)。(注意:本文提供思路,有關(guān)鍵代碼,但是代碼不全)
標(biāo)題欄說(shuō)明
自定義標(biāo)題欄ColumnHorizontalScrollView繼承HorizontalScrollView 這個(gè)安卓原生的控件,HorizontalScrollView是一種FrameLayout(框架布局),其子項(xiàng)被滾動(dòng)查看時(shí)是整體移動(dòng)的,并且子項(xiàng)本身可以是一個(gè)有復(fù)雜層次結(jié)構(gòu)的布局管理器。一個(gè)常見(jiàn)的應(yīng)用是子項(xiàng)在水平方向中,用戶可以滾動(dòng)顯示頂層水平排列的子項(xiàng)(items)。
在布局文件中添加ColumnHorizontalScrollView控件
橫向菜單中展示界面
注意:可以設(shè)置菜單中標(biāo)題的寬度大小,最好標(biāo)題寬度一致。
package com.example.app; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.LayoutParams; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Fragment implements OnClickListener { private ViewPager contentPager; private ContentPagerAdapter pagerAdapter; private ColumnHorizontalScrollView mColumnHorizontalScrollView; private LinearLayout mRadioGroup_content; /** 請(qǐng)求CODE */ public final static int CHANNELREQUEST = 1; /** 屏幕寬度 */ private int mScreenWidth = 0; /** Item寬度 */ private int mItemWidth = 0; /** 當(dāng)前選中的欄目*/ private int columnSelectIndex = 0; // 標(biāo)簽信息 private ListchannelItems = new ArrayList (); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.activity_main, container , false); this.contentPager = (ViewPager) v.findViewById(R.id.contentPager); this.pagerAdapter = new ContentPagerAdapter(getChildFragmentManager()); this.contentPager.setAdapter(this.pagerAdapter); this.contentPager.setCurrentItem(0); this.contentPager.setOnPageChangeListener(pageListener); this.mColumnHorizontalScrollView = (ColumnHorizontalScrollView) v.findViewById(R.id.mColumnHorizontalScrollView); this.mRadioGroup_content = (LinearLayout) v.findViewById(R.id.mRadioGroup_content); this.setChangeView(); return v; } /** * 設(shè)置標(biāo)題適配器 * @author raotaisen * */ private class ContentPagerAdapter extends FragmentPagerAdapter { public ContentPagerAdapter(FragmentManager fm) { super(fm); // TODO Auto-generated constructor stub } @Override public Fragment getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public int getCount() { // TODO Auto-generated method stub return channelItems.size(); } /** * 標(biāo)題設(shè)置 */ @Override public CharSequence getPageTitle(int position) { ChannelItem item = channelItems.get(position); return item.getChanneName(); } } /** * ViewPager切換監(jiān)聽(tīng)方法 * */ public ViewPager.OnPageChangeListener pageListener= new ViewPager.OnPageChangeListener(){ @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageSelected(int position) { contentPager.setCurrentItem(position); selectTab(position); } }; /** * 選擇的Column里面的Tab * */ private void selectTab(int tab_postion) { columnSelectIndex = tab_postion; for (int i = 0; i < mRadioGroup_content.getChildCount(); i++) { View checkView = mRadioGroup_content.getChildAt(tab_postion); int k = checkView.getMeasuredWidth(); int l = checkView.getLeft(); int i2 = l + k / 2 - mScreenWidth / 2; // rg_nav_content.getParent()).smoothScrollTo(i2, 0); mColumnHorizontalScrollView.smoothScrollTo(i2, 0); // mColumnHorizontalScrollView.smoothScrollTo((position - 2) * // mItemWidth , 0); } //判斷是否選中 for (int j = 0; j < mRadioGroup_content.getChildCount(); j++) { View checkView = mRadioGroup_content.getChildAt(j); boolean ischeck; if (j == tab_postion) { ischeck = true; } else { ischeck = false; } checkView.setSelected(ischeck); } // 指向?qū)?yīng)的tab位置 switch (tab_postion) { } } /** * 當(dāng)欄目項(xiàng)發(fā)生變化時(shí)候調(diào)用 */ private void setChangeView() { gettColumnData(); initTabColumn(); } /** * 獲取標(biāo)簽欄數(shù)據(jù) */ private void gettColumnData() { channelItems.clear(); channelItems.add(new ChannelItem(null, "蔥蔥")); channelItems.add(new ChannelItem(null, "飛飛")); channelItems.add(new ChannelItem(null, "vv")); channelItems.add(new ChannelItem(null, "剛子")); channelItems.add(new ChannelItem(null, "最新")); /** * 標(biāo)題可以動(dòng)態(tài)設(shè)置長(zhǎng)度。獲取數(shù)據(jù)添加到集合中展示。 */ pagerAdapter.notifyDataSetChanged(); } /** *初始化Column欄目項(xiàng) */ private void initTabColumn() { mRadioGroup_content.removeAllViews(); int count = channelItems.size(); // 設(shè)置橫向菜單欄中item屬性 mColumnHorizontalScrollView.setParam(getActivity(), mScreenWidth, mRadioGroup_content, null, null, null, null); for(int i = 0; i< count; i++){ LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT); params.leftMargin = 6; params.rightMargin = 6; // TextView localTextView = (TextView) mInflater.inflate(R.layout.column_radio_item, null); TextView columnTextView = new TextView(getActivity()); columnTextView.setTextSize(16); columnTextView.setGravity(Gravity.CENTER); columnTextView.setPadding(5, 5, 5, 5); columnTextView.setId(i); columnTextView.setText(channelItems.get(i).getChanneName()); // columnTextView.setTextColor(getResources().getColorStateList(R.color.top_category_scroll_text_color_day)); if(columnSelectIndex == i){ columnTextView.setSelected(true); } // 對(duì)item的監(jiān)聽(tīng) columnTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { for(int i = 0;i < mRadioGroup_content.getChildCount();i++){ View localView = mRadioGroup_content.getChildAt(i); if (localView != v) { localView.setSelected(false); }else{ localView.setSelected(true); contentPager.setCurrentItem(i); } } // Toast.makeText(getApplicationContext(), userChannelList.get(v.getId()).getName(), Toast.LENGTH_SHORT).show(); } }); mRadioGroup_content.addView(columnTextView, i ,params); } } @Override public void onClick(View v) { // TODO Auto-generated method stub } }
標(biāo)題菜單橫向滑動(dòng)自定義控件
package com.example.app; import android.app.Activity; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.HorizontalScrollView; import android.widget.ImageView; public class ColumnHorizontalScrollView extends HorizontalScrollView { /** 傳入整體布局 */ private View ll_content; /** 傳入更多欄目選擇布局 */ private View ll_more; /** 傳入拖動(dòng)欄布局 */ private View rl_column; /** 左陰影圖片 */ private ImageView leftImage; /** 右陰影圖片 */ private ImageView rightImage; /** 屏幕寬度 */ private int mScreenWitdh = 0; /** 父類的活動(dòng)activity */ private Activity activity; public ColumnHorizontalScrollView(Context context) { super(context); } public ColumnHorizontalScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ColumnHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 在拖動(dòng)的時(shí)候執(zhí)行 * */ @Override protected void onScrollChanged(int paramInt1, int paramInt2, int paramInt3, int paramInt4) { // TODO Auto-generated method stub super.onScrollChanged(paramInt1, paramInt2, paramInt3, paramInt4); shade_ShowOrHide(); if(!activity.isFinishing() && ll_content !=null && leftImage!=null && rightImage!=null && ll_more!=null && rl_column !=null){ if(ll_content.getWidth() <= mScreenWitdh){ leftImage.setVisibility(View.GONE); rightImage.setVisibility(View.GONE); } }else{ return; } if(paramInt1 ==0){ leftImage.setVisibility(View.GONE); rightImage.setVisibility(View.VISIBLE); return; } if(ll_content.getWidth() - paramInt1 + ll_more.getWidth() + rl_column.getLeft() == mScreenWitdh){ leftImage.setVisibility(View.VISIBLE); rightImage.setVisibility(View.GONE); return; } leftImage.setVisibility(View.VISIBLE); rightImage.setVisibility(View.VISIBLE); } /** * 傳入父類布局中的資源文件 * */ public void setParam(Activity activity, int mScreenWitdh,View paramView1,ImageView paramView2, ImageView paramView3 ,View paramView4,View paramView5){ this.activity = activity; this.mScreenWitdh = mScreenWitdh; ll_content = paramView1; leftImage = paramView2; rightImage = paramView3; ll_more = paramView4; rl_column = paramView5; } /** * 判斷左右陰影的顯示隱藏效果 * */ public void shade_ShowOrHide() { if (!activity.isFinishing() && ll_content != null) { measure(0, 0); //如果整體寬度小于屏幕寬度的話,那左右陰影都隱藏 if (mScreenWitdh >= getMeasuredWidth()) { leftImage.setVisibility(View.GONE); rightImage.setVisibility(View.GONE); } } else { return; } //如果滑動(dòng)在最左邊時(shí)候,左邊陰影隱藏,右邊顯示 if (getLeft() == 0) { leftImage.setVisibility(View.GONE); rightImage.setVisibility(View.VISIBLE); return; } //如果滑動(dòng)在最右邊時(shí)候,左邊陰影顯示,右邊隱藏 if (getRight() == getMeasuredWidth() - mScreenWitdh) { leftImage.setVisibility(View.VISIBLE); rightImage.setVisibility(View.GONE); return; } //否則,說(shuō)明在中間位置,左、右陰影都顯示 leftImage.setVisibility(View.VISIBLE); rightImage.setVisibility(View.VISIBLE); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。