fragment 切换,导航栏是图标加文字

来源:互联网 发布:义乌淘宝美工学校 编辑:程序博客网 时间:2024/06/05 03:09

fragment 切换,导航栏是图标加文字

效果图

activity_main.xml

 <LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    //viewpager切换    <android.support.v4.view.ViewPager        android:id="@+id/view_pager"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"/>    <View        android:id="@+id/v_line"        android:layout_width="match_parent"        android:layout_height="2px"        android:background="@color/white"/>    <android.support.design.widget.TabLayout        android:id="@+id/tab_layout"        android:layout_width="match_parent"        android:layout_height="40dp"        app:tabBackground="@color/white"        app:tabIndicatorHeight="0dp"        app:tabSelectedTextColor="@color/colorPrimary"        app:tabTextColor="@color/black"/></LinearLayout>

java代码

tablayout_view

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical">    <ImageView        android:id="@+id/iv_icon"        android:layout_width="24dp"        android:layout_height="24dp" />    <TextView        android:id="@+id/tv_name"        android:layout_marginTop="5dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="12sp"/></LinearLayout> 

fragment切换使用的adapter

public class ViewPagerAdapter extends FragmentPagerAdapter {    private List<Fragment> fragmentList = new ArrayList<>();    public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {        super(fm);        this.fragmentList.clear();        this.fragmentList.addAll(fragmentList);    }    @Override    public Fragment getItem(int position) {        return fragmentList.get(position);    }    @Override    public int getCount() {        return fragmentList.size();    }}

MainActivity 使用时的代码

public class MainActivity extends FragmentActivity {    ViewPager viewPager;    TabLayout tabLayout;    TabLayout.Tab tab1;    TabLayout.Tab tab2;    TabLayout.Tab tab3;    private int num=0;    private List<Fragment> fragmentList = new ArrayList<>();    private int[] mTabCheck = new int[]{R.drawable.ic_book, R.drawable.ic_recommed, R.drawable.ic_user};    private int[] mTabUnCheck = new int[]{R.drawable.ic_un_book, R.drawable.ic_un_recommed, R.drawable.ic_un_user};    private String[] titles = new String[]{"书架","推荐","我的"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        viewPager = (ViewPager) findViewById(R.id.view_pager);        tabLayout = (TabLayout) findViewById(R.id.tab_layout);        //切换用到的fragment        fragmentList.add(new BookFragment());        fragmentList.add(new RecommedFragment());        fragmentList.add(new UserFragment());        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);        viewPager.setAdapter(viewPagerAdapter);        viewPager.setCurrentItem(0);        tabLayout.setupWithViewPager(viewPager);        tabLayout.setTabMode(TabLayout.MODE_FIXED);//设置TabLayout的模式        //设置导航栏        tab1 = tabLayout.getTabAt(0);        tab1.setCustomView(getTabView(0));        tab2 = tabLayout.getTabAt(1);        tab2.setCustomView(getUnTabView(1));        tab3 = tabLayout.getTabAt(2);        tab3.setCustomView(getUnTabView(2));        //切换监听事件        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {            @Override            public void onTabSelected(TabLayout.Tab tab) {                Select(tab);            }            @Override            public void onTabUnselected(TabLayout.Tab tab) {                UnSelect(tab);            }            @Override            public void onTabReselected(TabLayout.Tab tab) {            }        });//设置TabLayout的选中监听    }    public void Select(TabLayout.Tab tab) {        View v = tab.getCustomView();        v.findViewById(R.id.iv_icon).setBackgroundResource(mTabCheck[tab.getPosition()]);        TextView tv = (TextView) v.findViewById(R.id.tv_name);        tv.setTextColor(getResources().getColor(R.color.menu_select));    }    public void UnSelect(TabLayout.Tab tab) {        View v = tab.getCustomView();        v.findViewById(R.id.iv_icon).setBackgroundResource(mTabUnCheck[tab.getPosition()]);        TextView tv = (TextView) v.findViewById(R.id.tv_name);        tv.setTextColor(getResources().getColor(R.color.menu_unselect));    }    public View getTabView(int position) {        View view = LayoutInflater.from(this).inflate(R.layout.tablayout_view, null);        ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon);        TextView textView = (TextView) view.findViewById(R.id.tv_name);        imageView.setBackgroundResource(mTabCheck[position]);        textView.setText(titles[position]);        textView.setTextColor(getResources().getColor(R.color.menu_select));        return view;    }    public View getUnTabView(int position) {        View view = LayoutInflater.from(this).inflate(R.layout.tablayout_view, null);        ImageView imageView = (ImageView) view.findViewById(R.id.iv_icon);        TextView textView = (TextView) view.findViewById(R.id.tv_name);        imageView.setBackgroundResource(mTabUnCheck[position]);        textView.setText(titles[position]);        textView.setTextColor(getResources().getColor(R.color.menu_unselect));        return view;    }}
阅读全文
0 0
原创粉丝点击