所以看来使用ViewPager时,将使用onPageSelected听者不会被调用第一页相同的问题,因为这个.
我有一些逻辑,为当前选定的页面填充一些更昂贵的UI元素,这在页面更改时有效,但是 it doesn't work for the first page.
如果我在侦听器之后设置当前项,则会为第一页触发回调,但视图尚未初始化,因此我无法操作它:
// Inside PagerAdapter.instantiateItem
ViewHolder vh = new ViewHolder();
cursor.moveToPosition(position);
vh.view = adapter.newView(context, cursor, null);
// Set position as tag so we can retrieve it with findViewByTag
vh.view.setTag(position);
((ViewPager) collection).addView(vh.view,0);
return vh;
// Inside MyActivity.onCreate
pagerAdapter = new SingleMessagePagerAdapter(this, cursor);
pager = (ViewPager)findViewById(R.id.message_pager);
pager.setAdapter(pagerAdapter);
pager.setOnPageSelectedListener(this);
pager.setCurrentItem(selectedItem);
// Inside MyActivity.onPageSelected
// Retrieve tagged view
View view = pager.findViewWithTag(position);
Run Code Online (Sandbox Code Playgroud)
这里view最终为null因为PagerAdapter.instantiateItem尚未运行.所以我想我的问题是,在活动生命周期的哪一点,我可以确定ViewPager已经初始化了视图?我尝试在里面做这个Activity.onAttachedToWindow,Activity.onResume但看起来这两个都被解雇了PagerAdapter.instantiateItem.
我从外部API中提取一些数据,并希望在本地缓存结果.我有一个class SearchTerm,我希望通过表格与几个不同的ActiveRecord类型相关联searchable_items.我很确定我的表格设置正确,但我的关联中的某些内容一定是错的.
class Foo < ActiveRecord::Base
has_many :search_terms, :as => :searchable, :through => :searchable_items
end
class Bar < ActiveRecord::Base
has_many :search_terms, :as => :searchable, :through => :searchable_items
end
class SearchTerm < ActiveRecord::Base
has_many :searchables, :through => :searchable_items
end
class SearchableItem < ActiveRecord::Base
belongs_to :search_term
belongs_to :searchable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
我希望能够做类似的事情SearchTerm.find_by_term('SearchTerm').searchables(它会返回一个Foo和Bar对象的数组)然而,我得到了错误
Could not find the association :searchable_items in model SearchTerm
提前感谢您提供给我的任何见解!
我正在尝试使用sed删除所有出现的
#ifdef _WIN32
#endif
Run Code Online (Sandbox Code Playgroud)
#ifdef和#endif之间存在的所有内容都是空行.我使用sed的经验有限,我已经阅读了关于多线功能的一些文档,但我似乎无法弄明白.任何帮助表示赞赏!
struct SomeStruct
{
int a;
int b;
};
SomeStruct someFn( int init )
{
SomeStruct ret = { init, init };
//...
return ret;
}
void someFn2( SomeStruct* pStruct )
{
// ..
}
int main( )
{
someFn2( &someFn(32) );
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要从编译器本地化错误消息.就目前而言,所有错误消息都在整个源代码中以英文字符串文字的形式传播.我们想将这些错误消息翻译成德语.什么是最好的方法来解决这个问题?保持字符串文字不变,并将char*映射到错误报告例程中的另一种语言,或者用描述性宏替换英语文字,例如.ERR_UNDEFINED_LABEL_NAME并在编译时将此映射到正确的字符串?
在类似的项目中如何处理这样的事情?