TabLayout属性及自定义底部导航栏
16lz
2021-01-23
tablayout的xml布局
<?xml version="1.0" encoding="utf-8"?>
viewpager的adapter
public class TabLayoutFragmentAdapter extends FragmentPagerAdapter { private List mTabList; private Context mContext; private List mFragments; private ImageView mTabIcon; private TextView mTabText; private int[] mTabImgs; private LinearLayout mTabView; public TabLayoutFragmentAdapter(FragmentManager fm) { super(fm); } public TabLayoutFragmentAdapter(FragmentManager fm, List tabList, Context context, List fragments, int[] tabImgs) { super(fm); mTabList = tabList; mContext = context; mFragments = fragments; mTabImgs = tabImgs; } @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public int getCount() { return mTabList.size(); } @Override public CharSequence getPageTitle(int position) { return mTabList.get(position); } /** * set the tab view * * @param position * @return */ public View getTabView(int position) { View view = LayoutInflater.from(mContext).inflate(R.layout.layout_tab_view, null); mTabView = (LinearLayout) view.findViewById(R.id.ll_tab_view); mTabText = (TextView) view.findViewById(R.id.tv_tab_text); mTabIcon = (ImageView) view.findViewById(R.id.iv_tab_icon); mTabText.setText(mTabList.get(position)); mTabIcon.setImageResource(mTabImgs[position]); if (0 == position) {//the default color of item home is green mTabText.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary)); mTabIcon.setImageResource(R.drawable.home_fill); } return view; }}
Fragment
public class TabLayoutFragment extends Fragment implements TabLayout.OnTabSelectedListener { private ViewPager mViewPager; private TabLayout mTabLayout; private TextView mTextView; private List mTabList = new ArrayList<>(); private TabLayoutFragmentAdapter mAdapter; private int[] mTabImgs = new int[]{R.drawable.home, R.drawable.location, R.drawable.like, R.drawable.person}; private List mFragments = new ArrayList<>(); public static TabLayoutFragment newInstance(String s) { TabLayoutFragment tabLayoutFragment = new TabLayoutFragment(); Bundle bundle = new Bundle(); bundle.putString(Constants.ARGS, s); tabLayoutFragment.setArguments(bundle); return tabLayoutFragment; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_tablayout, container, false); mTextView = (TextView) view.findViewById(R.id.activity_text_view); Bundle bundle = getArguments(); if (bundle != null) { String s = bundle.getString(Constants.ARGS); if (!TextUtils.isEmpty(s)) { mTextView.setText(s); } } mViewPager = (ViewPager) view.findViewById(R.id.view_pager); mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout); initTabList(); mAdapter = new TabLayoutFragmentAdapter(getChildFragmentManager(), mTabList, getActivity(), mFragments, mTabImgs); mViewPager.setAdapter(mAdapter); mViewPager.setCurrentItem(0); mTabLayout.setupWithViewPager(mViewPager); mTabLayout.setTabMode(TabLayout.MODE_FIXED); for (int i = 0; i < mTabLayout.getTabCount(); i++) { mTabLayout.getTabAt(i).setCustomView(mAdapter.getTabView(i)); } mTabLayout.addOnTabSelectedListener(this);// mViewPager.setCurrentItem(0);// mTabLayout.getTabAt(0).setIcon(R.drawable.home);// mTabLayout.getTabAt(1).setIcon(R.drawable.location);// mTabLayout.getTabAt(2).setIcon(R.drawable.like);// mTabLayout.getTabAt(3).setIcon(R.drawable.person);// setDefaultFragment(); return view; } @Override public void onStart() { super.onStart(); initFragmentList(); } private void setDefaultFragment() { getChildFragmentManager().beginTransaction() .replace(R.id.sub_content, HomeFragment.newInstance(getString(R.string.item_home))) .commit(); } /** * add Fragment */ public void initFragmentList() { mFragments.clear(); mFragments.add(HomeFragment.newInstance(getString(R.string.item_home))); mFragments.add(LocationFragment.newInstance(getString(R.string.item_location))); mFragments.add(LikeFragment.newInstance(getString(R.string.item_like))); mFragments.add(PersonFragment.newInstance(getString(R.string.item_person))); } /** * init the tab list. */ private void initTabList() { mTabList.clear(); mTabList.add(getString(R.string.item_home)); mTabList.add(getString(R.string.item_location)); mTabList.add(getString(R.string.item_like)); mTabList.add(getString(R.string.item_person)); } @Override public void onTabSelected(TabLayout.Tab tab) { setTabSelectedState(tab); } @Override public void onTabUnselected(TabLayout.Tab tab) { setTabUnSelectedState(tab); } @Override public void onTabReselected(TabLayout.Tab tab) { } private void setTabSelectedState(TabLayout.Tab tab) { View customView = tab.getCustomView(); TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text); ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon); tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); String s = tabText.getText().toString(); if (getString(R.string.item_home).equals(s)) { tabIcon.setImageResource(R.drawable.home_fill); } else if (getString(R.string.item_location).equals(s)) { tabIcon.setImageResource(R.drawable.location_fill); } else if (getString(R.string.item_like).equals(s)) { tabIcon.setImageResource(R.drawable.like_fill); } else if (getString(R.string.item_person).equals(s)) { tabIcon.setImageResource(R.drawable.person_fill); } } private void setTabUnSelectedState(TabLayout.Tab tab) { View customView = tab.getCustomView(); TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text); ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon); tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.black_1)); String s = tabText.getText().toString(); if (getString(R.string.item_home).equals(s)) { tabIcon.setImageResource(R.drawable.home); } else if (getString(R.string.item_location).equals(s)) { tabIcon.setImageResource(R.drawable.location); } else if (getString(R.string.item_like).equals(s)) { tabIcon.setImageResource(R.drawable.like); } else if (getString(R.string.item_person).equals(s)) { tabIcon.setImageResource(R.drawable.person); } }}
Fragment的布局
<?xml version="1.0" encoding="utf-8"?>
更多相关文章
- Android动态添加布局
- 解决Android Studio XML布局格式化无法换行的问题
- Android 的res/values/colors自定义颜色列表和注释表及布局文件
- Android线性LinearLayout布局xml属性介绍
- Unexpected namespace prefix "xmlns" found for tag LinearLayo
- android在布局中动态增加view时的层级控制
- Android 总结:打造Android中的流式布局和热门标签(源码有详细注释)
- android RecyclerView布局真的只是那么简单!
- Android简单自定义布局介绍