代码改变世界

Android开发之底部导航栏标准

2014-02-15 12:25  y-z-f  阅读(14155)  评论(2编辑  收藏  举报

以下是封装的库源码:

 1 package com.example.oldtab;
 2 
 3 import java.util.ArrayList;
 4 
 5 import android.content.res.Resources;
 6 import android.graphics.drawable.Drawable;
 7 import android.support.v4.app.FragmentTabHost;
 8 import android.support.v7.app.ActionBarActivity;
 9 import android.view.LayoutInflater;
10 import android.view.View;
11 import android.widget.TabHost;
12 import android.widget.TextView;
13 
14 public class NsFragmentTabHost implements TabHost.OnTabChangeListener {
15     private FragmentTabHost fragmentTabHost;
16     private LayoutInflater layoutInflater;
17     private Resources resources;
18     private ArrayList<NsFragmentItem> tabs;
19     private NsFragmentItem preItem;
20 
21     NsFragmentTabHost(FragmentTabHost tabhost, ActionBarActivity activity) {
22         this(tabhost, activity, android.R.id.tabcontent);
23     }
24 
25     NsFragmentTabHost(FragmentTabHost tabhost, ActionBarActivity activity,
26             int realContentid) {
27         fragmentTabHost = tabhost;
28         tabs = new ArrayList<NsFragmentItem>();
29         layoutInflater = activity.getLayoutInflater();
30         resources = activity.getResources();
31         tabhost.setup(activity.getBaseContext(),
32                 activity.getSupportFragmentManager(), realContentid);
33         tabhost.setOnTabChangedListener(this);
34     }
35 
36     public View newTabView(int layoutid, int backgroung, String title) {
37         TextView tv = (TextView) layoutInflater.inflate(layoutid, null);
38         tv.setText(title);
39         Drawable draw = resources.getDrawable(backgroung);
40         tv.setCompoundDrawablesWithIntrinsicBounds(null, draw, null, null);
41         return tv;
42     }
43 
44     public void AddTab(NsFragmentItem item) {
45         tabs.add(item);
46         fragmentTabHost.addTab(fragmentTabHost.newTabSpec(item.tag)
47                 .setIndicator(item.view), item.fragment, null);
48     }
49 
50     public NsFragmentItem getTabByTag(String tag) {
51         for (NsFragmentItem item : tabs) {
52             if (item.tag == tag) {
53                 return item;
54             }
55         }
56         return null;
57     }
58 
59     @Override
60     public void onTabChanged(String tabId) {
61         NsFragmentItem item = getTabByTag(tabId);
62         if (item != null) {
63             if (preItem != null) {
64                 preItem.view.setBackgroundResource(preItem.upStyle);
65             }
66             preItem = item;
67             item.view.setBackgroundResource(item.downStyle);
68         }
69     }
70 
71     public static class NsFragmentItem {
72         public View view;
73         public String tag;
74         public Class<?> fragment;
75         public int downStyle;
76         public int upStyle;
77     }
78 }

 

下面就是示例,如何使用上面的库

 1 package com.example.oldtab;
 2 
 3 import android.os.Bundle;
 4 import android.support.v4.app.FragmentTabHost;
 5 import android.support.v7.app.ActionBarActivity;
 6 import android.view.View;
 7 
 8 import com.example.oldtab.NsFragmentTabHost.NsFragmentItem;
 9 
10 public class Main extends ActionBarActivity {
11 
12     private static final String TAG_PERSON = "person";
13     private static final String TAG_GROUP = "group";
14     private static final String TAG_SETTING = "setting";
15 
16     private NsFragmentTabHost nsTabHost;
17     private FragmentTabHost tabHost;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
24         nsTabHost = new NsFragmentTabHost(tabHost, this, R.id.realtabcontent);
25         View i1 = nsTabHost.newTabView(R.layout.tabitem,
26                 R.drawable.ic_action_person, "个人");
27         View i2 = nsTabHost.newTabView(R.layout.tabitem,
28                 R.drawable.ic_action_group, "集体");
29         View i3 = nsTabHost.newTabView(R.layout.tabitem,
30                 R.drawable.ic_action_settings, "设置");
31         NsFragmentItem item1 = new NsFragmentItem();
32         item1.view = i1;
33         item1.tag = TAG_PERSON;
34         item1.fragment = PersonFragment.class;
35         item1.downStyle = R.drawable.tab_spec_down;
36         item1.upStyle = R.drawable.tab_spec_nomal;
37         NsFragmentItem item2 = new NsFragmentItem();
38         item2.view = i2;
39         item2.tag = TAG_GROUP;
40         item2.fragment = GroupFragment.class;
41         item2.downStyle = R.drawable.tab_spec_down;
42         item2.upStyle = R.drawable.tab_spec_nomal;
43         NsFragmentItem item3 = new NsFragmentItem();
44         item3.view = i3;
45         item3.tag = TAG_SETTING;
46         item3.fragment = SettingFragment.class;
47         item3.downStyle = R.drawable.tab_spec_down;
48         item3.upStyle = R.drawable.tab_spec_nomal;
49         nsTabHost.AddTab(item1);
50         nsTabHost.AddTab(item2);
51         nsTabHost.AddTab(item3);
52     }
53 }