使用反射时与操作栏交互
为了保持向后兼容性,我创建了一个类来访问操作栏:
import android.app.ActionBar; 导入 android.app.ActionBar.Tab; 导入 android.app.ActionBar.TabListener; 导入 android.app.FragmentTransaction;
public class ReflectionBar{
static void getActionBar(Activity a) {
ActionBar bar = a.getActionBar();
bar.addTab(bar.newTab().setText("Tab 1").setTabListener(new TabListener() {
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}));
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowHomeEnabled(true);
// remove the activity title to make space for tabs
bar.setDisplayShowTitleEnabled(false);
return;
}
}
我在 Activity 类中使用以下方法调用它:
if (android.os.Build.VERSION.SDK_INT>10){
ReflectionBar bar = new ReflectionBar();
bar.getActionBar(this);
}
问题是我需要在主 Activity 中监听 onTabSelected 的调用,但我不确定如何设置它。我尝试了一些不同的方法但没有成功,非常感谢任何帮助。
In order to keep backwards compatibility I've created a class to access the action bar:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
public class ReflectionBar{
static void getActionBar(Activity a) {
ActionBar bar = a.getActionBar();
bar.addTab(bar.newTab().setText("Tab 1").setTabListener(new TabListener() {
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}));
bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowHomeEnabled(true);
// remove the activity title to make space for tabs
bar.setDisplayShowTitleEnabled(false);
return;
}
}
And I call it in my Activity class using:
if (android.os.Build.VERSION.SDK_INT>10){
ReflectionBar bar = new ReflectionBar();
bar.getActionBar(this);
}
The problem is that I need to listen to the calls of onTabSelected in my main activity, but I am not sure how to set it up. I've tried a few different things with no success, any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定义一个接口。将接口的实例作为
final
参数传递到getActionBar()
中(实际上应该命名为initActionBar()
,因为您是不返回操作栏,但是,那只是我......)。在TabListener
对象的各种onTab...
方法中,调用界面上相应的方法。您将无法将ActionBar.Tab
对象传递到界面(因为这是 API 级别 11+),但在标记和文本属性之间,您应该能够收集一些值得传递的内容标识活动的选项卡。Define an interface. Pass an instance of the interface into
getActionBar()
as afinal
parameter (which really ought to be namedinitActionBar()
, since you're not returning an action bar, but, that's just me...). In the variousonTab...
methods in yourTabListener
objects, call a corresponding method on your interface. You won't be able to pass theActionBar.Tab
object to the interface (since that's API Level 11+), but between the tag and text properties, you should be able to glean something worth passing that identifies the tab to the activity.