【发布时间】:2012-08-18 13:01:29
【问题描述】:
我正在处理需要显示动态项目列表的布局。每个项目都有一个定义的 UUID、一个名称(横幅文本)、一个图像 URL 和一个布尔值以指示它是否被选中 - 我还创建了一个 POJO 来保存这些。我创建了一个自定义布局,将项目放在网格视图中,两个图标上带有横幅文本。它看起来像这样:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/ItemLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/ItemImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="@android:color/transparent" />
<ImageView
android:id="@+id/ItemOverlayImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:background="@android:color/transparent" />
<TextView
android:id="@+id/ItemTextBanner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#BF000000"
android:gravity="center"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
我还创建了自定义适配器和活动,我在读取所有项目、下载图像并编码为位图以及设置文本横幅时没有问题。问题是如果单击该项目,我希望“选定”状态和图像发生变化。
我知道我可以添加GridView.setOnItemOnClickListener 并且在里面我知道哪个项目被选中以及它来自父视图,但我不确定如何让它处于“选中”状态。 This 链接使用适配器中静态列表中的状态,这是我所建模的。我添加了以下方法并从GridView.setOnItemOnClickListener调用它。
public void itemSelected(View parent, int position)
{
if(items.get(position).isSelected())
{
ImageView interestSelected = (ImageView)parent.findViewById(R.id.ItemOverlayImage);
interestSelected.setImageResource(0);
}
else
{
ImageView interestSelected = (ImageView)parent.findViewById(R.id.ItemOverlayImage);
interestSelected.setImageResource(R.drawable.overlay);
}
}
第一个问题很明显,我没有将状态保存到我的 items 数组中,但这是一个简单的解决方法。真正的问题是只有第一个项目得到覆盖。那么如何更改所选 GridView 中项目的R.id.ItemOverlayImage 而不仅仅是第一个?还是我完全错了?
【问题讨论】:
标签: android android-layout android-widget android-view