Fragment封装切换
16lz
2021-01-26
这是一个封装了android常用的底部导航栏+fragment的库,用这个库,我们可以几行代码就搞定大多数APP的主界面的布局
使用步骤
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 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这个集合,每个实体类的构造方法有四个参数:
第一个参数:导航栏上面的某一个tab被点击时候,相应的切换的图片
第二个参数:导航栏上面的tab没有被被点击时候,相应的切换的图片
第三个参数:导航栏上面的某一个tab的文字显示
第四个参数:导航栏上面的某一个tab对应的Fragment对象,传进来就可以
**
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>
Demo下载
更多相关文章
- 编写高效Android代码
- android email 邮件转发 附件不能转发问题的解决
- android中onClick事件的实现
- [Android实例] android登录Web以及登录保持,cookie管理相关
- Android(安卓)Audio代码分析=Audio Strategy
- android中setOnClickListener的那点事
- android小特效(持续更新...)
- 【Android】 使用okhttp实现注册登录功能(与服务器端)
- Android第二个签名漏洞#9695860(The Second Master Key)的手动构