底部tab按鈕 工具類 BootomTabUtils
阿新 • • 發佈:2019-01-29
底部tab 工具類 BootomTabUtils
效果:
使用:
//建立底部tab String[] text_bottom = {"主頁", "View", "進階", "我的直學"}; int[] seled = {R.mipmap.main_nav_icon1_select, R.mipmap.main_nav_icon2_select_temp, R.mipmap.main_nav_icon3_select_temp, R.mipmap.main_nav_icon4_select}; int[] disseled = {R.mipmap.main_nav_icon1_unselect, R.mipmap.main_nav_icon2_unselect_temp, R.mipmap.main_nav_icon3_unselect_temp, R.mipmap.main_nav_icon4_unselect}; BootomTabUtils bootomTabUtils = new BootomTabUtils(); bootomTabUtils.editBootomView(tilebar_view, this, seled, disseled, text_bottom, 0); bootomTabUtils.listenerClick(new BootomTabUtils.Listener() { @Override public void clickTab1(View view, int tabIndex, String tabTest) { FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.frame, home_fragment).commit(); } @Override public void clickTab2(View view, int tabIndex, String tabTest) { FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); catory_fragment = new CatoryFragment(); fragmentTransaction.replace(R.id.frame, catory_fragment).commit(); hideTitleBar(); } @Override public void clickTab3(View view, int tabIndex, String tabTest) { FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); liveFragment = new LiveFragment(); fragmentTransaction.replace(R.id.frame, liveFragment).commit(); } @Override public void clickTab4(View view, int tabIndex, String tabTest) { FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); userFragment = new UserFragment(); fragmentTransaction.replace(R.id.frame, userFragment).commit(); } });
實現:
/* * Created by 劉國 on 2018/7/30. * 底部tab封裝類 */ public class BootomTabUtils { private String LOG = "BootomTabUtils_Log"; private LinearLayout bottom_tab; public BootomTabUtils editBootomView(LinearLayout view, Activity context, int[] selectedIcon, int[] disSelectedIcon, String[] text_bottom, int defaultIndex) { if ((selectedIcon.length == 4) && (disSelectedIcon.length == 4) && (text_bottom.length == 4)) { bottom_tab = new LinearLayout(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); bottom_tab.setOrientation(LinearLayout.HORIZONTAL); bottom_tab.setLayoutParams(layoutParams); // bottom_tab.setBackgroundColor(Color.parseColor("#EAEBEC")); bottom_tab.setBackgroundColor(Color.parseColor("#ffffff")); for (int i = 0; i < selectedIcon.length; i++) { LinearLayout ll = new LinearLayout(context); LinearLayout.LayoutParams ll_layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); ll_layoutParams.weight = 1f; ll_layoutParams.setMargins(2, 12, 2, 2); ll.setOrientation(LinearLayout.VERTICAL); ll.setLayoutParams(ll_layoutParams); ImageView imageView = new ImageView(context); TextView textView = new TextView(context); textView.setText(text_bottom[i]); ll.addView(imageView); ll.addView(textView, ll_layoutParams); textView.setGravity(Gravity.CENTER); if (i == defaultIndex) { imageView.setImageResource(selectedIcon[i]); textView.setTextColor(Color.parseColor("#FE671E")); } else { imageView.setImageResource(disSelectedIcon[i]); } bottom_tab.addView(ll, i); } addBootomView(view, context); addClickListener(bottom_tab, text_bottom, selectedIcon, disSelectedIcon, context); } else { Log.e(LOG, "ERROR:您editBootomView方法入參的陣列長度不相等"); return this; } return this; } /** * 做點選ui改變 * @param index * @param selectedIcon * @param disSelectedIcon */ private void doforView(int index, int[] selectedIcon, int[] disSelectedIcon) { int childCount = bottom_tab.getChildCount(); for (int i = 0; i < childCount; i++) { LinearLayout ll = (LinearLayout) bottom_tab.getChildAt(i); if (index == i) { ImageView imageView = (ImageView) ll.getChildAt(0); TextView textView = (TextView) ll.getChildAt(1); imageView.setImageResource(selectedIcon[index]); textView.setTextColor(Color.parseColor("#FE671E")); } else { ImageView imageView = (ImageView) ll.getChildAt(0); TextView textView = (TextView) ll.getChildAt(1); imageView.setImageResource(disSelectedIcon[i]); textView.setTextColor(Color.parseColor("#8A000000")); } } } /** * 新增監聽 * * @param bottom_tab * @param text_bottom * @param selectedIcon * @param disSelectedIcon * @param context */ private void addClickListener(LinearLayout bottom_tab, final String[] text_bottom, final int[] selectedIcon, final int[] disSelectedIcon, final Activity context) { int childCount = bottom_tab.getChildCount(); for (int i = 0; i < childCount; i++) { View childAt = bottom_tab.getChildAt(i); final int finalI = i; childAt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { switch (finalI) { case 0: doforView(0, selectedIcon, disSelectedIcon); listener.clickTab1(view, finalI, text_bottom[finalI]); break; case 1: doforView(1, selectedIcon, disSelectedIcon); listener.clickTab2(view, finalI, text_bottom[finalI]); break; case 2: doforView(2, selectedIcon, disSelectedIcon); listener.clickTab3(view, finalI, text_bottom[finalI]); break; case 3: doforView(3, selectedIcon, disSelectedIcon); listener.clickTab4(view, finalI, text_bottom[finalI]); break; } } }); } } /** * 新增底部tab */ public BootomTabUtils addBootomView(LinearLayout view, Activity context) { if (bottom_tab != null) { RelativeLayout linearLayout = new RelativeLayout(context); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);//兩個引數分別是layout_width,layout_height layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); linearLayout.setLayoutParams(layoutParams); linearLayout.addView(view); linearLayout.addView(bottom_tab, layoutParams); context.setContentView(linearLayout); } else { Log.e(LOG, "ERROR:editBootomView方法要在addBootomView方法前呼叫"); } return this; } public Listener listener; public interface Listener { public abstract void clickTab1(View view, int tabIndex, String tabTest); public abstract void clickTab2(View view, int tabIndex, String tabTest); public abstract void clickTab3(View view, int tabIndex, String tabTest); public abstract void clickTab4(View view, int tabIndex, String tabTest); } public void listenerClick(Listener listener) { this.listener = listener; } }