fragment+Radiobutton实现顶部导航切换

    看过网上很多很多的Fragment实现的导航栏,感觉都过于繁琐,所有写一篇关于自己的心得,这个应该是目前网上最简单的了。废话不多说,直接上项目吧

第一步:创建XXFragment.java和fragment_XX.xml文件:

public class AddFragment extends Fragment implements OnClickListener {
  private View view;
 
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
     view = inflater.inflate(R.layout.fragment_add, container, false);
     
     return view;
  }

  @Override
  public void onClick(View arg0) {
  }
}
这只是其中的一个Fragment,具体几个按照项目来定。至于 fragment_add.xml这个文件就一个textView。

第二步:创建activity_main.xml主文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RadioGroup
        android:id="@+id/main_tab_group"
        android:layout_width="match_parent"
        android:layout_weight="5"
        android:layout_height="wrap_content"
 
        android:orientation="vertical"
        android:paddingTop="2.0dip" >

        <RadioButton
            android:id="@+id/main_tab_home"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:button="@drawable/health_selector"
            android:checked="false" />

        <RadioButton
            android:id="@+id/main_tab_add"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:button="@drawable/mall_selector"
            android:checked="false" />

        <RadioButton
            android:id="@+id/main_tab_delete"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:button="@drawable/subscribe_selector"
            android:checked="false" />

        <RadioButton
            android:id="@+id/main_tab_update"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:button="@drawable/find_selector"
            android:checked="false" />

        <RadioButton
            android:id="@+id/main_tab_query"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:button="@drawable/more_selector"
            android:checked="false" />
    </RadioGroup>
    <FrameLayout 
        android:id="@+id/fg_view"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent"/>
</LinearLayout>

第三步:完善MainActivity.java文件:

public class MainActivity extends FragmentActivity implements OnClickListener {
  private RadioButton mRbHome, mRbAdd, mRbDelete, mRbQuery, mRbUpdate;
  private FrameLayout mFrg;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initData();
  }

  /**
   * 
   */
  private void initView() {
    mRbHome = (RadioButton) this.findViewById(R.id.main_tab_home);
    mRbAdd = (RadioButton) this.findViewById(R.id.main_tab_add);
    mRbDelete = (RadioButton) this.findViewById(R.id.main_tab_delete);
    mRbUpdate = (RadioButton) this.findViewById(R.id.main_tab_update);
    mRbQuery = (RadioButton) this.findViewById(R.id.main_tab_query);
    mFrg=(FrameLayout) this.findViewById(R.id.fg_view);
    mRbHome.setOnClickListener(this);
    mRbAdd.setOnClickListener(this);
    mRbDelete.setOnClickListener(this);
    mRbQuery.setOnClickListener(this);
    mRbUpdate.setOnClickListener(this);
  }

  /**
   * 
   */
  private void initData() {
    FragmentTransaction t;
     t =getSupportFragmentManager().beginTransaction();
     t.replace(R.id.fg_view, new HomeFragment());
     t.commit();
  }

  @Override
  public void onClick(View v) {
    FragmentTransaction t;
    t =getSupportFragmentManager().beginTransaction();
    switch (v.getId()) {
      case R.id.main_tab_home:
       t.replace(R.id.fg_view, new HomeFragment());
       t.commit();
        break;
      case R.id.main_tab_add:
       t.replace(R.id.fg_view, new AddFragment());
       t.commit();
       break;
      case R.id.main_tab_delete:
      t.replace(R.id.fg_view, new HomeFragment());
      t.commit();
        break;
      case R.id.main_tab_query:
        t.replace(R.id.fg_view, new QueryFragment());
        t.commit();
        break;
      case R.id.main_tab_update:
       t.replace(R.id.fg_view, new DeleteFragment());
       t.commit();
        break;

      default:
        break;
    }
  }


附上源代码(未使用ViewPager):http://download.csdn.net/detail/u013651405/8764731

附上源码(ViewPager+RadioButton+Fragment):http://download.csdn.net/detail/u013651405/8764755



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值