使用fragmentTableHost实现底部菜单栏

1底部菜单的几种实现方式:

TabHost+Activity

RadioButton+Fragment

这种方法使用RadioGroup来实现RadioButton的互斥选择,而且需要在activity中实现和判断点击事件

FragmentTableHost+Fragment

这个就比较简单了,只需要在layout文件中定义fragmentTableHost(需要support v4包),然后再在文件中创建出tabspec并添加到fragmentTableHost中就可以了,比radiobutton少了监听点击事件和判断点击事件,因为直接就对view和fragment进行了绑定

2.fragmentTableHost的结构:

使用fragmentTableHost实现底部菜单栏_第1张图片

3.创建流程:

下面代码是大概流程

mTabHost= (FragmentTabHost) findViewById(android.R.id.tabhost);

//1.invoke setup method
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

//2.create TabSpec
TabHost.TabSpec spec=mTabHost.newTabSpec("home");

//3.create a view as indicator
View view=null;
view=getLayoutInflater().inflate(R.layout.tab_indicator,null);

//initialize the child view of indicator view
ImageView img= (ImageView) view.findViewById(R.id.icon_tab);
TextView text= (TextView) view.findViewById(R.id.tv_indicator);

img.setBackgroundResource(R.mipmap.icon_home);
text.setText("主页");

//4.set indicator for spec
spec.setIndicator(view);

//5.add spec to fragmentTabHost
mTabHost.addTab(spec, Fragment.class, null);

4.具体实现

主布局文件:




    
    
    

    
    

        
        

    

fragment布局文件:

这里列出其中一个




    

状态选择器:

这里只列出一个,其他的一样


    
    

MainActivity:

package com.lc.store.activity;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.lc.store.R;
import com.lc.store.activity.bean.Tab;
import com.lc.store.activity.fragment.CartFragment;
import com.lc.store.activity.fragment.CategoryFragment;
import com.lc.store.activity.fragment.HomeFragment;
import com.lc.store.activity.fragment.HotFragment;
import com.lc.store.activity.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * FragmentTabHost structure:
 * 

* fragmentTabHost->tabSpec->indicator->view->ImageView * ->TextView * ->tabSpec */ public class MainActivity extends AppCompatActivity { private FragmentTabHost mTabHost; private List mTabs = new ArrayList(5); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); initTab(); } private void initTab() { Tab tab_home = new Tab(R.string.home, R.drawable.selector_icon_home, HomeFragment.class); Tab tab_hot = new Tab(R.string.hot, R.drawable.selector_icon_hot, HotFragment.class); Tab tab_category = new Tab(R.string.catagory, R.drawable.selector_icon_category, CategoryFragment.class); Tab tab_cart = new Tab(R.string.cart, R.drawable.selector_icon_cart, CartFragment.class); Tab tab_mine = new Tab(R.string.mine, R.drawable.selector_icon_mine, MineFragment.class); mTabs.add(tab_home); mTabs.add(tab_hot); mTabs.add(tab_category); mTabs.add(tab_cart); mTabs.add(tab_mine); //1.invoke setup method mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); for(Tab t:mTabs){ //2.create TabSpec TabHost.TabSpec spec = mTabHost.newTabSpec(getString(t.getTitle())); //3.create a view as indicator View view = null; view = getLayoutInflater().inflate(R.layout.tab_indicator, null); //initialize the child view of indicator view ImageView img = (ImageView) view.findViewById(R.id.icon_tab); TextView text = (TextView) view.findViewById(R.id.tv_indicator); img.setBackgroundResource(t.getIcon()); text.setText(t.getTitle()); //4.set indicator for spec spec.setIndicator(view); //5.add spec to fragmentTabHost mTabHost.addTab(spec, t.getFragment(), null); } //去掉分割线 mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); mTabHost.setCurrentTab(2); } }

 fragment:

注意,这里继承的是v4里面的fragment
package com.lc.store.activity.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lc.store.R;

/**
 * Created by jiangbo on 2016/4/2.
 * Description
 */
public class HomeFragment extends Fragment{
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home,container,false);
    }
}





你可能感兴趣的:(android)