页面切换显示横线

页面切换显示横线_第1张图片

1.布局


        
        
    

2.Activity代码

public class DataFragment extends Fragment {

    private TabLayout data_tl_tablayout;
    private ViewPager data_vp_viewpager;
    private List mFragments;
    private DataFragmentPageAdapter mDataFragmentPageAdapter;
    boolean flag;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_data, container, false);
        initView(inflate);
        initTabLayout();
        return inflate;
    }

    private void initTabLayout() {
        mFragments = new ArrayList<>();
        mFragments.add(new VideoDataFragment());
        mFragments.add(new PersonalDataFragment());
        mDataFragmentPageAdapter = new DataFragmentPageAdapter(getChildFragmentManager());
        mDataFragmentPageAdapter.setFragments(mFragments);
        data_vp_viewpager.setAdapter(mDataFragmentPageAdapter);

        data_tl_tablayout.addTab(data_tl_tablayout.newTab());
        data_tl_tablayout.addTab(data_tl_tablayout.newTab());

        data_tl_tablayout.setupWithViewPager(data_vp_viewpager);

        data_tl_tablayout.getTabAt(0).setCustomView(getTabView("视频数据",flag = true,"#2bc17a"));
        data_tl_tablayout.getTabAt(1).setCustomView(getTabView("用户数据",flag = false,"#000000"));
        data_tl_tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                data_vp_viewpager.setCurrentItem(tab.getPosition());
                changeTabSelect(tab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabNormal(tab);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void changeTabNormal(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView tab_icon = (TextView) view.findViewById(R.id.tab_icon);
        TextView tab_title = (TextView) view.findViewById(R.id.tab_title);
        tab_title.setTextColor(getResources().getColor(R.color.colorBluck));
        if (tab_title.getText().toString().equals("视频数据")) {
            tab_icon.setVisibility(View.INVISIBLE);
        } else{
            tab_icon.setVisibility(View.INVISIBLE);
        }
    }

    private void changeTabSelect(TabLayout.Tab tab) {
        View view = tab.getCustomView();
        TextView tab_icon = (TextView) view.findViewById(R.id.tab_icon);
        TextView tab_title = (TextView) view.findViewById(R.id.tab_title);
        tab_title.setTextColor(getResources().getColor(R.color.colorGreen));
        if (tab_title.getText().toString().equals("视频数据")) {
            tab_icon.setVisibility(View.VISIBLE);
        } else{
            tab_icon.setVisibility(View.VISIBLE);
        }
    }


    public View getTabView(String title,boolean flag,String color) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_icon, null);
        TextView tab_title = (TextView) v.findViewById(R.id.tab_title);
        tab_title.setText(title);
        tab_title.setTextColor(Color.parseColor(color));
        TextView tab_icon = (TextView) v.findViewById(R.id.tab_icon);
        if (flag){
            tab_icon.setVisibility(View.VISIBLE);
        }else {
            tab_icon.setVisibility(View.INVISIBLE);
        }
        return v;
    }

    private void initView(View inflate) {
        data_tl_tablayout = (TabLayout) inflate.findViewById(R.id.data_tl_tablayout);
        data_vp_viewpager = (ViewPager) inflate.findViewById(R.id.data_vp_viewpager);
    }
}

3:按钮布局






    
    


4.横线背景:



    
    

你可能感兴趣的:(页面切换显示横线)