2012-07-10 67 views
0

如何在没有任何意图或视图的Android简单标签中实现(如在TabHost中)?Android中的简单标签

我只需要选项卡标题,并且想要听取选项卡索引如何更改。

或者告诉我如何实现多位置开关。

UPD:我使用api v14和ActionBar选项卡已用于其他目的。

+0

我试过tabhost,但它不适合我,因为我不能使用TabActivity或在不同的选项卡上显示不同的活动。我有一个GridView和标签需要改变这个GridView的模式。 – user1513587 2012-07-10 03:48:49

回答

1

最简单的解决方案(在我看来,当然)就是简单地使用三个不同的ImageView或按钮来模仿“标签”。您可以根据需要为“选定的选项卡”图像换出“未选定的选项卡”图像。

据我所知,没有简单的方法超出TabHosts和其他此类解决方案需要活动,碎片或视图为不同的选项卡。

+0

非常丑陋的解决方案,我希望它不是独一无二的。 – user1513587 2012-07-10 04:08:54

+0

这有什么不好?什么是除一组按钮之外的选项卡? – 2012-07-10 15:27:11

0

我有一个类似的项目,而我只是需要听取标签更改。下面是谷歌一个很好的例子:

public class MyPagerAdapter extends FragmentPagerAdapter 
     implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { 
    private final Context mContext; 
    private final TabHost mTabHost; 
    private final ViewPager mViewPager; 

private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); 

private class TabInfo { 
    @SuppressWarnings("unused") 
    private final String tag; 
    private final Class<?> clss; 
    private final Bundle args; 

    public TabInfo(String _tag, Class<?> _clss, Bundle _args) { 
     tag = _tag; 
     clss = _clss; 
     args = _args;  
      } 
} 

private class DummyFactory implements TabHost.TabContentFactory { 

    private final Context mContext; 

    public DummyFactory(Context context) { 
     mContext = context; 
    } 
    public View createTabContent(String tag) { 
     View v = new View(mContext); 
     v.setMinimumHeight(0); 
     v.setMinimumWidth(0); 
     return v; 
    } 

} 
public MyPagerAdapter(FragmentActivity activity, TabHost tabHost, ViewPager viewPager) { 
    super(activity.getSupportFragmentManager()); 

    mContext = activity; 
    mTabHost = tabHost; 
    mViewPager = viewPager; 

    mTabHost.setOnTabChangedListener(this); 
    mViewPager.setAdapter(this); 
    mViewPager.setOnPageChangeListener(this); 
} 

public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) { 
    tabSpec.setContent(new DummyFactory(mContext)); 

    String tag = tabSpec.getTag(); 

    TabInfo tab = new TabInfo(tag, clss, args); 
    mTabs.add(tab); 
    mTabHost.addTab(tabSpec); 
    this.notifyDataSetChanged(); 

} 

@Override 
public Fragment getItem(int i) { 
    TabInfo tab = mTabs.get(i); 
    Fragment fragment = Fragment.instantiate(mContext, tab.clss.getName(), tab.args); 
    Log.d("DEBUG", "getItem from view pager called returning hash: " + fragment.hashCode()); 
    return fragment; 
} 

@Override 
public int getCount() { 

    return mTabs.size(); 
} 

public void onPageScrollStateChanged(int arg0) { 
    // TODO Auto-generated method stub 

} 

public void onPageScrolled(int arg0, float arg1, int arg2) { 
    // TODO Auto-generated method stub 

} 


public void onPageSelected(int position) { 
    // Unfortunately when TabHost changes the current tab, it kindly 
    // also takes care of putting focus on it when not in touch mode. 
    // The jerk. 
    // This hack tries to prevent this from pulling focus out of our 
    // ViewPager. 
    TabWidget widget = mTabHost.getTabWidget(); 
    int oldFocusability = widget.getDescendantFocusability(); 
    widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); 
    mTabHost.setCurrentTab(position); 
    widget.setDescendantFocusability(oldFocusability); 
} 

public void onTabChanged(String tabId) { 
    int position = mTabHost.getCurrentTab(); 

    mViewPager.setCurrentItem(position); 

} 
} 

相关部分这里适配器实现TabHost.OnTabChangeListener,并在构造函数体中,onTabChangedListener相应设置(mTabHost.setOnTabChangedListener(this)

您还会注意到内部类DummyFactory

private class DummyFactory implements TabHost.TabContentFactory { 

    private final Context mContext; 

    public DummyFactory(Context context) { 
     mContext = context; 
    } 
    public View createTabContent(String tag) { 
      View v = new View(mContext); 
     v.setMinimumHeight(0); 
     v.setMinimumWidth(0); 
     return v; 
    } 
} 

其在addTab用于创建内容视图与height和0 width,这允许您在布局中的TabHost下添加实际内容。

0

这里是一个例子

假设SE01,SE02和SE03在水平的LinearLayout或滚动型(如果你想支持更多的选项卡)三个按钮。因此,您可以在每个按钮上设置onClickListeners,并对它们进行编程,以便在单击时切换到特定的背景(可以显示选项卡)。这是最简单的方法。这里有一个代码片段,它很容易理解。希望这有助于:-)

  //initialize the first button to be 'selected' when the activity is started by a background which is similar to the other buttons but shows the selected button/tab as highlighted. 

    se01.setBackgroundResource(R.drawable.popup_full_dark2); 
    lastClicked = (Button) findViewById(R.id.ph_s01);//this keeps a track of the last tab/button pressed 
    toSe01();// a function which performs the changes to the view with respect to the tab selected 

    //Listeners 
    se01.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (v.getId() != lastClicked.getId()) { 
       lastClicked 
         .setBackgroundResource(R.drawable.popup_bottom_dark);//changes the background to show the tab is selected 
       v.setBackgroundResource(R.drawable.popup_full_dark2); 
       lastClicked = se01; 
       s.startAnimation(new Animation9()); 
       toSe01(); 
       s.scrollTo(0, 0); 
      } 
     } 
    }); 
    se02.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      // TODO Auto-generated method stub 
      if (v.getId() != lastClicked.getId()) { 
       lastClicked 
         .setBackgroundResource(R.drawable.popup_bottom_dark); 
       v.setBackgroundResource(R.drawable.popup_full_dark2); 
       lastClicked = se02; 
       s.startAnimation(new Animation9()); 
       toSe02(); 
       s.scrollTo(0, 0); 
      } 
     } 
    }); 
    se03.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      // TODO Auto-generated method stub 
      if (v.getId() != lastClicked.getId()) { 
       lastClicked 
         .setBackgroundResource(R.drawable.popup_bottom_dark); 
       v.setBackgroundResource(R.drawable.popup_full_dark2); 
       lastClicked = se03; 
       s.startAnimation(new Animation9()); 
       toSe03(); 
       s.scrollTo(0, 0); 
      } 
     } 
    });