0

我在 gridview 的每个单元格中有 2 个图像。第一个图像是不可见的,第二个图像是可见的。我想要的是当我使用长按任何网格视图项目时,不可见的图像应该是可见的。谢谢。

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;
   }
4

1 回答 1

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;
}

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

于 2012-07-11T09:08:30.917 回答