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); // 系统通知
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));
}
}
}