目前,我使用 ABS、ActionBar Tabs 和 TabsAdapter/ViewPager 为我的应用制作漂亮的标签布局。我在顶部有 5 个以上的类别选项卡 - 最终用户也可以添加新类别(我将在稍后设置)。所以,目前,我有一个主要的 SherlockFragmentActivity 和许多 SherlockFragment 类别文件。在主 SFA 的 onCreate 中,我构建了 actionBar 并添加了它的所有选项卡,如下所示:
mTabsAdapter.addTab(bar.newTab().setText(R.string.login),
LoginFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("Geographics"),
GeoFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),
EconFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),
ElectionsFragment.class, null);
我想做的是创建一个新的解决方案,CategoryFragment,而不是使用所有特定的 Elections、Geo、Econ 等。有人能想象出一个解决方案吗? 理想情况下,我只想将一个字符串传递给添加的选项卡,这样 CategoryFragment 就可以根据该字符串进行膨胀。我想要这个解决方案,因为代码在多个类中非常冗余,当所有类真正做的是从 SQL DB 在线加载内容时,仅获取其自身类别的数据。
这是我的 TabsAdapter 类:
public class TabsAdapter extends FragmentPagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private Polling activity;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
final class TabInfo {
private final Class<?> clss;
private final Bundle args;
//private final String title;
//This string is implemented only as part of my attempt!
TabInfo(Class<?> _class, Bundle _args, String _title) {
clss = _class;
args = _args;
title = _title;
}
}
/*Constructor method that adds a TabsAdapter to each tab that is created.
* It also adds the ViewPager to each tab so that the user can swipe to change tabs.
*/
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
this.activity = (Polling) activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
/*This is the method I've been trying to use to solve the problem, but it's not really cutting it!*/
public void buildTabs() throws ClassNotFoundException {
String [] tabs = {"Econ", "Elections", "Geo", "Politics", "Science", "Finance", "Religion",
"Military", "International" };
final String resource = "R.string.";
mTabsAdapter.addTab(bar.newTab().setText("Login"),
LoginFragment.class, null, "Login");
for (int j = 0; j < tabs.length; j++) {
String res = resource + tabs[j];
String clas = tabs[j] + "Fragment";
String total = "com.davekelley.polling." + clas;
mTabsAdapter.addTab(bar.newTab().setText(tabs[j]),
CategoryFragment.class, null, tabs[j]);
}
}
/*A fairly simple method that sets the TabInfo for each tab so that the TabsAdapter
* knows which class the tab that is being added actually belonds to. It also updates
* the UI interface when each tab is added.
*/
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args, String title) {
TabInfo info = new TabInfo(clss, args, title);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
public int getCount() {
return mTabs.size();
}
/*A method that is used in other classes to allow each tab Fragment to
* access its inherited methods from a mother-class, in this case, SherlockFragment
*/
public int getPosition(SherlockFragment fragment) {
for (int j = 1; j < mTabs.size(); j++) {
TabInfo info = (TabInfo) mActionBar.getTabAt(j).getTag();
if (info.title.matches(mTabs.get(j).title)) {
return j;
}
}
return -1;
}
public SherlockFragment getItem(int position) {
TabInfo info = mTabs.get(position);
return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
/*This method reads the user's selection for a new tab and sets that tab as
* the new current focus.*/
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
selectInSpinnerIfPresent(position, true);
}
private void selectInSpinnerIfPresent(int position, boolean animate) {
try {
View actionBarView = findViewById(R.id.abs__action_bar);
if (actionBarView == null) {
int id = getResources().getIdentifier("action_bar", "id", "android");
actionBarView = findViewById(id);
}
Class<?> actionBarViewClass = actionBarView.getClass();
Field mTabScrollViewField = actionBarViewClass.getDeclaredField("mTabScrollView");
mTabScrollViewField.setAccessible(true);
Object mTabScrollView = mTabScrollViewField.get(actionBarView);
if (mTabScrollView == null) {
return;
}
Field mTabSpinnerField = mTabScrollView.getClass().getDeclaredField("mTabSpinner");
mTabSpinnerField.setAccessible(true);
Object mTabSpinner = mTabSpinnerField.get(mTabScrollView);
if (mTabSpinner == null) {
return;
}
Method setSelectionMethod = mTabSpinner.getClass().getSuperclass().getDeclaredMethod("setSelection", Integer.TYPE, Boolean.TYPE);
setSelectionMethod.invoke(mTabSpinner, position, animate);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
/* This is the method that actually draws the newest tab onto the screen when
* it is selected.*/
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
sp = getSharedPreferences("prefs", MODE_PRIVATE);
SharedPreferences.Editor preferencesEditor = sp.edit();
preferencesEditor.putInt("lastPosition", mViewPager.getCurrentItem());
preferencesEditor.commit();
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
}
最佳答案
好吧,您想使用一个 fragment 类,比如 BaseFragment,而不是多个类;通过为这个新 fragment 传递一个字符串(标识符)来做到这一点。您还在所有这些 fragment 中运行了一些代码,因此最好使用新方法。
但是,将标识符传递给此 BaseFragment 只会导致其代码困惑,您将不得不处理所有这些 if...else,当它真正都与这个标识符相关,你的代码实际上仍然是一样的,因为你必须将特定 fragment 的代码移动到这个 BaseFragment 中。因此,这种方法没有附加值。您可以保留所有 fragment ,并在添加选项卡时实际使用此“标识符”来选择要添加的正确 fragment 。
至于在您的许多 fragment 中运行的代码。这是一个非常基本的方法:
创建一个扩展 Fragment 的类 BaseFragment。 (我在这里太天真了)添加一个函数 common() 来完成这个普通的事情。最后让你所有的 Fragments 扩展这个 BaseFragment 而不是 Fragment。现在,您可以调用 common()。 (同样,您可能需要 common1()、common2()、...)。
关于android - 为多个对象使用通用类(actionBar 选项卡),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10889447/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev