快速搭建底部多个Tab的APP框架

现在APP不是侧滑就是底部切换Tab的模式,今天就做一个底部切换的的简单封装。

首先底部的栏目可以用CheckBox,RadioButton,ImageView随便什么都可以只要能设置两种状态,你用View都行,我这次就用RadioGroup加RadioButton来做,比较简单,然后是上面的显示的几个界面,我们就用FrameLayout,装几个Fragment进行显示和隐藏就行了。好了现在是Acitivity的代码

public class MainActivity extends TabActivity implements CompoundButton.OnCheckedChangeListener{
    private List<Fragment> fragments;
    private FragmentManager fragmentManager;
    private RadioGroup mBottomLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBottomLayout = (RadioGroup) findViewById(R.id.bottom_layout);
        fragments = new ArrayList<>();
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        fragmentManager = getSupportFragmentManager();
        init(fragments,fragmentManager,R.id.container_layout);
        switchTab(0);
        for (int i = 0; i < mBottomLayout.getChildCount(); i++) {
            ((RadioButton) mBottomLayout.getChildAt(i)).setOnCheckedChangeListener(this);
        }
    }
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        for (int i = 0; i < mBottomLayout.getChildCount(); i++) {
            if (buttonView == mBottomLayout.getChildAt(i) && isChecked) {
                switchTab(i);
            }
        }
    }
}
添加fragment的集合,找到装fragment的容器id,监听底部栏目的选中监听

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.wangm.uitest.MainActivity">
    <FrameLayout
        android:id="@+id/container_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </FrameLayout>
   <RadioGroup
       android:id="@+id/bottom_layout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:checkedButton="@+id/rb_1"
       android:orientation="horizontal">
       <RadioButton
           android:id="@+id/rb_1"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:gravity="center"
           android:button="@null"
           android:padding="5dp"
           android:text="货车1"
           android:textColor="@color/tab_selector_text"
           android:drawablePadding="5dp"
           android:drawableTop="@drawable/selector_bottom_tab_1"/>
       <RadioButton
           android:id="@+id/rb_2"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:gravity="center"
           android:button="@null"
           android:padding="5dp"
           android:textColor="@color/tab_selector_text"
           android:text="货车2"
           android:drawablePadding="5dp"
           android:drawableTop="@drawable/selector_bottom_tab_1"/>
       <RadioButton
           android:id="@+id/rb_3"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:gravity="center"
           android:button="@null"
           android:textColor="@color/tab_selector_text"
           android:padding="5dp"
           android:text="货车3"
           android:drawablePadding="5dp"
           android:drawableTop="@drawable/selector_bottom_tab_1"/>
   </RadioGroup>

</LinearLayout>
这是activity的布局


下面就是切换FramemtBase 类了,通过manager用找tag的方式找fragment如果fragment没有用transaction 添加到容器中,如果找到了,直接显示当前的隐藏上次的

public class TabActivity extends AppCompatActivity {
   private List<Fragment> fragments;
   private  FragmentManager fragmentManager;
    private int curIndex = -1;
    private int containerId;
    public void init(List<Fragment> fragments,FragmentManager fragmentManager,int containerId){
        this.fragmentManager = fragmentManager;
        this.fragments =  fragments;
        this.containerId = containerId;
    }
    public void switchTab(int index){
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragmentByTag = fragmentManager.findFragmentByTag(fragments.get(index).getClass().getName());
        if(fragmentByTag == null){
            fragmentByTag = fragments.get(index);
            fragmentTransaction.add(containerId,fragmentByTag,fragmentByTag.getClass().getName());
        }else{
            fragmentTransaction.show(fragmentByTag);
        }
        if(curIndex != -1){
            fragmentTransaction.hide(fragments.get(curIndex));
        }
        curIndex = index;
        fragmentTransaction.commit();

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值