安卓底部导航栏点击变色切换不同Fragment
2016-12-15 15:33
495 查看
“`
public class BottomNavigationBar extends LinearLayout { private Context context; public BottomNavigationBar(Context context) { super(context); this.context = context; } /** * 底部的显示模式 */ public enum NavShowType { NORMAL, //普通模式,包含文字和图片 NO_TEXT, //无文字模式 NO_IMAGE, //无图模式 CHECKED_SHOW_TEXT //普通无文字 选中出现文字 } public static final int DEFAULT_UNCHECKED_IMAGE = R.drawable.ic_android_black_24dp; public static final int DEFAULT_CHECKED_IMAGE = R.drawable.ic_android_green_24dp; public static final int DEFAULT_CHECKED_TEXT = 0xff138768; public static final int DEFAULT_UNCHECKED_TEXT = 0xff747575; NavShowType type = NavShowType.NORMAL; public void setType(NavShowType type) { this.type = type; boolean tv = true, iv = true; switch (type) { case NORMAL: tv = true; iv = true; break; case NO_IMAGE: tv = true; iv = false; break; case NO_TEXT: tv = false; iv = true; break; case CHECKED_SHOW_TEXT: tv = false; iv = true; } for (NavigationItem item : items) { item.setTextVisible(tv); item.setImageVisible(iv); item.showView(); } if (type == NavShowType.CHECKED_SHOW_TEXT) { items.get(currentChecked).setTextVisible(true); } } //当前被选中的元素 int currentChecked = 0; public BottomNavigationBar(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BottomNavigationBar); String t = a.getString(R.styleable.BottomNavigationBar_show_type); if (t == null) { setType(NavShowType.NORMAL); } else { if (t.equals("1")) { setType(NavShowType.NO_TEXT); } else if (t.equals("2")) { setType(NavShowType.NO_IMAGE); } else if (t.equals("3")) { setType(NavShowType.CHECKED_SHOW_TEXT); } else if (t.equals("0")) { setType(NavShowType.NORMAL); } } textCheckdColor = a.getColor(R.styleable.BottomNavigationBar_textCheckedColor, textCheckdColor); textUnCheckColor = a.getColor(R.styleable.BottomNavigationBar_textUnCheckedColor, textUnCheckColor); a.recycle(); } public BottomNavigationBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } public void addItemView(final NavigationItem item) { boolean tv = true, iv = true; switch (type) { case NORMAL: tv = true; iv = true; break; case NO_IMAGE: tv = true; iv = false; break; case NO_TEXT: tv = false; iv = true; break; case CHECKED_SHOW_TEXT: tv = false; iv = true; } item.setTextVisible(tv); item.setImageVisible(iv); item.setTextCheckedColor(textCheckdColor); item.setTextUnCheckedColor(textUnCheckColor); items.add(item); final int i = items.size() - 1; item.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //这个item在集合中的第几个 int index = i; //如果不是当前的view boolean noChecked = index != currentChecked; if (noChecked) { //改变上一个view items.get(currentChecked).changeView(); if (type == NavShowType.CHECKED_SHOW_TEXT) { items.get(currentChecked).setTextVisible(false); } currentChecked = index; if (onItemViewSelectedListener != null) { onItemViewSelectedListener.onItemClcik(v, index); } //改变当前点击的view item.changeView(); if (type == NavShowType.CHECKED_SHOW_TEXT) { item.setTextVisible(true); } } } }); //Item 之间的分割线 // TextView tv_line = new TextView(context); // LayoutParams lineParams = new LayoutParams(2, ViewGroup.LayoutParams.MATCH_PARENT); // c29f lineParams.setMargins(0, 15, 0, 15); // tv_line.setLayoutParams(lineParams); // tv_line.setBackgroundColor(getResources().getColor(R.color.color_dcdcdc)); // addView(item); // addView(tv_line); if (items.size() == 1) { item.changeView(); } item.showView(); } int textCheckdColor = DEFAULT_CHECKED_TEXT; int textUnCheckColor = DEFAULT_UNCHECKED_TEXT; /** * 增加 * * @param text */ public void addItemView(String text, int textCheckdColor, int textUnCheckColor, int imageCheckedResource, int imageUnCheckedResource) { NavigationItem item = new NavigationItem(getContext(), textCheckdColor, textUnCheckColor, imageCheckedResource, imageUnCheckedResource); item.getmTextView().setText(text); addItemView(item); } public void setTextColor(int checkedColor, int unCheckedColor) { this.textCheckdColor = checkedColor; this.textUnCheckColor = unCheckedColor; for (NavigationItem item : items) { item.setTextCheckedColor(checkedColor); item.setTextUnCheckedColor(unCheckedColor); item.showView(); } } public void setTextColor(int checkedColor) { this.textCheckdColor = checkedColor; for (NavigationItem item : items) { item.setTextCheckedColor(checkedColor); item.setTextUnCheckedColor(textUnCheckColor); item.showView(); } } public void setTextColorRes(int checkedColorId, int unCheckedColorId) { setTextColor(getResources().getColor(checkedColorId), getResources().getColor(unCheckedColorId)); } public void setTextColorRes(int checkedColorId) { setTextColor(getResources().getColor(checkedColorId)); } // public void addItemView(String text) { // NavigationItem item = new NavigationItem(getContext(), textCheckdColor, textUnCheckColor, DEFAULT_CHECKED_IMAGE, DEFAULT_UNCHECKED_IMAGE); // item.getmTextView().setText(text); // addItemView(item); // } public void addItemView(String text, int imageCheckedResource, int imageUnCheckedResource) { addItemView(text, textCheckdColor, textUnCheckColor, imageCheckedResource, imageUnCheckedResource); } OnItemViewSelectedListener onItemViewSelectedListener; public void setOnItemViewSelectedListener(OnItemViewSelectedListener onItemViewSelectedListener) { this.onItemViewSelectedListener = onItemViewSelectedListener; } //item列表 ArrayList<NavigationItem> items = new ArrayList<>(); public interface OnItemViewSelectedListener { void onItemClcik(View v, int index); } Activity中的代码,其他省略 /** * 底部导航栏添加图标 */ private FragmentManager fm; private static Fragment homePagerFragment;//本页Fragment private static Fragment myOrderFragment;//第二个Fragment private void initView() { navBar.addItemView(getResources().getString(R.string.底部字), R.mipmap.图1, R.mipmap.图1); //有几个添加几个navBar.addItem(); fm = getSupportFragmentManager(); initListener(); } /** * 初始化第一个fragment */ private void initFragment() { loadFragment(0); } /** * 点击 */ private void initListener() { navBar.setOnItemViewSelectedListener(new BottomNavigationBar.OnItemViewSelectedListener() { @Override public void onItemClcik(View v, int index) { loadFragment(index); } }); } /** * 底部图标切换页面 * @param index */ private void loadFragment(int index) { ft = fm.beginTransaction(); if (index == 0) { if (homePagerFragment == null) { homePagerFragment = 本页Fragment.newInstance(getResources().getString(R.string.底部字)); } ft.replace(R.id.container, homePagerFragment); } if (index == 1) { if (myOrderFragment == null) { myOrderFragment = 第二个Fragment.newInstance(); } //有几个页面添加几个 ft.commit(); }
相关文章推荐
- 底部放5个RadioButton,点击切换不同的Fragment
- Android底部导航栏点击切换变色
- 安卓APP底部导航栏(有消息圆点指示器),实现fragment切换(eclipse)
- android 点击底部图片进行切换不同的Fragment
- 安卓中点击不同按钮切换不同到Fragment
- 安卓点击和滑动切换页面FragmentActivity
- 底部导航栏点击跳转fragment
- FragmentController实现项目中底部导航栏切换的UI框架
- android安卓 Fragment实现页面标题左右同步切换以及点击标题切换页面
- 使用选项卡TabWidget与TabHost实现底部或者顶部四个分类按钮 点击切换对应的fragment
- 安卓常见布局:RadioButton配合Fragment实现底部菜单栏切换
- Android之点击按钮切换不同的Fragment
- Android 安卓 fragment+viewpager 仿qq界面 实现点击菜单切换界面+滑动切换viewpager切换界面
- FragmentTabHost、Fragment实现底部点击切换Tab页面
- 关于fragmentTabhost点击底部切换不了的问题
- 【Android】安卓开发实战之使用Fragment(碎片)实现底部导航栏效果
- 安卓开发--应用市场的界面制作(一)--viewpager+fragment实现可滑动的底部导航栏
- 底部导航栏实现页面的切换(二):Fragment + RadioGroup
- Android Fragment应用实战:点击底部栏不懂选项弹不同Fragment实例
- FragmentTabhost底部Fragment点击切换按钮集成