既然 Fragment 取代了TabActivity,当然 TabActivity 的能实现的菜单栏,Fragment 当然也能实现。主要其实就是通过菜单栏的点击事件切换 Fragment 的显示和隐藏。
来看看栗子吧:
1.效果图来了:
2.代码具体实现
(1)对应的 Fragment 编辑代码和布局实现在前面的Fragment介绍和简单实现 中已经有提及,代码中没复杂的地方,此处略过,具体可看实例代码。
(2)菜单栏实现,这里使用代码实现的,其实也可以用布局文件实现,代码如下:
package com.yanis.yc_ui_fragment_menu; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; /** * * @author YeChao * @功能描述:自定义底部工具栏 * */ public class ViewIndicator extends LinearLayout implements OnClickListener { private int mDefaultIndicator = 0; // 默认的选定View private static int mCurIndicator; // 当前选定View private static View[] mIndicators; // View集合 private OnIndicateListener mOnIndicateListener; // 对应的监听器 // 对应的图标Tag private static final String TAG_ICON_0 = "icon_tag_0"; private static final String TAG_ICON_1 = "icon_tag_1"; private static final String TAG_ICON_2 = "icon_tag_2"; private static final String TAG_ICON_3 = "icon_tag_3"; private static final String TAG_ICON_4 = "icon_tag_4"; // 对应的文字Tag private static final String TAG_TEXT_0 = "text_tag_0"; private static final String TAG_TEXT_1 = "text_tag_1"; private static final String TAG_TEXT_2 = "text_tag_2"; private static final String TAG_TEXT_3 = "text_tag_3"; private static final String TAG_TEXT_4 = "text_tag_4"; // 未选中状态 private static final int COLOR_UNSELECT = Color.argb(100, 0xff, 0xff, 0xff); // 选中状态 private static final int COLOR_SELECT = Color.WHITE; // 构造函数 public ViewIndicator(Context context) { super(context); } public ViewIndicator(Context context, AttributeSet attrs) { super(context, attrs); mCurIndicator = mDefaultIndicator; setOrientation(LinearLayout.HORIZONTAL);// 水平布局 init(); } /** * 菜单视图布局 * * @param iconResID * 图片资源ID * @param stringResID * 文字资源ID * @param stringColor * 颜色资源ID * @param iconTag * 图片标签 * @param textTag * 文字标签 * @return */ private View createIndicator(int iconResID, int stringResID, int stringColor, String iconTag, String textTag) { // 实例一个LinearLayout LinearLayout view = new LinearLayout(getContext()); view.setOrientation(LinearLayout.VERTICAL);// 垂直布局 // 设置宽高和权重 view.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1)); view.setGravity(Gravity.CENTER_HORIZONTAL); view.setBackgroundResource(R.drawable.main_tab_item_bg_normal); // 实例一个ImageView ImageView iconView = new ImageView(getContext()); // 设置与该ImageView视图相关联的标记 iconView.setTag(iconTag); // 设置宽高和权重 iconView.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1)); iconView.setImageResource(iconResID);// 设置图片资源 // 实例一个TextView TextView textView = new TextView(getContext()); // 设置与该TextView视图相关联的标记 textView.setTag(textTag); // 设置宽高和权重 textView.setLayoutParams(new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1)); // 设置文字颜色 textView.setTextColor(stringColor); // 设置文字大小 textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); // 设置文字资源 textView.setText(stringResID); // 添加视图到布局中 view.addView(iconView); view.addView(textView); // 返回布局视图 return view; } /** * 初始化视图 */ private void init() { mIndicators = new View[5];// 5个View // 第一个为默认选中的 // 主页main_tab_item_bg_normal mIndicators[0] = createIndicator(R.drawable.main_tab_item_home_focus, R.string.tab_item_home, COLOR_SELECT, TAG_ICON_0, TAG_TEXT_0); mIndicators[0].setBackgroundResource(R.drawable.main_tab_item_bg); mIndicators[0].setTag(Integer.valueOf(0)); mIndicators[0].setOnClickListener(this); addView(mIndicators[0]); // 分类 mIndicators[1] = createIndicator( R.drawable.main_tab_item_category_normal, R.string.tab_item_category, COLOR_UNSELECT, TAG_ICON_1, TAG_TEXT_1); mIndicators[1].setBackgroundResource(R.drawable.main_tab_item_bg); mIndicators[1].setTag(Integer.valueOf(1)); mIndicators[1].setOnClickListener(this); addView(mIndicators[1]); // 下载 mIndicators[2] = createIndicator(R.drawable.main_tab_item_down_normal, R.string.tab_item_down, COLOR_UNSELECT, TAG_ICON_2, TAG_TEXT_2); mIndicators[2].setBackgroundResource(R.drawable.main_tab_item_bg); mIndicators[2].setTag(Integer.valueOf(2)); mIndicators[2].setOnClickListener(this); addView(mIndicators[2]); // 我的 mIndicators[3] = createIndicator(R.drawable.main_tab_item_user_normal, R.string.tab_item_user, COLOR_UNSELECT, TAG_ICON_3, TAG_TEXT_3); mIndicators[3].setBackgroundResource(R.drawable.main_tab_item_bg); mIndicators[3].setTag(Integer.valueOf(3)); mIndicators[3].setOnClickListener(this); addView(mIndicators[3]); // 设置 mIndicators[4] = createIndicator( R.drawable.main_tab_item_setting_normal, R.string.tab_item_setting, COLOR_UNSELECT, TAG_ICON_4, TAG_TEXT_4); mIndicators[4].setBackgroundResource(R.drawable.main_tab_item_bg); mIndicators[4].setTag(Integer.valueOf(4)); mIndicators[4].setOnClickListener(this); addView(mIndicators[4]); } public static void setIndicator(int which) { // /////////////////清除之前的状态///////////////////////////////// // mIndicators[mCurIndicator].setBackgroundResource(R.drawable.main_tab_item_bg_normal); ImageView prevIcon; TextView prevText; switch (mCurIndicator) { case 0: prevIcon = (ImageView) mIndicators[mCurIndicator] .findViewWithTag(TAG_ICON_0); prevIcon.setImageResource(R.drawable.main_tab_item_home); prevText = (TextView) mIndicators[mCurIndicator] .findViewWithTag(TAG_TEXT_0); prevText.setTextColor(COLOR_UNSELECT); break; case 1: prevIcon = (ImageView) mIndicators[mCurIndicator] .findViewWithTag(TAG_ICON_1); prevIcon.setImageResource(R.drawable.main_tab_item_category); prevText = (TextView) mIndicators[mCurIndicator] .findViewWithTag(TAG_TEXT_1); prevText.setTextColor(COLOR_UNSELECT); break; case 2: prevIcon = (ImageView) mIndicators[mCurIndicator] .findViewWithTag(TAG_ICON_2); prevIcon.setImageResource(R.drawable.main_tab_item_down); prevText = (TextView) mIndicators[mCurIndicator] .findViewWithTag(TAG_TEXT_2); prevText.setTextColor(COLOR_UNSELECT); break; case 3: prevIcon = (ImageView) mIndicators[mCurIndicator] .findViewWithTag(TAG_ICON_3); prevIcon.setImageResource(R.drawable.main_tab_item_user); prevText = (TextView) mIndicators[mCurIndicator] .findViewWithTag(TAG_TEXT_3); prevText.setTextColor(COLOR_UNSELECT); break; case 4: prevIcon = (ImageView) mIndicators[mCurIndicator] .findViewWithTag(TAG_ICON_4); prevIcon.setImageResource(R.drawable.main_tab_item_setting); prevText = (TextView) mIndicators[mCurIndicator] .findViewWithTag(TAG_TEXT_4); prevText.setTextColor(COLOR_UNSELECT); break; } // /////////////////更新前状态///////////////////////////////// // mIndicators[which].setBackgroundResource(R.drawable.main_tab_item_bg_focus); ImageView currIcon; TextView currText; /** * 设置选中状态 */ switch (which) { case 0: currIcon = (ImageView) mIndicators[which] .findViewWithTag(TAG_ICON_0); currIcon.setImageResource(R.drawable.main_tab_item_home_focus); currText = (TextView) mIndicators[which] .findViewWithTag(TAG_TEXT_0); currText.setTextColor(COLOR_SELECT); break; case 1: currIcon = (ImageView) mIndicators[which] .findViewWithTag(TAG_ICON_1); currIcon.setImageResource(R.drawable.main_tab_item_category_focus); currText = (TextView) mIndicators[which] .findViewWithTag(TAG_TEXT_1); currText.setTextColor(COLOR_SELECT); break; case 2: currIcon = (ImageView) mIndicators[which] .findViewWithTag(TAG_ICON_2); currIcon.setImageResource(R.drawable.main_tab_item_down_focus); currText = (TextView) mIndicators[which] .findViewWithTag(TAG_TEXT_2); currText.setTextColor(COLOR_SELECT); break; case 3: currIcon = (ImageView) mIndicators[which] .findViewWithTag(TAG_ICON_3); currIcon.setImageResource(R.drawable.main_tab_item_user_focus); currText = (TextView) mIndicators[which] .findViewWithTag(TAG_TEXT_3); currText.setTextColor(COLOR_SELECT); break; case 4: currIcon = (ImageView) mIndicators[which] .findViewWithTag(TAG_ICON_4); currIcon.setImageResource(R.drawable.main_tab_item_setting_focus); currText = (TextView) mIndicators[which] .findViewWithTag(TAG_TEXT_4); currText.setTextColor(COLOR_SELECT); break; } mCurIndicator = which; } public interface OnIndicateListener { public void onIndicate(View v, int which); } public void setOnIndicateListener(OnIndicateListener listener) { mOnIndicateListener = listener; } @Override public void onClick(View v) { if (mOnIndicateListener != null) { int tag = (Integer) v.getTag(); switch (tag) { case 0: if (mCurIndicator != 0) { mOnIndicateListener.onIndicate(v, 0); setIndicator(0); } break; case 1: if (mCurIndicator != 1) { mOnIndicateListener.onIndicate(v, 1); setIndicator(1); } break; case 2: if (mCurIndicator != 2) { mOnIndicateListener.onIndicate(v, 2); setIndicator(2); } break; case 3: if (mCurIndicator != 3) { mOnIndicateListener.onIndicate(v, 3); setIndicator(3); } break; case 4: if (mCurIndicator != 4) { mOnIndicateListener.onIndicate(v, 4); setIndicator(4); } break; default: break; } } } }
ViewIndicator
(3)最后就是主界面代码,切换 Fragment 的显示和隐藏以及菜单栏的选中状态
package com.yanis.yc_ui_fragment_menu; import com.yanis.yc_ui_fragment_menu.ViewIndicator.OnIndicateListener; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.View; public class MainActivity extends FragmentActivity { public static Fragment[] mFragments; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setFragmentIndicator(0); } /** * 初始化fragment */ private void setFragmentIndicator(int whichIsDefault) { //实例化 Fragment 集合 mFragments = new Fragment[5]; mFragments[0] = getSupportFragmentManager().findFragmentById(R.id.fragment_home); mFragments[1] = getSupportFragmentManager().findFragmentById(R.id.fragment_category); mFragments[2] = getSupportFragmentManager().findFragmentById(R.id.fragment_down); mFragments[3] = getSupportFragmentManager().findFragmentById(R.id.fragment_user); mFragments[4] = getSupportFragmentManager().findFragmentById(R.id.fragment_setting); //显示默认的Fragment getSupportFragmentManager().beginTransaction().hide(mFragments[0]) .hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]).show(mFragments[whichIsDefault]).commit(); //绑定自定义的菜单栏组件 ViewIndicator mIndicator = (ViewIndicator) findViewById(R.id.indicator); ViewIndicator.setIndicator(whichIsDefault); mIndicator.setOnIndicateListener(new OnIndicateListener() { @Override public void onIndicate(View v, int which) { //显示指定的Fragment getSupportFragmentManager().beginTransaction() .hide(mFragments[0]).hide(mFragments[1]) .hide(mFragments[2]).hide(mFragments[3]).hide(mFragments[4]).show(mFragments[which]).commit(); } }); } }
源代码地址:https://github.com/YeXiaoChao/Yc_ui_fragment_menu
时间: 2024-10-21 15:39:13