快速實現自定義Tab複用

當然的應用太多Tab+Fragment.
實現方式多種多樣:
TabLayout+Fragment
常規佈局+Frament
FragmentTabHost+Fragment

然後遇到特殊的Tab,比如一個Tab含有右上角有一個數字提醒或者Label,可能需要自行佈局才能滿足需求。爲了方便以後的複用,使用自定義的Tab來滿足需求。

TabInfo:

public class TabInfo {
    private int count;//Tab的個數 必選
    private int currentTab;//默認選中的tab 必選
    private String[] tabText;//文字必選


    private int normalResId;//可選
    private int selectResId;//可選
    private int normalTextColor;//可選
    private int selectTextColor;//可選
    private int normalTextSizeSp;//可選
    private int selectTextSizeSp;//可選
    }

CustomTabLayout:
繼承LinearLayout,外部傳入佈局,內部控制imageview,textview的顯示狀態與點擊回調

public void addTab(List<TabInfo> tabInfos, int layout) {
        this.tabInfos = tabInfos;
        tabLayout = layout;
        tabLayout = getItemLayout(tabLayout);
        if (tabLayout <= 0) {
            return;
        }
        for (int i = 0; i < tabInfos.size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(tabLayout, null);
            RelativeLayout itemLayout = (RelativeLayout) view.findViewById(R.id.custom_tablayout_item);
            itemLayout.setGravity(Gravity.CENTER);
            LayoutParams layoutParams = new LayoutParams(0, ViewGroup.LayoutParams.FILL_PARENT, 1.0f);
            itemLayout.setLayoutParams(layoutParams);

            TextView textView = (TextView) view.findViewById(R.id.custom_tablayout_text);
            ImageView imageView = (ImageView) view.findViewById(R.id.custom_tablayout_image);

            imageViews.add(imageView);
            textViews.add(textView);
            //設置文字
            textView.setText(tabInfos.get(i).getTabText()[i]);
            if (position >= tabInfos.size() || position < 0) {
                position = 0;
            } else {
                position = tabInfos.get(i).getCurrentTab();
            }
            //選中的圖片和顏色
            if (i == tabInfos.get(i).getCurrentTab()) {
                setTextSizeofSp(textView, tabInfos.get(i).getSelectTextSizeSp());
                textView.setTextColor(tabInfos.get(i).getSelectTextColor());
                imageView.setImageResource(tabInfos.get(i).getSelectResId());
            } else {
                setTextSizeofSp(textView, tabInfos.get(i).getNormalTextSizeSp());
                textView.setTextColor(tabInfos.get(i).getNormalTextColor());
                imageView.setImageResource(tabInfos.get(i).getNormalResId());
            }
            addView(view);


            final int finalI = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelectTab(finalI);
                    onTabOnclickListener.onTabClick(finalI, imageViews.get(finalI), textViews.get(finalI));
                }
            });

        }


    }

外部使用:

 customTabLayout = (CustomTabLayout) findViewById(R.id.custom_tablayout);
        //strs:文字描述  5:有5個Tab  0:默認選中的是第一個
        TabInfo.TabInfoBuilder tabInfoBuilder = new TabInfo.TabInfoBuilder(strs,count, 0);
        List<TabInfo> tabInfos = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            TabInfo tabInfo = tabInfoBuilder
                    .setNormalResId(normalResId[i])
                    .setSelectResId(selectResId[i])
                    .setNormalTextSizeSp(12)
                    .setSelectTextSizeSp(12)
                    .setNormalTextColor(getResources().getColor(R.color.colorAccent))
                    .setSelectTextColor(getResources().getColor(R.color.colorPrimaryDark))
                    .build();
            tabInfos.add(tabInfo);
        }
        customTabLayout.addTab(tabInfos,R.layout.tablayout_item);//代碼設置佈局
//        customTabLayout.addTab(tabInfos); xml設置佈局調用方式

代碼示例:https://github.com/Allure0/QuickAndroid/tree/master

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章