设置gridview元素的可见性

时间:2012-01-19 21:44:50

标签: android

我创建了一个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>

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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;
   }