Home

Awesome

FragmentTabhostUtils


开源地址:https://github.com/open-android/FragmentTabhostUtils

这是一个封装了android常用的底部导航栏+fragment的库,用这个库,我们可以几行代码就搞定大多数APP的主界面的布局

image image image image

黑马助手.png

使用步骤

1. 在project的build.gradle添加如下代码(如下图)

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

Sync项目以后,引入这个库就成功了~~~

2. 在Module的build.gradle添加依赖

compile 'com.github.open-android:FragmentTabhostUtils:0.5.0'

3. 在xml中,加入以下代码:

  <com.heima.tabview.library.TabView
        android:id="@+id/tabView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
  </com.heima.tabview.library.TabView>

4. 在项目当中添加如下代码:

    List<TabViewChild> tabViewChildList=new ArrayList<>();
    TabViewChild tabViewChild01=new TabViewChild(R.drawable.tab01_sel,R.drawable.tab01_unsel,"首页",  FragmentCommon.newInstance());
    TabViewChild tabViewChild02=new TabViewChild(R.drawable.tab02_sel,R.drawable.tab02_unsel,"分类",  FragmentCommon.newInstance());
    TabViewChild tabViewChild03=new TabViewChild(R.drawable.tab03_sel,R.drawable.tab03_unsel,"资讯",  FragmentCommon.newInstance());
    TabViewChild tabViewChild04=new TabViewChild(R.drawable.tab04_sel,R.drawable.tab04_unsel,"购物车",FragmentCommon.newInstance());
    TabViewChild tabViewChild05=new TabViewChild(R.drawable.tab05_sel,R.drawable.tab05_unsel,"我的",  FragmentCommon.newInstance());
    tabViewChildList.add(tabViewChild01);
    tabViewChildList.add(tabViewChild02);
    tabViewChildList.add(tabViewChild03);
    tabViewChildList.add(tabViewChild04);
    tabViewChildList.add(tabViewChild05);

是不是看起来感觉好复杂?其实就是new了5个实体类,把这5个实体类添加到了一个集合里,我们最终想要的是tabViewChildList这个集合,每个实体类的构造方法有四个参数:

5 为TabView设置数据源:

TabView tabView= (TabView) findViewById(R.id.tabView);
tabView.setTabViewChild(tabViewChildList,getSupportFragmentManager());
tabView.setOnTabChildClickListener(new TabView.OnTabChildClickListener() {
        @Override
        public void onTabChildClick(int  position, ImageView currentImageIcon, TextView currentTextView) {
             Toast.makeText(getApplicationContext(),"position:"+position,Toast.LENGTH_SHORT).show();
        }
    });

当然,还能自定义很多属性

在JAVA代码里自定义

    tabView.setTextViewSelectedColor(Color.BLUE);
    tabView.setTextViewUnSelectedColor(Color.BLACK);
    tabView.setTabViewBackgroundColor(Color.YELLOW);
    tabView.setTabViewHeight(dip2px(52));
    tabView.setImageViewTextViewMargin(2);
    tabView.setTextViewSize(14);
    tabView.setImageViewWidth(dip2px(30));
    tabView.setImageViewHeight(dip2px(30));
    tabView.setTabViewGravity(Gravity.TOP);
    tabView.setTabViewDefaultPosition(2);

在xml中自定义:

 <com.heima.tabview.library.TabView
        android:id="@+id/tabView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:tab_textViewSelColor="#ff0000"
        app:tab_textViewUnSelColor="#00ff00"
        app:tab_tabViewBackgroundColor="#0000ff"
        app:imageViewTextViewMargin="2dip"
        app:tab_imageViewHeight="30dip"
        app:tab_imageViewWidth="30dip"
        app:tab_tabViewDefaultPosition="1"
        app:tab_tabViewGravity="right"
        app:tab_tabViewHeight="52dip"
        app:tab_textViewSize="14sp"
        >
    </com.heima.tabview.library.TabView>