2012-01-19 135 views
0

我已經創建了一個gridview併爲其使用自定義適配器。我在網格視圖內添加了兩個圖像。一個是圖像,另一個看起來像關閉按鈕(在所有網格視圖項目中都是相同的)。關閉按鈕在開始時被設置爲不可見,我在我的活動中有另一個按鈕,我想要的是:當我點擊這個按鈕時,我想在我的網格視圖的每個元素中設置關閉按鈕,但現在,當點擊在它上面,沒有任何反應。 下面是我的代碼:設置gridview元素的可見性

ImageView deleteFavorImage; 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.browser); 
    ..... 

    GridView favorGrid = (GridView) findViewById(R.id.favorGrid); 
    favorGrid.setAdapter(adapter); 
    ..... 
    View customView = findViewById(R.id.costumButton); 
    customView.setOnClickListener(this); 

@Override 
public void onClick(View v) { 

    switch(v.getId()){ 

    case R.id.costumButton: 
     deleteFavorImage.setVisibility(View.VISIBLE); 
    break; 
    ..... 
    } 

這是我的形象適配器代碼:

  @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 

      View MyView = convertView; 
      final int pos=position; 

      LayoutInflater li = (LayoutInflater) MyContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      MyView = li.inflate(R.layout.favor_item, null); 


      ImageView iv = (ImageView) MyView.findViewById(R.id.favor_item_image); 
      iv.setImageResource(favorSites.get(pos)); 

      deleteFavorImage = (ImageView) MyView.findViewById(R.id.favDelete); 
      deleteFavorImage.setImageResource(R.drawable.dubtndelete); 

      return MyView; 
     } 

最後這是我的XML文件中的代碼:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="85dp" 
android:layout_height="85dp" 
android:orientation="vertical" 
android:background="@drawable/dufavframe" > 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="75dp" 
    android:layout_height="75dp" 
    android:orientation="vertical" 
    android:layout_centerInParent="true" 
    android:gravity="center"> 

    <com.test.favorsites.AspectRatioImageView android:layout_centerHorizontal="true" 
     android:id="@+id/favor_item_image" 
     android:layout_alignParentTop="true" android:layout_height="wrap_content" 
     android:layout_width="match_parent" android:adjustViewBounds="true"/> 
    </LinearLayout> 


    <ImageView android:id="@+id/favDelete" android:visibility="invisible" 
    android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="-3dp" 
    android:layout_alignParentRight="true" android:layout_marginRight="-5dp"> 
    </ImageView> 



    </RelativeLayout> 
+0

得到了與你的CapsLock鍵有問題嗎? –

回答

0
@Override 
public void onClick(View v) { 

switch(v.getId()){ 

case R.id.costumButton: 
    adapter.updateView(1); 
break; 
..... 
} 

public class YourAdapter extends BaseAdapter { 
    private int visibleFlag = 0; 
    /// 
    //// 
    public void updateView(int flag) { 
     visibleFlag = flag; 
     notifydatasetchanged(); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 

     View MyView = convertView; 
     final int pos=position; 

     LayoutInflater li = (LayoutInflater)MyContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     MyView = li.inflate(R.layout.favor_item, null); 


     ImageView iv = (ImageView) MyView.findViewById(R.id.favor_item_image); 
     iv.setImageResource(favorSites.get(pos)); 

     deleteFavorImage = (ImageView) MyView.findViewById(R.id.favDelete); 
     if (visibleFlag == 1) 
      deleteFavorImage.setVisibility(View.VISIBLE); 
     deleteFavorImage.setImageResource(R.drawable.dubtndelete); 

     return MyView; 
    } 
+0

如果convertView爲null,並且只有膨脹視圖,則應該檢查getView。如果convertView已經不是null,則重用convertView,因爲這是一個gridview項目的實例,剛剛離開可見空間並希望被重用。 這可以節省內存和性能,因爲您沒有浪費gridview項目實例,而這些實例在滾動gridview期間必須進行垃圾回收。 –

2

我找到了解決辦法因爲我是同樣的問題。

int positionSelected = 0;

而且在GridView中點擊聽者全局定義

gridView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      positionSelected=position; 
      ((BaseAdapter)gridView.getAdapter()).notifyDataSetChanged(); 

      } 

     } 
    }); 

而且在getview方法把下面的代碼

public View getView(final int position, View convertView, 
      ViewGroup parent) { 
     View vi = convertView; 

     if (vi == null) 
      vi = inflater.inflate(R.layout.horizontal_color_images_row, 
        null); 
     img_uparrow = (ImageView)vi.findViewById(R.id.img_uparrow); 
     if(positionSelected==position){ 
       img_uparrow.setVisibility(View.VISIBLE); 
      } 
     else 
      img_uparrow.setVisibility(View.INVISIBLE); 

return vi; 
    } 
+0

加1票給你。你救了我的時間。 –