Android:自定义图库 setSelection() 问题
我有一个扩展的 BaseAdapter,它具有连接到自定义图库的 LinearLayout 子项(每个子项都有一个 ImageView 和一个 TextView)。
当我第一次启动 Activity 时,我想调用 setSelection(position) 以使 ImageView 将其选择器更改为“选定”图像。一旦我将图库投放到后续选定的孩子身上,这就会起作用,但不是第一次启动该应用程序。
我的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/home_image_select" />
<item android:state_selected="false"
android:drawable="@drawable/home_image" />
</selector>
我的第一个猜测是在调用 setSelection() 之后在适配器上调用 notificationDataSetChanged(),我尝试这样做:
((CustomAdapter) gallery.getAdapter()).notifyDataSetChanged();
那没有做任何事情。我还尝试重写 Gallery 类的 setSelection() 来执行此操作:
View v = this.getAdapter().getView(position, null, this);
((ImageView) v.findViewById(R.id.gallery_image)).setSelected(true);
这也不起作用。我有什么遗漏或可以尝试的吗?
I have an extended BaseAdapter that has LinearLayout children (an ImageView and a TextView in each) hooked up to a custom Gallery.
When I first launch my Activity, I want to call setSelection(position)
to cause the ImageView to change its selector to the "selected" image. This works once I fling the Gallery, on subsequent selected children, but not the very first time the app is launched.
My selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/home_image_select" />
<item android:state_selected="false"
android:drawable="@drawable/home_image" />
</selector>
My first guess was to call notifyDataSetChanged() on the adapter after calling setSelection(), which I attempted to do like this:
((CustomAdapter) gallery.getAdapter()).notifyDataSetChanged();
That didn't do anything. I also tried overriding the setSelection() of the Gallery class to do this:
View v = this.getAdapter().getView(position, null, this);
((ImageView) v.findViewById(R.id.gallery_image)).setSelected(true);
That doesn't work either. Anything I'm missing or could try?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过覆盖 Gallery 的 setSelection() 找到了解决我自己问题的方法(毕竟它有效)。
I found the solution to my own problem by overwriting the setSelection() of Gallery (it worked after all).
我认为您不应该调用
notifyDataSetChanged()
,当底层数据集更改时,选择状态将被清除。只需调用
setSelection(position)
,它就可以在我的应用程序中运行。I think you should not call
notifyDataSetChanged()
, selection state will be cleared when underlying dataset changed.Just call
setSelection(position)
, it works in my app.