android tab 实现方法

    今天总结一下tab实现的方法,以便以后查看。

    1、viewpager,实例化1个viewpager,要用一个list,和一个pagerAdapter,来进行实例化。如果想要监听滑动,就要设置view pager的pageronchang listener。里面有个select方法,切换完成,可以在这个方法里面处理。

     2、fragment。实现fragment,1,v4和android自带的,只能选1个,导包的时候,只导1个类型的,其余的删掉,以免发生不必要的异常,而且比较难调。2,调用hide方法隐藏fragment,调用show方法显示frgament。要想fragment和activity联系起来,要在activity布局里面写fame layout。调用add方法,有2个参数,第一个参数是fame layout的id,第二个参数是要添加进去的fragment,必须要调add方法,否则,fragment不会显示在界面上。

    在这里说一下fragment和viewpager实现tab的区别。1、viewpager,不管有几个tab必须在activity中实现。而且fragment不同,是分开写的,有利于代码的维护,而viewpager会使activity代码过多,不容易维护,简洁性不好。2、fragment在处理不同屏幕尺寸的时候比view pager有得天独厚的优势,官方也推荐使用fragment。

    实现fragment步骤

    1、在xml里面写个布局,确定fragment替换view的位置。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/llview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"></LinearLayout>
</LinearLayout>
注意:fragment是用来显示view的,不一定要写1个framelayout,可以是1个线性布局。不过,推荐使用framelayout,出错率较低。

2、使用fragemnt有2种方式,一种方式是在布局里直接写,另一种方式是动态添加,作为专业程序员,必须要掌握动态添加的方法。

/*** fragment */
    MenuFragment mMenuFragment = new MenuFragment();
    ArroundCarFragment mArroundCarFragment = new ArroundCarFragment();
    GoodsFragment mGoodsFragment = new GoodsFragment();
    
    FragmentManager fragmentManager;
fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.llview, mArroundCarFragment);
        fragmentTransaction.add(R.id.llview, mGoodsFragment);
        fragmentTransaction.commit();
添加后,要显示哪个fragment,就把其他的隐藏调,通过hide方法和show方法,如下所示

fragmentManager.beginTransaction().hide(mGoodsFragment).show(mArroundCarFragment).commitAllowingStateLoss();
为什么这里不用上面fragmentTransaction变量的commit方法?因为我的activity继承了FragmentActivity,所以commit方法只能调用1次,否则会报错。所以直接通过fragmentManager变量来调用方法。

3、hide、show方法和replace两种方式切换fragment有什么区别?

replace方式,直接切换的,上一个fragment被destroy掉了,再回到上一个fragment只能重新创建。hide、show方法,先把fragment用add方法加进去,显示的时候隐藏其他fragment,把要展示的fragment直接show出来,上一个fragment没有被destroy掉,状态还可以保存。

4、关于侧滑菜单使用fragment

例子代码如下

 /*** 侧滑控件 */
    public static SlidingMenu menu;
 menu = new SlidingMenu(this);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        int width = CommonTools.getWindowWidth(this);
        menu.setBehindWidth(width - CommonTools.dp2px(mContext, 60));
        menu.setMenu(R.layout.fram_menu);
        menu.setBehindScrollScale(1.0f);//滑动样式
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,mMenuFragment).commit();
需要注意的是setMenu方法,只是设置侧滑自己的页面,不要写activity的布局。getSupportFragmentManager的replace方法的第一个参数是view的id,这个view会在setMenu的布局里去找id,也就是说,这个view是setMenu布局里的id。如果找不到id的话,侧滑菜单是黑色的,显示不出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值