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"?>                            

更多相关文章

  1. Android动态添加布局
  2. 解决Android Studio XML布局格式化无法换行的问题
  3. Android 的res/values/colors自定义颜色列表和注释表及布局文件
  4. Android线性LinearLayout布局xml属性介绍
  5. Unexpected namespace prefix "xmlns" found for tag LinearLayo
  6. android在布局中动态增加view时的层级控制
  7. Android 总结:打造Android中的流式布局和热门标签(源码有详细注释)
  8. android RecyclerView布局真的只是那么简单!
  9. Android简单自定义布局介绍

随机推荐

  1. 2021年11月9日作业1
  2. 樱花怎么画?超详细樱花画法步骤!
  3. 用户注册作业
  4. 2021.11.9 作业
  5. 大前端第二天第一个作业
  6. HTML 练习
  7. Vue介绍及Vue脚手架安装
  8. 11月9日 走进HTML的世界
  9. ES6基本语法,Axios异步网络请求介绍
  10. 前端基础,HTML5常用标签学习总结