自定義TabLayout item 布局

adapter

public class TabViewPagerAdapter extends FragmentPagerAdapter {


    private List<String> mTabList;
    private List<Fragment> mListFragment;
    private Context mContext;

    public TabViewPagerAdapter(FragmentManager fm,
                               @NonNull Context context,
                               @NonNull List<Fragment> listFragment) {
        super(fm);
        checkNotNull(context);
        mContext = context;
        mListFragment = checkNotNull(listFragment);
        mTabList = new ArrayList<>();
        mTabList.add(context.getResources().getString(R.string.system_msg_icon));
        mTabList.add(context.getResources().getString(R.string.activity_msg_icon));
        mTabList.add(context.getResources().getString(R.string.scene_msg_icon));
    }

    @Override
    public Fragment getItem(int position) {
        return mListFragment.get(position);
    }

    @Override
    public int getCount() {
        return mListFragment.size();
    }

    /**
     * 初始化布局顯示
     */
    public View getTabView(int position) {
        Typeface customFont = Typeface.createFromAsset(mContext.getAssets(), "Netfits-Android.ttf");
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.tab_layout, null);
        TextView mMsgTypeTextView = (TextView) inflate.findViewById(R.id.msg_type);
        mMsgTypeTextView.setTypeface(customFont);
        mMsgTypeTextView.setText(mTabList.get(position));
        return inflate;
    }
}



activity

private void initUiView() {

        AppCompatActivity msgCenterFrg = (AppCompatActivity) getActivity();
        msgCenterFrg.setSupportActionBar(mMsgCenterToolBar);
        ActionBar supportActionBar = msgCenterFrg.getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayHomeAsUpEnabled(true);
            supportActionBar.setDisplayShowTitleEnabled(false);
            mMsgCenterToolBar.setBackgroundColor(getActivity().getResources().getColor(R.color.msg_system_bg_color));
            mMsgCenterToolBar.setTitle(getActivity().getResources().getString(R.string.message_page_tool_bar));
            mMsgCenterToolBar.setTitleTextColor(getActivity().getResources().getColor(R.color.white));
            mMsgCenterToolBar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // back 
                }
            });
        }
        List<Fragment> mListFragment = new ArrayList<>();
        mScenesMsgFragment = new ScenesMsgFragment();
        mActivityMsgFragment = new ActivityMsgFragment();
        mSystemMsgFragment = new SystemMsgFragment();
        // 順序固定,不可更改
        mListFragment.add(mScenesMsgFragment);// 場景推薦
        mListFragment.add(mActivityMsgFragment); // 活動通知
        mListFragment.add(mSystemMsgFragment); // 系統(tǒng)通知
        mMsgCenterTableLayout.setTabMode(TabLayout.MODE_FIXED);
        mMsgCenterTableLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.msg_system_bg_color));
        mTabViewPagerAdapter = new TabViewPagerAdapter(getFragmentManager(), getActivity(), mListFragment);
        mMsgCenterViewPager.setAdapter(mTabViewPagerAdapter);
        mMsgCenterTableLayout.setupWithViewPager(mMsgCenterViewPager);
        for (int i = 0; i < mMsgCenterTableLayout.getTabCount(); i++) {
            TabLayout.Tab tabAt = mMsgCenterTableLayout.getTabAt(i);
            if (tabAt != null) {
                tabAt.setCustomView(mTabViewPagerAdapter.getTabView(i));
            }
        }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 首先看一下最終要實(shí)現(xiàn)的效果: 一般我們使用TabLayout都默認(rèn)文字布局,比較單一。為了能靈活應(yīng)對產(chǎn)品的各種需求...
    MrAllRight_Liu閱讀 10,386評論 2 17
  • 導(dǎo)言: 很多人都喜歡使用安卓的listview進(jìn)行列表式的展示,然而列表項(xiàng)的內(nèi)容往往不是一個單一的textview...
    修羅的小熊閱讀 16,373評論 2 3
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,281評論 25 708
  • 今天來分享下做導(dǎo)航欄的另外一種方法,導(dǎo)航欄可以放在頂部,也可以放在底部,之前分享過一片底部導(dǎo)航欄的實(shí)現(xiàn)方式一行代碼...
    juexingzhe閱讀 807評論 0 9
  • 再次打開簡書,發(fā)現(xiàn)一不小心差不多一年就這樣過去了。又是一年秋,又是一個雨天,想來雨天和書寫是很配的??去年的迷茫如今...
    懶小仙閱讀 246評論 0 1

友情鏈接更多精彩內(nèi)容