我正在處理需要顯示項目的動態列表的佈局。每個項目都有一個定義的UUID,一個名稱(橫幅文本),一個圖像URL和一個布爾值來表明它是否被選中 - 我還創建了一個POJO來保存這些。我創建了一個自定義佈局,將項目放在網格視圖中,其中包含橫幅文本的兩個圖標。它看起來像這樣:GridView與CustomLayout項目和狀態
<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
而不僅僅是第一個?或者我是否完全錯誤?
謝謝,只要我開始使用視圖,而不是父母它工作得很好。我也刪除了FrameLayout,覆蓋看起來也是如此。我認爲FrameLayout將有必要將圖像疊加在一起。 –
@Luksprog嘿你能幫我嗎。我想單擊圖像視圖的事件並拖動網格項的按鈕。但不知道如何? –