蜂窝上 ListFragment 中的 ListAdapter 具有兼容性 api
我正在使用兼容性 API 在 Android Gingerbread 和 honeycomb 设备上提供我的应用程序。 我构建了一个 ListFragment
并将自己的 ListAdapter
实现设置为 ListFragment
:
ListableAdapter adapter = new ListableAdapter(this.savedItems, (BaseActivity) this.getActivity());
this.setListAdapter(adapter);
ListAdapter
实现了 getView
像这样的方法:
public View getView(int pos, View convertView, ViewGroup parent) {
Log.d(this.getClass().getName(), "building list item @ " + pos);
View v = convertView;
if (!this.savedItems.isEmpty() && (pos < this.savedItems.size())) {
if (v == null) {
LayoutInflater vi = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.saved_list_item, null);
}
Listable item = this.savedItems.get(pos);
Log.d(this.getClass().getName(), "Item @ " + pos + ": " + item.getDisplayName());
if (item != null) {
ImageView icon = (ImageView) v.findViewById(R.id.saved_item_icon);
TextView title = (TextView) v.findViewById(R.id.saved_item);
StarField star = (StarField) v.findViewById(R.id.favorit);
star.setEnabled(false);
icon.setImageResource(item.getImageID());
title.setText(item.getDisplayName());
star.setChecked(item.isFavorit());
}
}
// this.pos++;
return v;
}
在 Gingerbread 设备(三星 Galaxy S2)上,这工作得非常好。我的列表已按我的预期填充。在我的 GalaxyTab 10.1 (Android 3.1) 上,参数 pos
出现在每次调用 0
中,因此我只有一个列表项。
有谁知道为什么 ListFragment
使用这种奇怪的参数调用 getView
吗?
I am using the compatibility API to provide my app on Android Gingerbread and honeycomb devices.
I built a ListFragment
and set my own ListAdapter
implementation to the ListFragment
:
ListableAdapter adapter = new ListableAdapter(this.savedItems, (BaseActivity) this.getActivity());
this.setListAdapter(adapter);
The ListAdapter
implements the getView
method like this:
public View getView(int pos, View convertView, ViewGroup parent) {
Log.d(this.getClass().getName(), "building list item @ " + pos);
View v = convertView;
if (!this.savedItems.isEmpty() && (pos < this.savedItems.size())) {
if (v == null) {
LayoutInflater vi = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.saved_list_item, null);
}
Listable item = this.savedItems.get(pos);
Log.d(this.getClass().getName(), "Item @ " + pos + ": " + item.getDisplayName());
if (item != null) {
ImageView icon = (ImageView) v.findViewById(R.id.saved_item_icon);
TextView title = (TextView) v.findViewById(R.id.saved_item);
StarField star = (StarField) v.findViewById(R.id.favorit);
star.setEnabled(false);
icon.setImageResource(item.getImageID());
title.setText(item.getDisplayName());
star.setChecked(item.isFavorit());
}
}
// this.pos++;
return v;
}
On Gingerbread Devices (Samsung Galaxy S2) this works very fine. My list is populated as I expected. On my GalaxyTab 10.1 (Android 3.1) the Parameter pos
is in every call 0
so I just have one list item.
Does any one know why the ListFragment
calls the getView
with this strange parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试不将 Activity 转换为 BaseActivity ?
如果您改用 getActivity() 会发生什么。
您在 Fragment 生命周期的哪个部分设置适配器?
最好的祝愿,
蒂姆
did you try not to cast the Activity to BaseActivity?
What happens if you use getActivity() instead.
In which part of the Fragment lifecycle do you setup the adapter?
Best wishes,
Tim