Android:自定义图库 setSelection() 问题

发布于 2024-12-29 09:36:00 字数 956 浏览 6 评论 0原文

我有一个扩展的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枉心 2025-01-05 09:36:00

我通过覆盖 Gallery 的 setSelection() 找到了解决我自己问题的方法(毕竟它有效)。

 @Override
public void setSelection(int position) {
    super.setSelection(position);

    View v = this.getAdapter().getView(position, null, this);
    v.setFocusable(true);
    v.requestFocus();
}

I found the solution to my own problem by overwriting the setSelection() of Gallery (it worked after all).

 @Override
public void setSelection(int position) {
    super.setSelection(position);

    View v = this.getAdapter().getView(position, null, this);
    v.setFocusable(true);
    v.requestFocus();
}
你在我安 2025-01-05 09:36:00

我认为您不应该调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文