您的位置:首页 > 其它

安卓底部导航栏点击变色切换不同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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  安卓 导航栏