带有CustomLayout项和状态的GridView

时间:2012-08-23 03:37:43

标签: android android-layout android-widget android-view

我正在开发一个需要显示动态项目列表的布局。每个项目都有一个定义的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而不仅仅是第一个?或者我是否完全错了?

1 个答案:

答案 0 :(得分:1)

关于您的代码的一些要点:

您可能只获得带有叠加层的第一个项目,因为您在父级findViewById而不是行视图GridView上使用view进行搜索,因此{{1} }将返回它找到的第一个findViewById(位于第一个可见行)。为了显示叠加层,您可以根据适配器的ImageView方法中使用的代码来选择如何执行此操作,如果您只想显示一个带有叠加层的项目(例如,当您单击一个项目,单击的前一个项目(如果有)将取消选择)或者如果您有切换项目的选项(单击 - &gt;叠加,另一个单击 - >没有叠加)。如果不知道上面的信息,我就会这样做:

getView

只要您在public void onItemClick (AdapterView<?> parent, View view, int position, long id) { ImageView overlay = (ImageView) view.findViewById(R.id.ItemOverlayImage); if (!items.get(position).isSelected()) { // the item doesn't have an overlay overlay.setImageResource(R.drawable.overlay); // set the overlay items.setSelected(true); // set to true so the adapter knows it } else { // the item has the overlay so we remove it overlay.setImageResource(0); items.setSelected(false); set to false so the adapter knows it } } 方法中使用getView来设置或不设置叠加层,您就可以了。

此外,您应该移除包裹isSelected的{​​{1}},因为您不对其进行任何操作(?!?)。

相关问题