fragment+Radiobutton实现顶部导航切换

来源:互联网 发布:域名与商标冲突 编辑:程序博客网 时间:2024/05/28 23:09

    看过网上很多很多的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



0 0
原创粉丝点击