自定义TabHost的实现:
public class MyTabHost { int selectColor = Color.parseColor("#029FFF"); int unselectColor = Color.parseColor("#029FCC"); LinearLayout tabLayout; FrameLayout contentLayout; Context context; private List<MyTab> tabs = new LinkedList<MyTab>(); public MyTabHost(Context context) { this.context = context; } public void setTabColor(int selectColor, int unselectColor) { this.selectColor = selectColor; this.unselectColor = unselectColor; } public LinearLayout getTabLayout() { return tabLayout; } public void setTabLayout(LinearLayout tabLayout) { this.tabLayout = tabLayout; } public void setContentLayout(FrameLayout contentLayout) { this.contentLayout = contentLayout; } public FrameLayout getContentLayout() { return contentLayout; } private Button newButton(String text) { Button button = new Button(context); button.setTextColor(Color.BLACK); button.setTextSize(20); button.setPadding(10, 5, 10, 5); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); params.setMarginEnd(1); button.setLayoutParams(params); button.setGravity(Gravity.CENTER_VERTICAL); button.setBackground(null); button.setBackgroundColor(unselectColor); button.setText(text); return button; } /** * 获得tab数量 * @return */ public int getTabCount() { return tabs.size(); } /** * 删除一个Tab * * @param tab */ public void removeTab(MyTab tab) { tabs.remove(tab); } /** * 添加一个Tab * @param title * @return */ public MyTab addTab(String title) { final Button button = newButton(title); if(tabLayout != null) tabLayout.addView(button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int index = getIndexByButton(button); setCurrentTab(index); } }); button.setBackgroundColor(unselectColor); MyTab tab = new MyTab(button, false, null); tabs.add(tab); return tab; } /** * 添加一个Tab * * @param button * @param select * @param content * @return */ public MyTab addTab(final Button button, boolean select, Object content) { MyTab tab = new MyTab(button, select, content); tabs.add(tab); return tab; } /** * 获取当前选中的Tab * * @return */ public MyTab getCurrentTab() { for (MyTab tab : tabs) { if (tab != null && tab.isSelect()) return tab; } return null; } /** * 获取当前选中的Tab * * @return */ public MyTab getTabByButton(Button button) { if (button == null) return null; for (MyTab tab : tabs) { if (tab != null && tab.getButton() == button) return tab; } return null; } /** * 设置当前选中哪个Tab * * @param index */ public void setCurrentTab(int index) { if (index < 0 || index >= tabs.size()) return; for (int i = 0; i < tabs.size(); i++) { MyTab tab = tabs.get(i); if (i == index) { tab.setSelect(true); tab.getButton().setBackgroundColor(selectColor); contentLayout.getChildAt(index).setVisibility(View.VISIBLE); } else { tab.setSelect(false); tab.getButton().setBackgroundColor(unselectColor); contentLayout.getChildAt(i).setVisibility(View.INVISIBLE); } } } /** * 获取button对于的索引 * @param button * @return */ public int getIndexByButton(Button button) { if (button == null) return -1; for (int i = 0; i < tabs.size(); i++) { MyTab tab = tabs.get(i); if (tab != null && tab.getButton() == button) return i; } return -1; } /** * 设置当前选中哪个Tab * * @param index */ public void setCurrentTab(Button button) { if (button == null) return; for (MyTab tab : tabs) { if (tab != null && tab.getButton() == button) { tab.setSelect(true); tab.getButton().setBackgroundColor(selectColor); } else { tab.setSelect(false); tab.getButton().setBackgroundColor(unselectColor); } } } public static class MyTab { private Button button; private boolean select; private Object content; private View contentView; private MyTab(Button button, boolean select, Object content) { this.button = button; this.select = select; this.content = content; } public void setContentView(View contentView) { this.contentView = contentView; } public View getContentView() { return contentView; } public Button getButton() { return button; } public void setButton(Button button) { this.button = button; } public boolean isSelect() { return select; } public void setSelect(boolean select) { this.select = select; } public Object getContent() { return content; } public void setContent(Object content) { this.content = content; } } }
自定义TabHost的应用:
public class RecommandFragment extends Fragment { String TAG = "RecommandFragment"; LinearLayout tabLayout; FrameLayout contentLayout; Context context; int unselectColor; int selectColor; MyTabHost tabHost; int COLUMN_COUNT = 4; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getActivity(); selectColor = Color.parseColor("#029FFF"); unselectColor = Color.parseColor("#DDDDDD"); tabHost = new MyTabHost(context); tabHost.setTabColor(selectColor, unselectColor); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_recommand, container, false); tabLayout = (LinearLayout)view.findViewById(R.id.tabLayout); tabLayout.setGravity(Gravity.CENTER_VERTICAL); tabHost.setTabLayout(tabLayout); contentLayout = (FrameLayout)view.findViewById(R.id.contentLayout); tabHost.setContentLayout(contentLayout); return view; } void addTab(VideoGroupData data) { MyTab tab = tabHost.addTab(data.getTitle()); tab.setContent(data); List<VideoItemData> videoItemList = data.getData(); int n = videoItemList.size(); List<VideoItemData> dataList = CommonUtils.getFirstNItems(videoItemList, n); VideoGridAdapter videoAdapter = new VideoGridAdapter(context, dataList, false); FixedGridLayout fixGridLayout = new FixedGridLayout(context); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); fixGridLayout.setLayoutParams(params); int fragmentMeasuredWidth = getView().getMeasuredWidth(); int fragmentWidth = getView().getWidth(); Logger.i(TAG, "fragmentMeasuredWidth:"+fragmentMeasuredWidth); Logger.i(TAG, "fragmentWidth:"+fragmentWidth); fixGridLayout.setRowParams(fragmentMeasuredWidth / COLUMN_COUNT, COLUMN_COUNT); fixGridLayout.setAdapter(videoAdapter); ScrollView scrollView = new ScrollView(context); params = new LayoutParams(LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT); scrollView.setLayoutParams(params); scrollView.addView(fixGridLayout); tabHost.getContentLayout().addView(scrollView); tabHost.setCurrentTab(tabHost.getTabCount() - 1); } }
布局文件fragment_recommend.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF00FF" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#FFFFFF" android:padding="1dp" > <ImageButton android:id="@+id/btnHide" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="@null" android:padding="5dp" android:scaleType="fitXY" android:src="@drawable/hide" /> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_toLeftOf="@id/btnHide" android:scrollbars="none" > <LinearLayout android:id="@+id/tabLayout" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > </LinearLayout> </HorizontalScrollView> </RelativeLayout> <FrameLayout android:id="@+id/contentLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> </LinearLayout>