您好,登錄后才能下訂單哦!
本文講述了Android自定義橫向滑動菜單的實現。分享給大家供大家參考,具體如下:
前言
開發安卓過程中,經常會用到標題欄的樣式,有時候傳統方式不能滿足開發者的需要,這時候就需要自定義控件來實現。(注意:本文提供思路,有關鍵代碼,但是代碼不全)
標題欄說明
自定義標題欄ColumnHorizontalScrollView繼承HorizontalScrollView 這個安卓原生的控件,HorizontalScrollView是一種FrameLayout(框架布局),其子項被滾動查看時是整體移動的,并且子項本身可以是一個有復雜層次結構的布局管理器。一個常見的應用是子項在水平方向中,用戶可以滾動顯示頂層水平排列的子項(items)。
在布局文件中添加ColumnHorizontalScrollView控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tabs="http://schemas.android.com/apk/res-auto" android:id="@+id/homeTabs" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/db_bg_gray" android:orientation="vertical"> <com.wankr.app.widget.ColumnHorizontalScrollView android:id="@+id/mColumnHorizontalScrollView" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_centerVertical="true" android:scrollbars="none"> <LinearLayout android:id="@+id/mRadioGroup_content" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="10.0dip" android:paddingRight="10.0dip" /> </com.wankr.app.widget.ColumnHorizontalScrollView> <android.support.v4.view.ViewPager android:id="@+id/contentPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
橫向菜單中展示界面
注意:可以設置菜單中標題的寬度大小,最好標題寬度一致。
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; /** 請求CODE */ public final static int CHANNELREQUEST = 1; /** 屏幕寬度 */ private int mScreenWidth = 0; /** Item寬度 */ private int mItemWidth = 0; /** 當前選中的欄目*/ private int columnSelectIndex = 0; // 標簽信息 private List<ChannelItem> channelItems = new ArrayList<ChannelItem>(); @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; } /** * 設置標題適配器 * @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(); } /** * 標題設置 */ @Override public CharSequence getPageTitle(int position) { ChannelItem item = channelItems.get(position); return item.getChanneName(); } } /** * ViewPager切換監聽方法 * */ 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); } // 指向對應的tab位置 switch (tab_postion) { } } /** * 當欄目項發生變化時候調用 */ private void setChangeView() { gettColumnData(); initTabColumn(); } /** * 獲取標簽欄數據 */ 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, "最新")); /** * 標題可以動態設置長度。獲取數據添加到集合中展示。 */ pagerAdapter.notifyDataSetChanged(); } /** *初始化Column欄目項 */ private void initTabColumn() { mRadioGroup_content.removeAllViews(); int count = channelItems.size(); // 設置橫向菜單欄中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); } // 對item的監聽 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 } }
標題菜單橫向滑動自定義控件
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; /** 傳入拖動欄布局 */ private View rl_column; /** 左陰影圖片 */ private ImageView leftImage; /** 右陰影圖片 */ private ImageView rightImage; /** 屏幕寬度 */ private int mScreenWitdh = 0; /** 父類的活動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); } /** * 在拖動的時候執行 * */ @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; } //如果滑動在最左邊時候,左邊陰影隱藏,右邊顯示 if (getLeft() == 0) { leftImage.setVisibility(View.GONE); rightImage.setVisibility(View.VISIBLE); return; } //如果滑動在最右邊時候,左邊陰影顯示,右邊隱藏 if (getRight() == getMeasuredWidth() - mScreenWitdh) { leftImage.setVisibility(View.VISIBLE); rightImage.setVisibility(View.GONE); return; } //否則,說明在中間位置,左、右陰影都顯示 leftImage.setVisibility(View.VISIBLE); rightImage.setVisibility(View.VISIBLE); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。