fragment+fragmenttabhost实现底部选项卡导航(可滑动切换)

Fragment对于我们来说可能并不陌生,在android3.0之后引进开发,对于处理平板大屏幕界面分布,fragment有着activity没有的优势,它“寄生”于activity解决了一个屏幕显示多个“分屏”的问题,管理同一个activity下多个“碎片”界面的布局显示及其数据交互。在3.0版本以下的开发环境,则需要导入V4到作为支持。

fragment的具体使用方法在这里并没有详细介绍,而主要要介绍是底部选项卡导航的实现,我们先看看效果图

实现参考了github上面的一些例子和网友的一些demo,附上自己的demo下载链接点击打开链接

主界面代码

package com.example.fragmenttabhost;import com.example.fragmentbottommenu.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * * @author yummy * email:yummyl.lau@gmail.com * 主菜单界面 */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button nosupport = (Button) findViewById(R.id.nosupport);Button support = (Button) findViewById(R.id.support);nosupport.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, FragmentTab.class);startActivity(intent);}});support.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, FragmentTabSupportSlip.class);startActivity(intent);}});}}主界面布局代码<RelativeLayout xmlns:android=""xmlns:tools=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:layout_centerInParent="true"android:id="@+id/nosupport"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="不支持滑动的菜单"/><Buttonandroid:layout_below="@+id/nosupport"android:id="@+id/support"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="支持滑动的菜单" /></RelativeLayout>

其中跳转到两个activity,一个是不支持滑动FragmentTab.java,一个是支持滑动FragmentTabSupportSlip.java

FragmentTab.java代码

package com.example.fragmenttabhost;import com.example.fragmentbottommenu.R;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;import android.view.Menu;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TabHost.TabSpec;/** * * @author yummy * email:yummyl.lau@gmail.com * */public class FragmentTab extends FragmentActivity {private FragmentTabHost mTabHost;private RadioGroup mTabRg;private final Class[] fragments = { TabFragmentOne.class, TabFragmentTwo.class,TabFragmantThree.class, TabFragmentThree.class };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_tab);initView();}private void initView() {mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);// 得到fragment的个数int count = fragments.length;for (int i = 0; i < count; i++) {// 为每一个Tab按钮设置图标、文字和内容TabSpec tabSpec = mTabHost.newTabSpec(i + "").setIndicator(i + "");// 将Tab按钮添加进Tab选项卡中mTabHost.addTab(tabSpec, fragments[i], null);}mTabRg = (RadioGroup) findViewById(R.id.tab_rg_menu);mTabRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.tab_rb_1:mTabHost.setCurrentTab(0);break;case R.id.tab_rb_2:mTabHost.setCurrentTab(1);break;case R.id.tab_rb_3:mTabHost.setCurrentTab(2);break;case R.id.tab_rb_4:mTabHost.setCurrentTab(3);break;default:break;}}});mTabHost.setCurrentTab(0);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}其实现是通过tabhost设置fragment,底部采用单选按钮,通过样式控制实现效果图,,单选按钮的监听实现切换fragment。

对应布局xml

<LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><FrameLayoutandroid:id="@+id/realtabcontent"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1" /><RadioGroupandroid:id="@+id/tab_rg_menu"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/mmfooter_bg"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/tab_rb_1"style="@style/tab_rb_style"android:checked="true"android:drawableTop="@drawable/tab_selector_weixing"android:text="微信"/><RadioButtonandroid:id="@+id/tab_rb_2"style="@style/tab_rb_style"android:drawableTop="@drawable/tab_selector_tongxunlu"android:text="通讯录" /><RadioButtonandroid:id="@+id/tab_rb_3"style="@style/tab_rb_style"android:drawableTop="@drawable/tab_selector_faxian"android:text="发现" /><RadioButtonandroid:id="@+id/tab_rb_4"style="@style/tab_rb_style"android:drawableTop="@drawable/tab_selector_wo"android:text="我" /></RadioGroup><android.support.v4.app.FragmentTabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="wrap_content"android:visibility="gone" ><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="0dp"android:layout_height="0dp"android:layout_weight="0" /></android.support.v4.app.FragmentTabHost></LinearLayout>

没有人会帮你一辈子,所以你要奋斗一生。

fragment+fragmenttabhost实现底部选项卡导航(可滑动切换)

相关文章:

你感兴趣的文章:

标签云: