长按以使图像在gridview中可见

时间:2012-07-11 08:14:56

标签: android gridview android-widget

我在gridview的每个单元格中都有2个图像。第一张图片不可见,第二张图片可见。我想要的是当我长时间点击gridview的任何项目时,应该看到不可见的图像。谢谢。

myGrid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
            DELETE_SIT=1;

            //Toast.makeText(FavorilerAct.this,"long click"+DELETE_SIT, Toast.LENGTH_SHORT).show();
            ((BaseAdapter)myGrid.getAdapter()).notifyDataSetChanged();

            return true;
        }
    });

//这是自定义适配器扩展BaseAdapter

中的getview方法
public View getView(int position, View convertView, ViewGroup parent) 
       {
          View MyView = convertView;
          String url = stock_list.get(position);

      if ( convertView == null )
      {
         LayoutInflater li = getLayoutInflater();
         MyView = li.inflate(R.layout.gridview, null);

         // Add The Text!!!
         TextView tv = (TextView)MyView.findViewById(R.id.favorite_author);
         tv.setText(names.get(position));
         ImageView img=(ImageView)MyView.findViewById(R.id.favorite_image);
         ImageView artiImg=(ImageView)MyView.findViewById(R.id.delete_fav_btn);
         if(DELETE_SIT!=1)
             artiImg.setVisibility(View.INVISIBLE);
         else
             artiImg.setVisibility(View.VISIBLE);
        try {
            img.setImageBitmap(loadBitmap(url));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(DELETE_SIT==1){
            //ImageView delete_btn=(ImageView)MyView.findViewById(R.id.delete_fav_btn);
            //artiImg.setImageResource(R.drawable.kapat);
            artiImg.setVisibility(View.VISIBLE);
        }
      }

      return MyView;
   }

1 个答案:

答案 0 :(得分:0)

好的,试试这个:

public View getView(int position, View convertView, ViewGroup parent) 
{
      View vi= convertView;
      String url = stock_list.get(position);
      ViewHolder holder = null;

      if ( vi == null )
      {
          LayoutInflater li = getLayoutInflater();
          vi = li.inflate(R.layout.gridview, null);
          holder = new ViewHolder();

          holder.tv = (TextView)vi.findViewById(R.id.favorite_author);
          holder.img=(ImageView)vi.findViewById(R.id.favorite_image);
          holder.artiImg=(ImageView)vi.findViewById(R.id.delete_fav_btn);

          vi.setTag(holder);
       }else{
           holder = (ViewHolder)vi.getTag();
       }

       holder.tv.setText(names.get(position));

      if(DELETE_SIT!=1){
          try {
              holder.img.setImageBitmap(loadBitmap(url));
          } catch (IOException e) {
              e.printStackTrace();
          }
          holder.img.setVisibility(View.VISIBLE);
          holder.artiImg.setVisibility(View.INVISIBLE);
      }
      else{
          holder.img.setVisibility(View.INVISIBLE);
          holder.artiImg.setVisibility(View.VISIBLE);
      }

   }

  return vi;
}

static class ViewHolder{
    public TextView tv;
    public ImageView img;
    public ImageView artiImg;
}

如果您还有问题,请告诉我。